(我的STL相关的博客目录:https://blog.csdn.net/Kprogram/article/details/90408011)
(string-c++官网:http://www.cplusplus.com/reference/string/string/?kw=string)
stirng有什么使用价值呢?
字符数组是C中非常常用而又麻烦的一个东西:
如果分配的初始空间不够,会造成字符丢失和数组越界;
如果没有很好的初始化(如初始化为'\0'),也可能会造成数组越界,打印时可能还会显示奇怪的字符;
即使做好了各种细节工作,对其的变换和比较等操作也是很麻烦。
于是C++提供了string类:
它长度不定,且拥有很多实用的成员函数,能在很多地方完美地替代字符数组,是一个非常便捷的东西。
它能方便的和字符数组互相转换:string 可以直接就被赋值于字符数组(char a[10]; string s = a),而copy函数可以把string中的字符复制到字符数组中(末尾有使用详解)
接下来我来讲解string的使用方法:
0 - 头文件:string 命名空间:std
#include <string>
using namespace std;
1 - 声明一个string,并为其赋值
string 有多种赋值方法:
1.不赋值:
string s0;
2.初始化时赋值:
string s1 = "deep";
string s2("dark");
string s4(10, "fantacy");
string s3 = s2; //拷贝
3.用字符数组赋值:
char sz[100];
scanf("%s", sz);
string s5 = sz;
4.用cin赋值:
string s6;
cin >> s6;
2 - 便捷的成员函数 ( 假设你创建的string对象名字叫 S )
(红色为重要函数,粉色为常用函数,带* 的为很少使用的函数,大部分情况下红+粉红函数就够用了)
容量和尺寸:
size():返回字符串的尺寸,因为是以byte为单位的,所以可能不是字符的实际数量(但ASCALL字符串的尺寸一定是其长度)
length():跟size作用一毛一样(官网上说的哈,我也实验过了)
maxsize():返回字符串的最大尺寸,没啥用(虽然string长度可变,但为了省时,它会预分配一些多的空间,所以maxsize >= size)
empy():如果字符串为空,返回True, 反之则返回 False。
clear():变为空串。
resize(n):重新设置字符串的长度,如果n小于size,则保留前n个字符。反之,随便增加字符到n,或者增加指定字符:S.resize(n, 'c')
*capacity():返回string的容量大小,容量不同于size,如果元素的数量超过了容量string会重新分配内存,以存储更多的元素,而且会造成一些影响。
*reserve(n):重置string的容量,如果n小于容量,则不会造成影响,这个函数也没啥用。
*shrink_to_fit():缩小容量至其等于size。
元素:
S[n]:跟数组一个道理。
back():返回最后一个元素的引用(意味着可以用V.back() = data修改末尾元素值)
front():返回第一个元素的引用
at(n):返回第n个元素的引用
修改操作:
insert():插入字符串
string str="to be question";
string str2="the ";
string str3="or not to be";
//插入方法和插入之后的结果
//插入于第六个字符处
str.insert(6,str2); // to be (the )question
//把str3的第3个字符向后数4个字符插入
str.insert(6,str3,3,4); // to be (not )the question
//插入8个字符
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,3,':'); // to be not to be(:::) that is the question
erase(n):删除第n个字符
erase(a, b):删除[a, b]内所有字符
replace():替换字符串
string base="this is a test string.";
string str2="n example";
string str3="sample phrase";
string str4="useful.";
string str=base; // "this is a test string."
//从str的第9个往后数5个的字符串被替换
str.replace(9,5,str2); // "this is an example string." (1)
//同理,把str3的第7个后数6个字符用来替换
str.replace(19,6,str3,7,6); // "this is an example phrase." (2)
str.replace(8,10,"just a"); // "this is just a phrase." (3)
str.replace(8,6,"a shorty",7); // "this is a short phrase." (4)
//重复三次
str.replace(22,1,3,'!'); // "this is a short phrase!!!" (5)
operator+=:在字符串后面添加字符串。
append(string):同上,括号内可以是string对象,也可以是一串字符。
operator+:可以把两个字符串相加
string S = "123";
S += "456"; 或 S.append("456")
cout << S << endl;
//output:
123456
push_back(char):添加一个字符到向量的末尾
pop_back():删除末尾字符
S.swap(S1):交换 S 和 S1 两个string
*assign():不太用的上,实在想了解点这里>useless<
字符串操作:(有匹配子串,比较等重要函数)
compare(str):比较两个字符串,相同返回1,不同返回0。参数的设置和replace原理相同!
string str1 ("green apple");
string str2 ("red apple");
1.比较整个字符串
str1.compare(str2)
2.比较第6个字符开始到往后数5个字符(apple)
str1.compare(6,5,"apple")
3.把str2的第4个字符开始到往后数5个字符(也是apple)用来比较
str1.compare(6,5,str2,4,5)
find():在字符串中寻找给定的字符串,返回的是找到的第一个匹配子串首位置,若没找到,返回string::npos(其实是一个无符号整形)
rfind():同find相反,返回的是最后一个匹配子串的位置。
1.从头寻找
unsigned int found = str.find(str2);
if (found!=string::npos)
cout << "first 'needle' found at: " << found << '\n';
2.从第n个字符处开始寻找
found=str.find("needles are small",found+1,6);
if (found!=string::npos)
cout << "second 'needle' found at: " << found << '\n';
可以用此方法替换掉第一个needles
str.replace(str.find(str2),str2.length(),"preposition");
substr():裁剪字符串,返回裁剪下来的内容。对被裁剪的字符串不产生影响。
substr(a, b)裁剪下a - b的字符; substr(n)裁剪下n之后的字符
copy():与substr不同,它是用把string上的字符拷贝到char[]里。(注意,参数的顺序跟上面那些函数有点不同)
char buffer[20];
string str ("Test string...");
从第5个字符开始,拷贝6个字符到buffer中
size_t length = str.copy(buffer,6,5);
buffer[length] = '\0';
scout << "buffer contains: " << buffer << '\n';
//out put:
string
find_first_of(str):在字符串中搜索与参数中指定的任何字符匹配的第一个字符。(比如str = "123",它就返回1,2 或者 3这三个字符之一在字符串里面最先出现的地方,例:"a2aa",返回1;"abc321ad" 返回3)
find_last_of(str):同理,不过是从后面开始找
find_first_not_of(str);这时候是找不在str里面的字符的第一个位置了
find_last_not_of(str):同理
string类还有关于迭代的函数,这里不进行讲解。