首页 > 其他分享 >STL容器——string

STL容器——string

时间:2022-12-24 17:12:09浏览次数:31  
标签:容器 const string STL char int 字符串 include

1.string容器

本质:string是C++风格的字符串,但是string本质是一个类,string字符串的第一个字符位置为0。

区别string和char*:

char*是一个指针。

string是一个类,类内有char*,有一个char*的容器来管理这个字符串(string)

优:

string类内部封装了很多成员方法,像是:查找find,拷贝copy,删除delete,替换replace,插入insert等。

同时string管理char*分配的内存,无需担心复制越界或是取值越界等问题,这些均有string类内部自主处理。

 

①string是一个类,他的一些构造函数如下:

●string();  //创建一个空的字符串,例如:string str;

  string(const char* s);  //用字符串s来初始化

●string(const string& str);  //用一个string对象初始化另一个string对象

●string(int n, char c);  //使用n个字符c,来初始化

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 void test1(){
 6     string s1;
 7 
 8     const char* str = "hello world";
 9     string s2(str);
10     cout << "s2=" << s2 << endl;
11 
12     string s3(s2);
13     cout << "s3=" << s3 << endl;
14 
15     string s4(10 ,'a');
16     cout << "s4=" << s4 << endl;
17 }
18 
19 int main(){
20     test1();
21     system("pause");
22     return 0;
23 }

②string的赋值

作用:给字符串赋值啊。。

赋值的函数原型如下:

● string& operator= (const char* s);  //char*类型字符串 赋值给当前的字符串

● string& operator= (const string &s);  //把字符串s赋值给当前字符串

● string& operator= (char c);  //用字符串赋值给当前的字符串

● string& assign(const char* s);  //把字符串s赋值给当前字符串

● string& assign(const char* s, int n);  //把字符串的前n个字符赋值给当前字符串

● string& assign(const string &s);  //把字符串s赋值给当前字符串

● string& assign(int n, char c);  //用n个字符c赋值给当前字符串

例子:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 //operator=的三个例子
 7 void test01(){
 8     string str1;
 9     str1 = "nihao";
10     cout << "str1=" << str1 << endl;
11 
12     string str2;
13     str2 = str1;
14     cout << "str2=" << str2 << endl;
15 
16     string str3;
17     str3 = 'a';
18     cout << "str3=" << str3 << endl;
19 }
20 
21 //assign的四个例子
22 void test02(){
23     string str4;
24     str4.assign("wow,hhh");
25     cout << "str4=" << str4 << endl;
26 
27     string str5;
28     str5.assign("ab,cdef",3);
29     cout << "str5=" << str5 << endl;
30 
31     string str6;
32     str6.assign(str4);
33     cout << "str6=" << str6 << endl;
34 
35     string str7;
36     str7.assign(5,'h');
37     cout << "str7=" << str7 << endl;
38 }
39 
40 int main(){
41     //test01();
42     test02();
43     system("pause");
44     return 0;
45 }

③string字符串拼接

作用:在字符串末尾拼接字符串。。

函数原型如下:

● string& operator+= (const char* str);  //重载+=,在当前字符串后面连接新的字符串

● string& operator+= (const char c);  //重载+=,在当前字符串后面增加一个字符c

● string& operator+= (const string& str);  //重载+=,在当前字符串后面连接另一个字符串

● string& append(const char *s);  //把字符串s连接到当前字符串结尾

● string& append(const char *s, int n);  //把字符串s的前n个字符连接到当前字符串结尾

● string& append(const string &s);  //同string& operator+= (const string& str);

● string& append(const string &s, int pos, int n);  //字符串s中从pos开始的n个字符连接到当前字符串结尾

例子:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 //operator
 6 void test01(){
 7     string str1 = "你好你好";
 8     str1 += ",哈哈哈";
 9     cout << "str1= " << str1 << endl;
10 
11     string str2 = "wowow";
12     str2 += 'c';
13     cout << "str2= " << str2 << endl;
14 
15     string str3 = "ooo";
16     str3 += str1;
17     cout << "str3= " << str3 << endl;
18 }
19 
20 //append
21 void test02(){
22     string str4 = "hh";
23     str4.append("66666");
24     cout << "str4= " << str4 << endl;
25 
26     string str5 = "324";
27     str5.append("abc,defg",4);
28     cout << "str5= " << str5 << endl;
29 
30     string str6 = "nihao:";
31     str6.append(str4);
32     cout << "str4= " << str4 << endl;
33 
34     string str7 = "mmm";
35     str7.append("123456",3,2);
36     cout << "str7= " << str7 << endl;
37 }
38 
39 int main(){
40     // test01();
41     test02();
42     system("pause");
43     return 0;
44 }

④string查找替换

作用:

1.查找:查找指定字符串是否存在(有就返回相应的值[切记位置由字符串开头为1开始往后数,无论你指定第几个开始查找],没有就返回-1)

2.替换:在指定位置替换字符串

函数原型如下:

区别find和rfind:find是从左往右查找,rfind是从右往左查找。。

例子:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 //查找
 5 void test1(){
 6     string str1 = "012345678910";
 7     int a = str1.find("10",5);  //无论从第几个位置开始找,总会返回从字符串开头到指定字符串的位置的值(10),而非从第5个字符开始,就返回5
 8     cout << "a= " << a <<endl;
 9 
10     int b = str1.rfind("10");
11     cout << "b= " << b <<endl;
12 }
13 //替换
14 void test2(){
15     string str2 = "hi,gg!"; //6个字符
16     str2.replace(3, 2,"world"); //从第3个后面开始的2个字符(gg),替换为world
17     cout << "str2= " << str2 << endl;
18 }
19 
20 int main(){
21     // test1();
22     test2();
23     system("pause");
24     return 0;
25 }

⑤string字符串比较

作用:比较字符串(按ASCII码来进行比较,等于返回0,大于返回1,小于返回-1)。。

在中文中,比较大小主要用来检测两个字符串是否相等,大小没有什么意义。

函数原型如下:

例子:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 void test(){
 6     string s1 = "bi";
 7     string s2 = "bi";
 8     string s3 = "ai";
 9     string s4 = "ci";
10 
11     int a = s1.compare(s2);
12     int b = s1.compare(s3);
13     int c = s1.compare(s4);
14 
15     //在abc的ASCII中a值最小,b居中,c最大
16     cout << "a= " << a <<endl;
17     cout << "b= " << b <<endl;
18     cout << "c= " << c <<endl;
19 }
20 
21 int main(){
22     test();
23     system("pause");
24     return 0;
25 }

⑥string单个字符存取

对于string的单个字符存取有两种方式:

也可以通过此种方式修改字符串里面的某个字符。。

例子:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 void test(){
 6     string s = "hello";
 7     for(int i = 0; i < s.size() ; i++){
 8         cout << s[i] << endl;
 9     }
10     for(int i = 0; i < s.size() ; i++){
11         cout << s.at(i) << endl;
12     }
13 
14     s[0] = 'x';
15     s.at(4) = 'w';
16     cout << "s= " << s << endl;
17 }
18 
19 int main(){
20     test();
21     system("pause");
22     return 0;
23 }

⑦string的插入和删除

作用:字面意思。。

函数原型如下:

例子:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 void test(){
 6     string s1 = "hello";
 7     s1.insert(2,"wow"); //从第2个字符后开始插入wow
 8     cout << "s1= " << s1 << endl;
 9 
10     string s2 = "hi";
11     s2.insert(1,s1);    //从第1个字符后开始插入字符串s1
12     cout << "s2= " << s2 << endl;
13 
14     s1.erase(3,4);
15     cout << "s1= " << s1 << endl;
16 }
17 
18 int main(){
19     test();
20     system("pause");
21     return 0;
22 }

 ⑧string子串

作用:从字符串中获取指定的字串。

函数原型:string substr(int pos = 0, int n = npos) const;  //返回由pos开始的n个字符组成的字符串

人话:新的字符串名字 = 原字符串名字.substr(pos,npos);

标签:容器,const,string,STL,char,int,字符串,include
From: https://www.cnblogs.com/MorningMaple/p/16989049.html

相关文章