深圳网站 建设,经营网站icp备案要求,温州手机网站推广,深圳网络推广培训目录 1.STL简介
1.1什么是
1.2STL的历史版本
1.3STL的六大组件 编辑
1.4有用的网址
2.string类
2.1string的多种定义方式
2.2string的插入
2.2.1尾插#xff08;push_back#xff09;
2.2.2insert插入
2.3拼接#xff08;append#xff09;
2.4删除
2.4.1尾…目录 1.STL简介
1.1什么是
1.2STL的历史版本
1.3STL的六大组件 编辑
1.4有用的网址
2.string类
2.1string的多种定义方式
2.2string的插入
2.2.1尾插push_back
2.2.2insert插入
2.3拼接append
2.4删除
2.4.1尾删pop_back
2.4.2使用erase删除
2.5string的查找
2.5.1使用find函数正向搜索
2.5.2使用rfind函数反向搜索第一个匹配项
2.6string的比较
2.7string的替换
2.8string的交换
2.9string的大小及容量
2.9.1获取有效字符的个数
2.9.3使用capacity函数获取所分配的空间的大小 2.9.4使用resize改变当前对象的有效字符的个数
2.9.5用reserve改变当前对象容量的大小
2.9.6使用clear删除对象的内容删除后对象变为空字符串
2.9.7使用empty判断对象是否为空 2.10string中元素的多种访问方式
2.10.1下标
2.10.2使用at访问对象中的元素
2.10.3使用范围for访问对象中的元素
2.10.4使用迭代器访问对象中的元素
2.11string中的运算符
2.11.1
2.11.2
2.11.3
2.11.4operator 和 operator
2.11.5relational operators
2.12string中与迭代器相关的函数
2.12.1与正向迭代器相关的函数
2.12.2与反向迭代器相关的函数
2.13string与字符串之间的转换
2.13.1将字符串转换为string
2.13.2使用c_str或data将string转换为字符串
2.14string中子字符串的提取
2.14.1使用substr函数提取string中的子字符串
2.14.3使用copy函数将string的子字符串复制到字符数组中
2.15string中的getline函数
2.15.1用法一
2.15.2用法二 1.STL简介
1.1什么是 STL(standard template libaray-标准模板库)是C标准库的重要组成部分不仅是一个可复用的组件库而且是一个包罗数据结构与算法的软件框架。 1.2STL的历史版本 原始版本 Alexander Stepanov、Meng Lee 在惠普实验室完成的原始版本本着开源精神他们声明允许 任何人任意运用、拷贝、修改、传播、商业使用这些代码无需付费。唯一的条件就是也需要向原 始版本一样做开源使用。 HP版本--所有STL实现版本的始祖。 P. J. 版本 由P. J. Plauger开发继承自HP版本被Windows Visual C采用不能公开或修改缺陷可读 性比较低符号命名比较怪异。 RW版本 由Rouge Wage公司开发继承自HP版本被C Builder 采用不能公开或修改可读性一 般。 SGI版本 由Silicon Graphics Computer SystemsInc公司开发继承自HP版 本。被GCC(Linux)采用可 移植性好可公开、修改甚至贩卖从命名风格和编程 风格上看阅读性非常高。我们后面学习 STL要阅读部分源代码主要参考的就是这个版本。 1.3STL的六大组件 1.4有用的网址
cplusplus.com
2.string类
2.1string的多种定义方式
string(); //构造一个空字符串string(const char* s); //用C-string构造string类string(const char* s, size_t n); //用C-string的前n个字符构造string类string(size_t n, char c); //用n个c字符构造string类string(const string str); //拷贝构造string(const string str, size_t pos, size_t len npos); //用从pos位置开始的长度为len的字串来构造string类 示例
string s1;
string s2(hello world);
string s3(hello world, 3);
string s4(10, a);
string s5(s2);
string s6(s2, 0, 5); 2.2string的插入
2.2.1尾插push_back 函数void push_back (char c); 示例
#include iostream
#include string
using namespace std;
int main()
{string s;s.push_back(X);s.push_back(Y);s.push_back(W);s.push_back(L);cout s endl; //XYWLreturn 0;
}2.2.2insert插入 函数 string insert (size_t pos, const string str);//在pos位置插入string对象 string insert (size_t pos, const char* s);//在pos位置插入字符串str iterator insert (iterator p, char c);//在pos位置插入字符char 示例
#include iostream
#include string
using namespace std;
int main()
{string s(X); //X//insert(pos, string)在pos位置插入string对象string l(Y);s.insert(2, l); //XY//insert(pos, str)在pos位置插入字符串strs.insert(1, W); //XYW//insert(pos, char)在pos位置插入字符chars.insert(s.end(), L); //XYWLcout s endl; //XYWLreturn 0;
}2.3拼接append 函数string append (const string str);//拼接string对象 string append (const char* s);//拼接字符串str string append (size_t n, char c);拼接字符char 示例
#include iostream
#include string
using namespace std;
int main()
{string s1(I);string s2( love);s1.append(s2); //I loves1.append( HHR); //I love HHRs1.append(3, !); //I love HHR!!!cout s1 endl; //I love HHR!!!return 0;
}2.4删除
2.4.1尾删pop_back 函数void pop_back(); 示例
#include iostream
#include string
using namespace std;
int main()
{string s(XYWL);s.pop_back();s.pop_back();s.pop_back();cout s endl; //Xreturn 0;
}2.4.2使用erase删除 函数string erase (size_t pos 0, size_t len npos); iterator erase (iterator p); iterator erase (iterator first, iterator last); 示例
#include iostream
#include string
using namespace std;
int main()
{string s(I love XYWL!!!);//erase(pos, n)删除pos位置开始的n个字符s.erase(11, 3); //I love XYWL//erase(pos)删除pos位置的字符s.erase(s.end()-1); //I love XYW//erase(pos1, pos2)删除[pos1,pos2)上所有字符s.erase(s.begin() 1, s.end()); //Icout s endl; //Ireturn 0;
}2.5string的查找
查找分为正向和反向
2.5.1使用find函数正向搜索 函数size_t find (const string str, size_t pos 0) const;//搜索与string对象所匹配的第一个位置 size_t find (const char* s, size_t pos 0) cons//搜索与字符串str所匹配的第一个位置 size_t find (char c, size_t pos 0) const;//搜索与字符char所匹配的第一个位置 示例
#include iostream
#include string
using namespace std;
int main()
{string s1(https://www.acwing.com/problem/content/submission/1/);string s2(www);size_t pos1 s1.find(s2);cout pos1 endl; //8char str[] acwing.com;size_t pos2 s1.find(str);cout pos2 endl; //12size_t pos3 s1.find(:);cout pos3 endl; //5return 0;
}2.5.2使用rfind函数反向搜索第一个匹配项 函数size_t rfind (const string str, size_t pos npos) const;搜索与string对象所匹配的第一个位置size_t rfind (const char* s, size_t pos npos) const;//搜索与字符串str所匹配的第一个位置size_t rfind (char c, size_t pos npos) const;//搜索与字符char所匹配的第一个位置 #define _CRT_SECURE_NO_WARNINGS 1
#include iostream
#include string
using namespace std;
int main()
{string s1(https://www.acwing.com/problem/content/submission/1/);string s2(1);size_t pos1 s1.rfind(s2);cout pos1 endl; //50char str[] content;size_t pos2 s1.rfind(str);cout pos2 endl; //31size_t pos3 s1.rfind(/);cout pos3 endl; //51return 0;
}2.6string的比较 函数int compare (const string str) const;//雷同 int compare (size_t pos, size_t len, const string str) const;//雷同 int compare (size_t pos, size_t len, const string str, size_t subpos, size_t sublen) const;//雷同 比较规则同C语言 1、比较字符串中第一个不匹配的字符值较小或者所有比较字符都匹配但比较字符串较短则返回小于0的值。 2、比较字符串中第一个不匹配的字符值较大或者所有比较字符都匹配但比较字符串较长则返回大于0的值。 3、比较的两个字符串相等则返回0。 总结括号外的大为1小-1同0。 示例
#include iostream
#include string
using namespace std;
int main()
{string s1(hello world);string s2(hello hhy);//hello world和hello hhy比较cout s1.compare(s2) endl; //1//ello和hello hhy比较cout s1.compare(1, 4, s2) endl; //-1//hello和hello比较cout s1.compare(0, 4, s2, 0, 4) endl; //0return 0;
}2.7string的替换 函数string replace (size_t pos, size_t len, const char* s);//将pos位置开始的len个字符替换为字符串str string replace (size_t pos, size_t len, size_t n, char c);将pos位置开始的len个字符替换为n个字符char 示例
#include iostream
#include string
using namespace std;
int main()
{string s(hello world);s.replace(6, 4, XTWL); //hello XYWLd//replace(pos, len, n, char)将pos位置开始的len个字符替换为n个字符chars.replace(10, 1, 1, !); //hello XYWL!cout s endl;return 0;
}2.8string的交换 函数void swap (string x, string y); void swap (string str); #include iostream
#include string
using namespace std;
int main()
{string s1(hello);string s2(world);swap(s1, s2);cout s1 endl; //worldcout s2 endl; //hellos1.swap(s2);cout s1 endl; //hellocout s2 endl; //worldreturn 0;
}2.9string的大小及容量
2.9.1获取有效字符的个数 函数size_t size() const; size_t length() const; 示例
#include iostream
#include string
using namespace std;
int main()
{string s(XYWL);cout s.size() endl; //4cout s.length() endl; //4return 0;
}2.9.3使用capacity函数获取所分配的空间的大小 函数size_t capacity() const; #include iostream
#include string
using namespace std;
int main()
{string s(XYWL);cout s.capacity() endl; //15return 0;
} 2.9.4使用resize改变当前对象的有效字符的个数 函数void resize (size_t n); void resize (size_t n, char c); resize规则 1、当n大于对象当前的size时将size扩大到n扩大的字符为c若c未给出则默认为’\0’。 2、当n小于对象当前的size时将size缩小到n。
#include iostream
#include string
using namespace std;
int main()
{string s1(XYWL);s1.resize(20);cout s1 endl; //XYWLcout s1.size() endl; //20cout s1.capacity() endl; //31string s2(XYWL);s2.resize(20, 0);cout s2 endl; //XYWL0000000000000000cout s2.size() endl; //20cout s2.capacity() endl; //31 string s3(XYWL);s3.resize(2);cout s3 endl; //XYcout s3.size() endl; //2cout s3.capacity() endl; //15return 0;
}注意 若给出的n大于当前对象的capacity则capacity也会随其增长。
2.9.5用reserve改变当前对象容量的大小 函数void reserve (size_t n 0); reserve规则 1、当n大于对象当前的capacity时将capacity扩大到n或大于n。 2、当n小于对象当前的capacity时什么也不做与resize不同。
示例
#define _CRT_SECURE_NO_WARNINGS 1
#include iostream
#include string
using namespace std;
int main()
{string s(XYWL);cout s endl; //XYWLcout s.size() endl; //4cout s.capacity() endl; //15s.reserve(30);cout s endl; //XYWLcout s.size() endl; //4cout s.capacity() endl; //31s.reserve(3);cout s endl; //XYWLcout s.size() endl; //4cout s.capacity() endl; //31return 0;
}注此函数对size没任何影响
2.9.6使用clear删除对象的内容删除后对象变为空字符串 函数void clear(); 示例
#include iostream
#include string
using namespace std;
int main()
{string s(XYWL);//clear()删除对象的内容该对象将变为空字符串s.clear();cout s endl; //空字符串return 0;
}
2.9.7使用empty判断对象是否为空 函数bool empty() const; 示例
#include iostream
#include string
using namespace std;
int main()
{string s(XYWL);cout s.empty() endl; //0s.clear();cout s.empty() endl; //1return 0;
}2.10string中元素的多种访问方式
2.10.1下标
在string中我们可以直接使用[ ]下标访问对象中的元素。 函数char operator[] (size_t pos); const char operator[] (size_t pos) const; 示例
#include iostream
#include string
using namespace std;
int main()
{string s(XYWL);//[]下标访问对象元素for (size_t i 0; i s.size(); i){cout s[i];}cout endl;for (size_t i 0; i s.size(); i){s[i] x;}cout s endl; //xxxxreturn 0;
}
2.10.2使用at访问对象中的元素 函数char at (size_t pos); const char at (size_t pos) const; 示例
#include iostream
#include string
using namespace std;
int main()
{string s(XYWL);for (size_t i 0; i s.size(); i){cout s.at(i);}cout endl;for (size_t i 0; i s.size(); i){s.at(i) x;}cout s endl; //xxxxreturn 0;
}2.10.3使用范围for访问对象中的元素
#include iostream
#include string
using namespace std;
int main()
{string s(XYWL);for (auto e : s){cout e;}cout endl; //XYWLfor (auto e : s) {e x;}cout s endl; //xxxxreturn 0;
}2.10.4使用迭代器访问对象中的元素
#include iostream
#include string
using namespace std;
int main()
{string s(XYWL);string::iterator it1 s.begin();while (it1 ! s.end()){cout *it1;it1;}cout endl; //XYWLstring::iterator it2 s.begin();while (it2 ! s.end()){*it2 1;it2;}cout s endl; //YZXMreturn 0;
}2.11string中的运算符
2.11.1 string类中对运算符进行了重载重载后的运算符支持string类的赋值、字符串的赋值以及字符的赋值。 示例
#include iostream
#include string
using namespace std;
int main()
{string s1;string s2(XYWL);s1 s2;cout s1 endl; //XYWLs1 hello;cout s1 endl; //hellos1 0;cout s1 endl; //0return 0;
}2.11.2 string类中对运算符进行了重载重载后的运算符支持string类的复合赋值、字符串的复合赋值以及字符复合的赋值。 示例
#include iostream
#include string
using namespace std;
int main()
{string s1;string s2(hello);//支持string类的复合赋值s1 s2;cout s1 endl; //hello//支持字符串的复合赋值s1 XYWL;cout s1 endl; //hello XYWL//支持字符的复合赋值s1 !;cout s1 endl; //hello XYWL!return 0;
}
2.11.3 string类中对运算符进行了重载重载后的运算符支持以下几种类型的操作 string类 string类 string类 字符串 字符串 string类 string类 字符 字符 string类 它们相加后均返回一个string类对象。 示例
#include iostream
#include string
using namespace std;
int main()
{string s;string s1(super);string s2(man);char str[] woman;char ch !;//string类 string类s s1 s2;cout s endl; //superman//string类 字符串s s1 str;cout s endl; //superwoman//字符串 string类s str s1;cout s endl; //womansuper//string类 字符s s1 ch;cout s endl; //super!//字符 string类s ch s1;cout s endl; //!superreturn 0;
}2.11.4operator 和 operator 函数istream operator (istream is, string str); ostream operator (ostream os, const string str); 示例
#include iostream
#include string
using namespace std;
int main()
{string s;cin s; cout s endl; return 0;
}2.11.5relational operators string类中还对一系列关系运算符进行了重载它们分别是、!、、、、。 示例
#include iostream
#include string
using namespace std;
int main()
{string s1(abcd);string s2(abde);cout (s1 s2) endl; //0cout (s1 s2) endl; //1cout (s1 s2) endl; //0return 0;
}注比较的是ASCII码
2.12string中与迭代器相关的函数
2.12.1与正向迭代器相关的函数
begin函数返回一个指向字符串第一个字符的迭代器。 函数iterator begin(); const_iterator begin() const; end函数返回一个指向字符串结束字符的迭代器即’\0’。 函数iterator end(); const_iterator end() const; 示例
#include iostream
#include string
using namespace std;
int main()
{string s(hello string);string::iterator it s.begin();while (it ! s.end()){cout *it;it;}cout endl; //hello stringreturn 0;
}
2.12.2与反向迭代器相关的函数
rbegin函数返回指向字符串最后一个字符的反向迭代器。 函数reverse_iterator rbegin(); const_reverse_iterator rbegin() const; rend函数返回指向字符串第一个字符前面的理论元素的反向迭代器。 函数reverse_iterator rend(); const_reverse_iterator rend() const; 示例
#include iostream
#include string
using namespace std;
int main()
{string s(hello world);string::reverse_iterator rit s.rbegin();while (rit ! s.rend()){cout *rit;rit;}cout endl; //dlrow ollehreturn 0;
}
2.13string与字符串之间的转换
2.13.1将字符串转换为string
#include iostream
#include string
using namespace std;
int main()
{//方式一string s1(hello world);//方式二char str[] hello world;string s2(str);cout s1 endl; //hello worldcout s2 endl; //hello worldreturn 0;
}2.13.2使用c_str或data将string转换为字符串 函数const char* c_str() const; const char* data() const; 区别 1.在C98中c_str()返回 const char* 类型返回的字符串会以空字符结尾。 2.在C98中data()返回 const char* 类型返回的字符串不以空字符结尾。 3.但是在C11版本中c_str()与data()用法相同。 #include iostream
#include string
using namespace std;
int main()
{string s(hello world );const char* str1 s.data();const char* str2 s.c_str();cout str1 endl;cout str2 endl;return 0;
}2.14string中子字符串的提取
2.14.1使用substr函数提取string中的子字符串 函数string substr (size_t pos 0, size_t len npos) const; 示例
#include iostream
#include string
using namespace std;
int main()
{string s1(abcdef);string s2;s2 s1.substr(2, 4);cout s2 endl; //cdefreturn 0;
}2.14.3使用copy函数将string的子字符串复制到字符数组中 函数size_t copy (char* s, size_t len, size_t pos 0) const; #include iostream
#include string
using namespace std;
int main()
{string s(abcdef);char str[20];//copy(str, n, pos)复制pos位置开始的n个字符到str字符串size_t length s.copy(str, 4, 2);//copy函数不会在复制内容的末尾附加\0需要手动加str[length] \0;cout str endl; //dcefreturn 0;
}2.15string中的getline函数
因为在之前我们知道在读入字符串是时cin当读到空格时就会停止于是我们有了getline。
2.15.1用法一 函数istream getline (istream is, string str); getline函数将从is中提取到的字符存储到str中直到读取到换行符’\n’为止。
#include iostream
#include string
using namespace std;
int main()
{string s;getline(cin, s); //输入hello CSDNcout s endl; //输出hello CSDNreturn 0;
}2.15.2用法二 函数istream getline (istream is, string str, char delim); getline函数将从is中提取到的字符存储到str中直到读取到分隔符delim或换行符’\n’为止。
#include iostream
#include string
using namespace std;
int main()
{string s;getline(cin, s, r); //输入hello worldcout s endl; //输出hello woreturn 0;
}