news 2026/6/9 18:27:23

C++入门之string(一)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++入门之string(一)

目录

一、STL简介

​编辑二、STL中的方法

①string的构造函数

②string的析构函数

③赋值运算符=重载

④[]的重载

三、遍历+修改引出迭代器

方法一: 下标 + [] (小众)

方法二: 迭代器 (所有容器主流遍历+修改方式)

方法三: 范围for C++11 语法糖


一、STL简介

STL是C++标准库的重要组成部分,不仅是一个可复用的组件库,而是一个包罗数据结构和算法的软件框架

string其实就是字符数组顺序表

class string { private: char* _str; size_t size; size_t _capacity; };

string的实例化类

①编码----->utf-8

②编码------>utf-16

③编码----->utf-32

④宽字符字符串,用于兼容旧版 Windows API

编码:

ASCII:早期美国制定的编码,只用 1 个字节的低 7 位,只能表示英文字母、数字和符号,无法支持其他语言。

Unicode:它不是一种 “存储格式”,而是一套值 - 符号映射表,给全世界几乎所有字符分配了唯一的数字编号(码点)。前面说到的utf-8/utf-16/utf-32都是unicode编码。

UTF-8是如何编码的?

npos----->整数的最大值

二、STL中的方法

①string的构造函数

void test_string1() { //string(); string s1; // 无参构造(默认构造) //空字符串 //string (const char* s); string s2("hello world");// 常量字符串构造 //hello world //string (const string& str); string s3(s2); // 拷贝构造s2 //hello world //string (const string& str, size_t pos, size_t len = npos); string s4(s2, 1, 6); // 从s2下标1开始,取6个字符 //ello w //string (const string& str, size_t pos, size_t len = npos); string s5(s2, 1, 60); // 从s2下标1开始,取60个字符(不足则取到末尾)//ello world //string (const string& str, size_t pos, size_t len = npos); string s6(s2, 6); // 从s2下标6开始,取到字符串末尾 //world const char* str = "hello world"; //string (const char* s, size_t n); string s7(str, 5); // 从C字符串str取前5个字符构造 //hello //string (size_t n, char c); string s8(100, '#'); // 生成100个指定字符'#' //100个# }

②string的析构函数

③赋值运算符=重载

//(一)string& operator= (const string& str); s1 = s2; // 赋值重载:用另一个string对象str赋值 cout << s1 << endl; // hello world //(二)string& operator= (const char* s); s1 = "xxxx"; // 赋值重载:用C风格字符串const char*赋值 cout << s1 << endl; // xxxx //(三)string& operator= (char c); s1 = 'x'; // 赋值重载:用单个字符char赋值 cout << s1 << endl; // x

④[]的重载

三、遍历+修改引出迭代器

方法一: 下标 + [] (小众)

④[]的重载

for (size_t i = 0;i < s1.size();i++) { s1[i]++; } cout << s1 << endl; //jfmmp!xpsme

方法二: 迭代器 (所有容器主流遍历+修改方式)

//[begin(),end()) //s1的每个字符-- string::iterator it1 = s1.begin(); while (it1 != s1.end()) { (*it1)--; ++it1; } cout << s1 << endl; //iello world
不仅仅适用于string,vector,list等容器适用
vector<int> v; v.push_back(100000); v.push_back(2); v.push_back(3); vector<int>::iterator it2 = v.begin(); while (it2 != v.end()) { cout << *it2 << endl; ++it2; } cout << endl; list<int> lt; lt.push_back(1); lt.push_back(2); lt.push_back(3); list<int>::iterator it3 = lt.begin(); while (it3 != lt.end()) { (*it3)++; cout << *it3 << endl; ++it3; } cout << endl;

迭代器的意义:

1、同一类似方式遍历修改容器

2、算法脱离具体底层结构,和底层结构解耦

算法独立模板实现针对多个容器处理

std::reverse是算法函数,在<algorithm>里,要求传入同一容器的一对迭代器[begin, end)

reverse(s1.begin(), s1.end()); reverse(v.begin(), v.end()); reverse(lt.begin(), lt.end()); cout << s1 << endl; //dlrow ollei for (auto x : v) cout << x << " "; //3 2 100000 cout << endl; for (auto x : lt) cout << x << " "; //4 3 2 cout << endl;

方法三: 范围for C++11 语法糖

先来看看auto

auto ----自动识别类型
auto x = 10; // auto 自动推导为 int // x = 10 auto y = 10.1; // auto 自动推导为 double // y = 10.1 cout << x << endl; // 输出:10 cout << y << endl; // 输出:10.1 int& z = x; // z 是 x 的引用(别名) auto& m = z; // auto 自动推导为 int&,m 是 z/x 的别名 m++; // x 变成 11 auto p1 = &x; // auto 自动推导为 int*(指针类型) auto* p2 = &x; // 明确指定指针,auto 推导为 int,最终 p2 是 int*

范围for会自动取容器数据赋值给e,自动判断结束,自动迭代,本质底层也会替代成迭代器

for (auto& e : s1) { e++; } cout << endl; for (auto e : s1) { cout << e << " "; } cout << endl; for (auto e : v) { cout << e << " "; } cout << endl; for (auto e : lt) { cout << e << " "; } cout << endl;

遍历数组中的元素

int arr[] = { 1,2,3,4,5,6,7 }; for (int i = 0;i < sizeof(arr) / sizeof(arr[0]);i++) { cout << arr[i] << " "; } cout << endl; for (auto e : arr) { cout << e << " "; } cout << endl;
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/9 18:27:10

耐用的饲料颗粒机

在饲料加工领域&#xff0c;颗粒机的耐用性一直是困扰众多养殖户和饲料生产企业的核心痛点。根据行业调研数据表明&#xff0c;超过60%的中小型饲料厂每年因磨盘、压辊等易损件更换产生的直接成本高达5万元&#xff0c;而因设备停机导致的产能损失更是难以估量。目前&#xff0…

作者头像 李华
网站建设 2026/6/9 18:23:53

企业级GB28181视频监控平台:构建统一安防系统的终极解决方案

企业级GB28181视频监控平台&#xff1a;构建统一安防系统的终极解决方案 【免费下载链接】wvp-GB28181-pro 基于GB28181-2016、部标808、部标1078标准实现的开箱即用的网络视频平台。自带管理页面&#xff0c;支持NAT穿透&#xff0c;支持海康、大华、宇视等品牌的IPC、NVR接入…

作者头像 李华
网站建设 2026/6/9 18:15:55

无线芯片功耗与射频性能实战解析:从数据手册到PCB设计

1. 从数据手册到设计实战&#xff1a;如何解读一颗无线组合芯片的真实性能每次拿到一颗新的无线通信芯片&#xff0c;尤其是像NXP IW623P这样集成了Wi-Fi 6/6E和蓝牙的“二合一”组合方案&#xff0c;我第一件事就是翻到数据手册的“电气特性”和“功耗”章节。这几乎是所有硬件…

作者头像 李华
网站建设 2026/6/9 18:13:42

i.MX RT1020引脚配置全解析:从数据手册到硬件设计的工程实践

1. 项目概述&#xff1a;从引脚图到硬件设计的桥梁对于任何一位嵌入式硬件工程师而言&#xff0c;拿到一颗新的微控制器&#xff08;MCU&#xff09;或处理器&#xff0c;第一件要紧事就是翻开数据手册&#xff0c;找到那张至关重要的引脚配置图。这不仅仅是连接电路那么简单&a…

作者头像 李华