STL中的string
来源:互联网 发布:retrofit源码分析 编辑:程序博客网 时间:2024/06/06 04:39
1 String概念
string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是用char*表示的。string与char*都可以用来表示字符串,那么二者有什么区别呢。
string和char*的比较
string是一个类, char*是一个指向字符的指针。
string封装了char*,管理这个字符串,是一个char*型的容器。
string不用考虑内存释放和越界。
string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。
string提供了一系列的字符串操作函数(这个等下会详讲)
查找find,拷贝copy,删除erase,替换replace,插入insert
2 string的构造函数
默认构造函数:
string(); //构造一个空的字符串string s1。
拷贝构造函数:
string(const string &str); //构造一个与str一样的string。如strings1(s2)。
带参数的构造函数
string(const char *s); //用字符串s初始化
string(int n,char c); //用n个字符c初始化
3 string的存取字符操作
string类的字符操作:
const char &operator[] (int n) const;
const char &at(int n) const;
char &operator[] (int n);
char &at(int n);
operator[]和at()均返回当前字符串中第n个字符,但二者是有区别的。
主要区别在于at()在越界时会抛出异常,[]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。
4 从string取得const char*的操作
const char *c_str() const; //返回一个以'\0'结尾的字符串的首地址
5 把string拷贝到char*指向的内存空间的操作
int copy(char *s, int n, int pos=0) const;
把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目。注意要保证s所指向的空间足够大以容纳当前字符串,不然会越界。
6 string的长度
int length() const; //返回当前字符串的长度。长度不包括字符串结尾的'\0'。
bool empty() const; //当前字符串是否为空
7 string的赋值
string &operator=(const string&s);//把字符串s赋给当前的字符串
string &assign(const char *s); //把字符串s赋给当前的字符串
string &assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串
string &assign(const string&s); //把字符串s赋给当前字符串
string &assign(int n,char c); //用n个字符c赋给当前字符串
string &assign(const string &s,intstart, int n); //把字符串s中从start开始的n个字符赋给当前字符串
8 string字符串连接
string &operator+=(const string&s); //把字符串s连接到当前字符串结尾
string &operator+=(const char *s);//把字符串s连接到当前字符串结尾
string &append(const char *s); //把字符串s连接到当前字符串结尾
string &append(const char *s,int n); //把字符串s的前n个字符连接到当前字符串结尾
string &append(const string&s); //同operator+=()
string &append(const string &s,intpos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
string &append(int n, char c); //在当前字符串结尾添加n个字符c
9 string的比较
int compare(const string &s)const; //与字符串s比较
int compare(const char *s) const; //与字符串s比较
compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。
10 string的子串
string substr(int pos=0, int n=npos)const; //返回由pos开始的n个字符组成的子字符串
11 string的查找和 替换
查找
int find(char c,int pos=0) const; //从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos=0)const; //从pos开始查找字符串s在当前字符串的位置
int find(const string &s, int pos=0)const; //从pos开始查找字符串s在当前字符串中的位置
find函数如果查找不到,就返回-1
int rfind(char c, int pos=npos) const; //从pos开始从后向前查找字符c在当前字符串中的位置
int rfind(const char *s, int pos=npos)const;
int rfind(const string &s, intpos=npos) const;
//rfind是反向查找的意思,如果查找不到, 返回-1
替换
string &replace(int pos, int n, constchar *s);//删除从pos开始的n个字符,然后在pos处插入串s
string &replace(int pos, int n, conststring &s); //删除从pos开始的n个字符,然后在pos处插入串s
void swap(string &s2); //交换当前字符串与s2的值
//4 字符串的查找和替换
// 字符串的查找和替换void test5(){string s1 = "abc hello abc 111 abc 222 abc 333";// 第一次出现 abc indexint index = s1.find("abc",0);// 位置从下标0开始cout << "index: " << index<<endl;// 案例1 求abc出现的次数 以及每次出现的数组下标int offindex= s1.find("abc",0);while (offindex!=string::npos)// string::npos表示保证大于任何有效下标的值{cout << "offindex: " << offindex << endl;offindex += 1;// 下标继续往后移动,直到字符串结束为止offindex = s1.find("abc",offindex);}// 案例2 把小写abc=====>大写ABCstring s3 = "aaa bbb ccc";s3.replace(0,3,"AAA");cout << "s3: " << s3 << endl;offindex = s1.find("abc",0);while (offindex!=string::npos){cout << "offindex:" <<offindex<< endl;s1.replace(offindex,3,"ABC");offindex += 1;offindex = s1.find("abc",offindex);}cout << "s1转换后的结果是:" << s1 << endl;}
12 String的区间删除和插入
string &insert(int pos, const char *s);
string &insert(int pos, const string&s);
//前两个函数在pos位置插入字符串s
string &insert(int pos, int n, charc); //在pos位置 插入n个字符c
string &erase(int pos=0, intn=npos); //删除pos开始的n个字符,返回修改后的字符串
13 string算法相关
// string 的相关算法void test7(){string s1 = "AAAbbb";// 1 函数的入口地址 2 函数的对象 3预定义的函数对象transform(s1.begin(),s1.end(),s1.begin(),towupper);// 这里的towlower是预定义的函数对象,这里实质上是通过函数名来调用函数cout << "s1: "<<s1 << endl;string s2 = "AAAbbb";transform(s2.begin(),s2.end(),s2.begin(),tolower);// tolower 小写函数cout << "s2: " << s2 << endl;}
14 string操作的源码示例:
#include<iostream>#include<string>#include<algorithm>using namespace std;// string的初始化void test1(){string s1 = "aaaa";string s2("bbbb");string s3 = s2;// 通过拷贝构造函数来 初始化对象 s3string s4(10,'a');cout << "s1: " <<s1<< endl;cout << "s2: " << s2 << endl;cout << "s3: " << s3 << endl;cout << "s4: " << s4 << endl;}// string 的遍历void test2(){string s1 = "abcdefg";// 1 数组方式遍历for (int i = 0; i < s1.length(); i++){cout << s1[i] << " ";}cout << endl;// 2迭代器方式遍历for (string::iterator it=s1.begin();it!=s1.end();++it){cout << *it << " ";}cout << endl;// 3 抛出异常try {for (int i = 0; i < s1.length()+3; i++){cout << s1.at(i) << " ";// at() 可以抛出异常}}catch (...)// 捕捉所有异常{cout << "发生异常!" << endl;}try{for (int i = 0; i < s1.length() + 3; i++){cout << s1[i]<< " ";// 出现错误 不向外面抛出异常 程序引起的中断}}catch (...)// 捕捉所有异常{cout << "发生异常!" << endl;}}// 字符指针和 string 的转换void test3(){string s1 = "aaabbb";// 1 s1=====>char * printf("s1 : %s\n",s1.c_str());//2 char *====>string // 3 s1的内容copy进 buf 中char buf[128] = {0};s1.copy(buf,3,0);//注意 只给你copy 3 个字符 不会变成 C 风格cout << "buf: " << buf << endl;}// 字符串的连接void test4(){string s1 = "aaa";string s2 = "bbb";s1 = s1 + s2;cout << "s1: " << s1 << endl;string s3 = "333";string s4 = "444";s3.append(s4);cout << "s3: " << s3 << endl;}// 字符串的查找和替换void test5(){string s1 = "abc hello abc 111 abc 222 abc 333";// 第一次出现 abc indexint index = s1.find("abc",0);// 位置从下标0开始cout << "index: " << index<<endl;// 案例1 求abc出现的次数 以及每次出现的数组下标int offindex= s1.find("abc",0);while (offindex!=string::npos)// string::npos表示保证大于任何有效下标的值{cout << "offindex: " << offindex << endl;offindex += 1;// 下标继续往后移动,直到字符串结束为止offindex = s1.find("abc",offindex);}// 案例2 把小写abc=====>大写ABCstring s3 = "aaa bbb ccc";s3.replace(0,3,"AAA");cout << "s3: " << s3 << endl;offindex = s1.find("abc",0);while (offindex!=string::npos){cout << "offindex:" <<offindex<< endl;s1.replace(offindex,3,"ABC");offindex += 1;offindex = s1.find("abc",offindex);}cout << "s1转换后的结果是:" << s1 << endl;}// string 的截断(区间删除)和插入void test6(){string s1 = "hello1 hello2 hello1";string::iterator it =find(s1.begin(),s1.end(),'1');// 这里用到算法 find ,需要添加头文件 <algorithm>if (it!=s1.end()){s1.erase(it);}cout << "s1删除后的结果是:" <<s1<< endl;s1.erase(s1.begin(),s1.end());cout << "s1全部删除,s1: "<<s1 << endl;cout << "s1的长度是:"<<s1.length() << endl;string s2 = "BBB";s2.insert(0,"AAA");// 头插法cout << s2 << endl;s2.insert(s2.length(),"CCC");// 尾插法cout << s2 << endl;}// string 的相关算法void test7(){string s1 = "AAAbbb";// 1 函数的入口地址 2 函数的对象 3预定义的函数对象transform(s1.begin(),s1.end(),s1.begin(),towupper);// 这里的towlower是预定义的函数对象,这里实质上是通过函数名来调用函数cout << "s1: "<<s1 << endl;string s2 = "AAAbbb";transform(s2.begin(),s2.end(),s2.begin(),tolower);// tolower 小写函数cout << "s2: " << s2 << endl;}int main(void){// test1();// test2();// test3();// test4();// test5();// test6();test7();return 0;}
- STL模板中的string
- STL中的string类
- STL中的string
- 模拟实现C++STL中的string类
- STL string
- STL String
- STL string
- STL--string
- STL string
- STL string
- 【STL】string
- stl string
- STL string
- STL: string
- STL::string
- STL-String
- STL string
- STL string
- 酷狗音乐上线酷群等功能,主推社交、个性化体验
- 重回大而全!小米Max与MIUI 8发布会汇总
- 终于传出要自研GPU,是什么让苹果在自研芯片的道路上越跑越远?
- Ado.Net数据库连接笔记
- Ambari-Server Rest API处理1(基础知识准备+从Server到Handler)
- STL中的string
- windows server2012 r2 + ubuntu双系统安装方法
- print 这玩意
- Leetcode 4. Median of Two Sorted Arrays(java版)
- 留住美一刻 OPPO A37预约专享6重礼
- 96. Unique Binary Search Trees
- 会让你耳朵怀孕的Hi-Fi神器 vivo Xplay5领衔
- 腾讯丁珂:大数据激励患者自我管理
- poj 1486 Sorting Slides(二分图匹配)