[转载]stl之string类用法详细总结

原文地址:https://blog.csdn.net/lsh_2013/article/details/46728993

标准c++中String类非常强大,合理使用,能极大提高编程效率,下面就对string类的用法进行总结。

头文件

#include<string>

String类的构造函数如下:

1) string s; //生成一个空字符串s

2) string s(str) //拷贝构造函数生成str的复制品

3) string s(str,index) //将字符串str内“始于位置index”的部分当作字符串的初值

4) string s(str,index, n) //将字符串str内“始于index且长度顶多n”的部分作为字符串的初值

5) string s(cstr) //将C字符串作为s的初值

6) string s(chars,chars_len) //将C字符串前chars_len个字符作为字符串s的初值。

7) string s(n,c) //生成一个字符串,包含n个c字符

8) string s(str.begin(),str.end()) //以区间begin():end() (不包含end())内的字符作为字符串s的初值


#include <iostream>
#include <string>
using namespace std;
int main ()
{
    //定义
    string s0 ("abcdefghijklmn");
    string s1;
    string s2 (s0);
    string s3(s0,3);
    string s4 (s0,3, 4);
    string s5 ("let us learn string");
    string s6 ("let us learn string",6);
    string s7 (10, 'x');    
    string s8 (s0.begin(), s0.begin()+7);
    //输出
    cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " << s3;
    cout << "\ns4: " << s4 << "\ns5: " << s5 << "\ns6: " << s6;
    cout << "\ns7: " << s7 << "\ns8: " << s8 << '\n';
    return 0;
}

String类常用的操作函数

之后会对相关函数进行讲解,如果不想将下面操作函数全部看完,大伙可以找自己感兴趣的函数看。

1) =,assign()   //赋以新值

2) swap()   //交换两个字符串的内容

3)+=,append(),push_back() //在尾部添加字符

4) insert() //插入字符

5) erase() //删除字符

6) clear() //删除全部字符

7) replace() //替换字符

8) + //串联字符串

9)==,!=,<,<=,>,>=,compare()  //比较字符串

10)size(),length()  //返回字符数量

11) max_size() //返回字符的可能最大个数

12) empty()  //判断字符串是否为空

13) capacity() //返回重新分配之前的字符容量

14) reserve() //保留一定量内存以容纳一定数量的字符

15) [ ], at() //存取单一字符

16)>>,getline() //从stream读取某值

17) <<  //将谋值写入stream

18) copy() //将某值赋值为一个C_string

19) c_str() //将内容以C_string返回

20) data() //将内容以字符数组形式返回

21) substr() //返回某个子字符串

22)查找函数

23)begin() end() //提供类似STL的迭代器支持

24) rbegin() rend() //逆向迭代器

25) get_allocator() //返回配置器

字符串添加


#include <string>
#include <iostream>
using namespace std;
int main(void)
{
    string s1;
    //尾部插入字符
    s1.push_back('a');
    s1.push_back('b');
    s1.push_back('c');
    cout << "打印s1: " << s1 << endl;
    char* cArray="efgh";
    string s2(cArray);
    cout << "打印s2: " << s2 << endl;
    //字符串的"+"操作
    cout << "打印s1+s2: " << s1 + s2 << endl;
    //字符串s1后添加字符串s2
    cout << "append后,打印s1: " << s1.append(s2) << endl;
    //在s1的第个字符位置前插入字符'8'
    s1.insert(s1.begin()+1,'8');
    cout << "insert后,打印s1: " << s1 << endl;
    //字符串的"+="操作
    s1+=s2;
    cout << "s1+=s2后,打印s1: " << s1 << endl;
    return 0;
}

字符的遍历


#include <string>
#include <iostream>
using namespace std;
int main(void)
{
    char* cArray="hello, world!";
    string s(cArray);
    //数组方式
    for(unsigned int j=0; j< s.size(); j++)
        cout << "第" << j << "个字符: " << s[j] << endl;
    //迭代器方式
    string::reverse_iterator ri;
    cout << "反向打印字符" << endl;
    for(ri=s.rbegin(); ri!=s.rend(); ri++)
        cout << *ri << ' ' ;
    cout << endl;
    return 0;
}

字符的删除

1)iterator erase(iterator p);//删除字符串中p所指的字符

2)iterator erase(iterator first, iterator last);//删除字符串中迭代器

区间[first,last)上所有字符

3)string& erase(size_t pos = 0, size_t len = npos);//删除字符串中从索引

位置pos开始的len个字符

4)void clear();//删除字符串中所有字符

#include <string>
#include <iostream>
using namespace std;
int main(void)
{
    string s("12345678a");
    s.erase(s.begin());
    cout << s << endl; //打印出:a 
    s.erase(s.begin()+3, s.end()-2);
    cout << s << endl; //打印出:a
    s.erase(0,2);
    cout << s << endl; //打印出:a
    s.clear();
    return 0;
}

字符的替换

1)string& replace(size_t pos, size_t n, const char *s);//将当前字符串

从pos索引开始的n个字符,替换成字符串s

2)string& replace(size_t pos, size_t n, size_t n1, char c); //将当前字符串

从pos索引开始的n个字符,替换成n1个字符c

3)string& replace(iterator i1, iterator i2, const char* s);//将当前字符串

[i1,i2)区间中的字符串替换为字符串s

#include <string>
#include <iostream>
using namespace std;
int main(void)
{
    string s("hello,boy!");
    s.replace(6,3,"gril");  //boy替换为girl
    cout << s << endl;      //打印hello gril!
    s.replace(10,1,1,'.');  //将"hello gril!"的'!'替换为'.'
    cout << s << endl;      //打印hello gril.
    s.replace(s.begin(),s.begin()+5, "good morning");
    cout << s << endl;     //打印good morning girl.
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhenaoxi1077/article/details/80432740