白家乐网站怎么建站,晋城市公共事业建设局网站,深圳前十网站建设公司,网站设计知识准备STL简介
什么是STL
STL(standard template libaray-标准模板库)#xff1a;是C标准库的重要组成部分#xff0c;不仅是一个可复用的组件库#xff0c;而且是一个包罗数据结构与算法的软件框架
STL的六大组件
仿函数#xff1a;greater、less、…算法#xff1a;find、…STL简介什么是STLSTL(standard template libaray-标准模板库)是C标准库的重要组成部分不仅是一个可复用的组件库而且是一个包罗数据结构与算法的软件框架STL的六大组件仿函数greater、less、…算法find、swap、reverse、sort、merge、…迭代器iterator、const_iterator、reverse_iterator、const_reverse_iterator空间配置器allocator容器string、vector、list、deque、map、set、multimap、multiset配接器stack、queue、priority_queueC语言中的字符串C语言中字符串是以’\0’结尾的一些字符的集合为了操作方便C标准库中提供了一些str系列的库函数但是这些库函数与字符串是分离开的不太符合OOP的思想而且底层空间需要用户自己管理稍不留神可能还会越界访问。标准库中的string类string类cplusplus.com/reference/string/string/?kwstring在使用string类时必须包含#include头文件以及using namespace std;auto和范围forauto关键字在早期C/C中auto的含义是使用auto修饰的变量是具有自动存储器的局部变量后来这个不重要了。C11中标准委员会变废为宝赋予了auto全新的含义即auto不再是一个存储类型指示符而是作为一个新的类型指示符来指示编译器auto声明的变量必须由编译器在编译时期推导而得。用auto声明指针类型时用auto和auto*没有任何区别但用auto声明引用类型时则必须加当在同一行声明多个变量时这些变量必须是相同的类型否则编译器将会报错因为编译器实际只对第一个类型进行推导然后用推导出来的类型定义其他变量。auto不能作为函数的参数可以做返回值但是建议谨慎使用auto不能直接用来声明数组#includeiostream using namespace std; int func1() { return 10; } // 不能做参数 void func2(auto a) {} // 可以做返回值但是建议谨慎使用 auto func3() { return 3; } int main() { int a 10; auto b a; auto c a; auto d func1(); // 编译报错:rror C3531: “e”: 类型包含“auto”的符号必须具有初始值设定项 auto e; cout typeid(b).name() endl; cout typeid(c).name() endl; cout typeid(d).name() endl; int x 10; auto y x; auto* z x; auto m x; cout typeid(x).name() endl; cout typeid(y).name() endl; cout typeid(z).name() endl; auto aa 1, bb 2; // 编译报错error C3538: 在声明符列表中“auto”必须始终推导为同一类型 auto cc 3, dd 4.0; // 编译报错error C3318: “auto []”: 数组不能具有其中包含“auto”的元素类型 auto array[] { 4, 5, 6 }; return 0; }#includeiostream #include string #include map using namespace std; int main() { std::mapstd::string, std::string dict { { apple, 苹果 },{ orange, 橙子 }, {pear,梨} }; // auto的用武之地 //std::mapstd::string, std::string::iterator it dict.begin(); auto it dict.begin(); while (it ! dict.end()) { cout it-first : it-second endl; it; } return 0; }范围for对于一个有范围的集合而言由程序员来说明循环的范围是多余的有时候还会容易犯错误。因此C11中引入了基于范围的for循环。for循环后的括号由冒号“ ”分为两部分第一部分是范围内用于迭代的变量第二部分则表示被迭代的范围自动迭代自动取数据自动判断结束。范围for可以作用到数组和容器对象上进行遍历范围for的底层很简单容器遍历实际就是替换为迭代器这个从汇编层也可以看到。#includeiostream #include string #include map using namespace std; int main() { int array[] { 1, 2, 3, 4, 5 }; // C98的遍历 for (int i 0; i sizeof(array) / sizeof(array[0]); i) { array[i] * 2; } for (int i 0; i sizeof(array) / sizeof(array[0]); i) { cout array[i] endl; } // C11的遍历 for (auto e : array) e * 2; for (auto e : array) cout e endl; string str(hello world); for (auto ch : str) { cout ch ; } cout endl; return 0; }string类的常用接口说明string类对象的常见构造(constructor)函数名称功能说明string()构造空的string类对象即空字符串string(const char* s)用C-string来构造string类对象string(size_t n, char c)string类对象中包含n个字符cstring(const strings)拷贝构造函数void Teststring() { string s1; // 构造空的string类对象s1 string s2(hello bit); // 用C格式字符串构造string类对象s2 string s3(s2); // 拷贝构造s3 }string类对象的容量操作函数名称功能说明size返回字符串有效字符长度length返回字符串有效字符长度capacity返回空间总大小empty检测字符串是否为空串是返回 true否则返回 falseclear清空有效字符reserve为字符串预留空间 **resize将有效字符的个数改成 n 个多出的空间用字符 c 填充扩容// reserve 保留 // reverse 反转 逆置 int main() { try { string s1; string s2(hello world); // 实践中没有参考和使用的价值 cout s1.max_size() endl; cout s2.max_size() endl; s1.reserve(s1.max_size()); } catch (const exception e) { cout e.what() endl; } return 0; }检查扩容情况int main() { try { string s1; string s2(hello worldxxxxxxxxxxxxx); // 确定需要多少空间提前开好空间即可 s1.reserve(500); size_t old s1.capacity(); cout old endl; for (size_t i 0; i 500; i) { s1.push_back(x); if (old ! s1.capacity()) { cout s1.capacity() endl; old s1.capacity(); } } } catch (const exception e) { cout e.what() endl; } return 0; }扩容倍数大致是1.5reserve一般不会缩容不会删除数据最小缩到size()resizeint main() { string s1(hello world); cout s1.size() endl; cout s1.capacity() endl; cout s1 endl; // capacity -》 扩容尾插 //s1.resize(100); s1.resize(100, x); cout s1.size() endl; cout s1.capacity() endl; cout s1 endl; // size n capacity - 尾插 string s2(hello world); cout s2.size() endl; cout s2.capacity() endl; cout s2 endl; s2.resize(12); cout s2.size() endl; cout s2.capacity() endl; // n size - 删除数据保留前n个 string s3(hello world); cout s3.size() endl; cout s3.capacity() endl; cout s3 endl; s3.resize(5); cout s3.size() endl; cout s3.capacity() endl; string s5; s5.resize(100, #); cout s5 endl; return 0; }注意1. size()与length()方法底层实现原理完全相同引入size()的原因是为了与其他容器的接口保持一致一般情况下基本都是用size()。2. clear()只是将string中有效字符清空不改变底层空间大小。3. resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个不同的是当字符个数增多时resize(n)用0来填充多出的元素空间resize(size_t n, char c)用字符c来填充多出的元素空间。注意resize在改变元素个数时如果是将元素个数增多可能会改变底层容量的大小如果是将元素个数减少底层空间总大小不变。4. reserve(size_t res_arg0)为string预留空间不改变有效元素个数当reserve的参数小于string的底层空间总大小时reserver不会改变容量大小。string类对象的访问及遍历操作函数名称功能说明operator []返回 pos 位置的字符const string 类对象调用begin endbegin 获取一个字符的迭代器 end 获取最后一个字符下一个位置的迭代器rbegin rendbegin 获取一个字符的迭代器 end 获取最后一个字符下一个位置的迭代器范围 forC11 支持更简洁的范围 for 的新遍历方式[]int main() { string s1(hello world); cout s1.size() endl; cout s1.length() endl; for (size_t i 0; i s1.size(); i) { cout s1[i] ; //cout s1.operator[](i) ; } cout endl; return 0; }// string给逆置一下 size_t begin 0, end s1.size() - 1; while (begin end) { /*char tmp s1[begin]; s1[begin] s1[end]; s1[end] tmp;*/ swap(s1[begin], s1[end]); begin; --end; } cout s1 endl;iterator// iterator用法像指针 string::iterator it s1.begin(); while (it ! s1.end()) { *it 1; cout *it ; it; } cout endl; reverse(s1.begin(), s1.end()); cout s1 endl;范围forint main() { string s1(hello world); const string s2(hello world); s1[0] x; //s2[0] x; cout s2[0] endl; string::const_iterator it s2.begin(); while (it ! s2.end()) { //*it 1; cout *it ; it; } cout endl; // yyds for (auto e : s1) { cout e ; } cout endl; return 0; }反向迭代器void func(const string s) { //string::const_reverse_iterator it s.rbegin(); auto it s.rbegin(); while (it ! s.rend()) { // *it x; cout *it ; it; } cout endl; auto it s.crbegin(); while (it ! s.crend()) { // *it x; cout *it ; it; } cout endl; } int main() { string s1(hello world); // 遍历 // 1、下标[] // 2、迭代器 // 3、范围for string::reverse_iterator it1 s1.rbegin(); while (it1 ! s1.rend()) { //*it1 x; cout *it1 ; it1; } cout endl; func(s1); return 0; }at抛异常[]是程序报错int main() { try { string s1(hello world); cout s1[11] endl; //cout s1[20] endl; cout s1.at(20) endl; } catch (const exception e) { cout e.what() endl; } return 0; }string类对象的修改操作函数名称功能说明push_back在字符串后尾插字符 cappend在字符串后追加一个字符串operator在字符串后追加字符串 strc_str返回 C 格式字符串find npos从字符串 pos 位置开始往后找字符 c返回该字符在字符串中的位置rfind从字符串 pos 位置开始往前找字符 c返回该字符在字符串中的位置substr在 str 中从 pos 位置开始截取 n 个字符然后将其返回// 增 (push_back/append)/insert // 删 erase // 查 [] // 改 []/迭代器 int main() { string s1(hello); s1.push_back( ); s1.append(world); cout s1 endl; string s2 xxxx; const string s3 xxxx; s2.append(s1.begin(), --s1.end()); cout s2 endl; s1 !; s1 xxxxx; s1 s2; cout s1 endl; return 0; }insert、eraseint main() { //string s1(hello world); //s1.insert(5, xxxx); //cout s1 endl; ////s1.insert(0, 1, x); //s1.insert(s1.begin(), y); //cout s1 endl; string s1(hello world); s1.insert(5, xxxx); cout s1 endl; s1.insert(0, 1, x); s1.insert(s1.begin(), y); cout s1 endl; s1.erase(5, 4); cout s1 endl; s1.erase(5); cout s1 endl; return 0; }find、replace、s2.swap(s3)表示交换指针int main() { string s1(hello world hello bit); cout s1 endl; // 所有的空格替换为20% size_t pos s1.find( , 0); while (pos ! string::npos) { s1.replace(pos, 1, 20%); // 效率很低能不用就不要用了 pos s1.find( , pos 3); } cout s1 endl; string s2(hello world hello bit); cout s2 endl; string s3; for (auto ch : s2) { if (ch ) { s3 20%; } else { s3 ch; } } cout s3 endl; s2.swap(s3); cout s2 endl; return 0; }c_str()int main() { string filename(Test.cpp); FILE* fout fopen(filename.c_str(), r); char ch fgetc(fout); while (ch ! EOF) { cout ch; ch fgetc(fout); } return 0; }substr()int main() { string s1(Test.cpp); string s2(Test.tar.zip); size_t pos1 s1.rfind(.); if (pos1 ! string::npos) { //string suff s1.substr(pos1, s1.size() - pos1); string suff s1.substr(pos1); cout suff endl; } size_t pos2 s2.rfind(.); if (pos2 ! string::npos) { string suff s2.substr(pos2); cout suff endl; } string str(https://legacy.cplusplus.com/reference/string/string/substr/); string sub1, sub2, sub3; pos1 str.find(:); sub1 str.substr(0, pos1 - 0); cout sub1 endl; pos2 str.find(/, pos13); sub2 str.substr(pos1 3, pos2 - (pos1 3)); cout sub2 endl; sub3 str.substr(pos2 1); cout sub3 endl; return 0; }find_first_of()int main() { std::string str(Please, replace the vowels in this sentence by asterisks.); std::size_t found str.find_first_not_of(abc); while (found ! std::string::npos) { str[found] *; found str.find_first_not_of(abc, found 1); } std::cout str \n; string s1(xxxx); string s2 xxxx; return 0; }注意1. 在string尾部追加字符时s.push_back© / s.append(1, c) / s c’三种的实现方式差不多一般情况下string类的操作用的比较多操作不仅可以连接单个字符还可以连接字符串。2. 对string操作时如果能够大概预估到放多少字符可以先通过reserve把空间预留好。5. string类非成员函数函数功能说明operator尽量少用因为传值返回导致深拷贝效率低operator输入运算符重载operator输出运算符重载getline获取一行字符串relational operators大小比较vs和g下string结构的说明vs下string的结构注意下述结构是在32位平台下进行验证32位平台下指针占4个字节。string总共占28个字节内部结构稍微复杂一点先是有一个联合体联合体用来定义string中字符串的存储空间当字符串长度小于16时使用内部固定的字符数组来存放当字符串长度大于等于16时从堆上开辟空间union _Bxty { // storage for small buffer or pointer to larger one value_type _Buf[_BUF_SIZE]; pointer _Ptr; char _Alias[_BUF_SIZE]; // to permit aliasing } _Bx;这种设计也是有一定道理的大多数情况下字符串的长度都小于16那string对象创建好之后内部已经有了16个字符数组的固定空间不需要通过堆创建效率高。其次还有一个size_t字段保存字符串长度一个size_t字段保存从堆上开辟空间总的容量最后还有一个指针做一些其他事情。故总共占1644428个字节g下string的结构G下string是通过写时拷贝实现的string对象总共占4个字节内部只包含了一个指针该指针将来指向一块堆空间内部包含了如下字段空间总大小字符串有效长度引用计数struct _Rep_base { size_type _M_length; size_type _M_capacity; _Atomic_word _M_refcount; };指向堆空间的指针用来存储字符串。