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;