赶紧的,先发一篇,最基本的string C++

来源:互联网 发布:密立根油滴实验数据 编辑:程序博客网 时间:2024/05/16 10:08




1.       搜索字符串

继续昨天的搜索字符串的学习,还可以搜索非空字符串、C样式字符串中包含的对应于指定字符个数的子字符串,比如将昨日搜索字符串的代码改动一下:

cout << sentence.find(“akat”, 1, 2) <<endl;   //输出为9

其中find()函数的第一个参数是非空字符串,第二个参数是开始搜索的索引位置,第三个参数是非空字符串中药提取作为要查找的字符串的字符个数。

即,在字符串”Manners maketh man”中从索引位置为1的位置开始搜索字符串”akat”中的”ak”字符串,输出得到搜索到的位置为9

 

搜索had字符串,并计算出现字数。

其中这个搜索方式和以上的搜索不同,搜索从index=0的位置开始,在index=19的位置上找到第一个word,并把找到的word的索引位置存储在index中,再执行for循环中的第三个控制表达式(两个操作,一个是索引位置递增index += substr.length()——跳过搜索到字符串的长度,另一个是word出现次数+1),然后循环继续,在index+= substr.length()索引位置上继续搜索,如果存储在index中的值是string::npos,就表示没有找到word,循环就停止。

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <string>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     //转义字符。。。  
  9.     string text = "Smith, where Jones had had \"had had\", had had \"had\".""\n \"Had had\" had had the examiners' approval.";  
  10.     string word = "had";  
  11.   
  12.     cout << endl << "The string is: " << endl << text << endl;  
  13.   
  14.     //比较复杂的for循环语句,语句中既进行了非法值的判断index = text.find(word,index)) != string::npos,又对应有字符串个数进行了计数index += word.length(),count++   
  15.     int count = 0;  
  16.     for(int index = 0; (index = text.find(word,index)) != string::npos; index += word.length(),count++);  
  17.   
  18.     cout << "Your text contained "  
  19.          << count << " occurrences of \""  
  20.          << word << "\"."  
  21.          << endl;  
  22.     return 0;  
  23. }  

find_first_of()函数与find_last_of()函数

find_first_of()函数作用从string对象的开头开始搜索,查找给定字符集合中第一次出现的位置,可以用于搜索字符集和中的字符,以下代码输出为5

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <string>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     string text = "Smith, where Jones had had \"had had\", had had \"had\".""\n \"Had had\" had had the examiners' approval.";  
  9.     string separators = ",.\"";  
  10.   
  11.     cout << text.find_first_of(separators)  
  12.          << endl;  
  13.   
  14.     return 0;  
  15. }  

也可以把参数定义为非空字符串,以下代码输出为2因为第一个元音是i,其索引位置为2。可以用于检测元音字母。

cout << text.find_first_not_of(“AaEeIiOoUu”);       //输出为 2

    << endl;

 

还可以使用find_last_of()函数从string对象的结尾开始,进行逆向搜索,以查找集合中的字符最后一次出现的位置

cout << text.find_first_of(“AaEeIiOoUu”);       //输出为 92

    << endl;

 

另一个选项是查找不在字符集和中的字符,这可以使用find_first_not_of()函数和find_last_not_of函数。以下代码,因为第一个字符不是元音,所以结果就是该字符,其索引值是0。

cout << text.find_first_not_of(“AaEeIiOoUu”);       //输出为 0

    << endl;



逆向搜索字符串

rfind()函数,以下搜索语句都查找rfind()函数中的参数最后一次出现的位置,返回它找到的第一个字符的位置。即,找到参数所在字符串最后一个位置并返回。

[cpp] view plaincopy
  1. string sentence = "Manners maketh man";  
  2. string word = "an";  
  3. cout << sentence.rfind(word)      << endl;  //输出16  
  4. cout << sentence.rfind("man") << endl;  //输出15  
  5. cout << sentence.rfind('e')       << endl;  //输出11  

同样也可以添加参数,指定从后向前搜索的起始位置;当第一个参数是C样式的字符串时,还可以添加第三个参数,指定从C样式字符串中提取的字符串个数,作为要搜索的子字符串


2.       修改字符串

 

插入字符串insert()函数

以下代码words字符串插入到phrase中索引位置为14的字符前面,输出就为“We can insert a string into a string.”

[cpp] view plaincopy
  1. string phrase = "We can insert a string.";  
  2. string word = "a string into ";  
  3. phrase.insert(14,words);  

当然也可以吧非空字符串插入到string对象中

phrase.insert(14,”a string into ”);

 

更复杂一点的是,把一个string对象的子字符串插入到string类型的对象中。在insert()函数调用中提供另外两个参数。一个参数指定子字符串中第一个字符的索引位置,另一个参数指定子字符串的字符个数。

phrase.insert(13, words, 8, 5);

这行代码的insert函数后两个参数就是把words中从第8个位置开始的5个字符组成的子字符串插入到phrase中。

 

把非空字符串中指定书目的字符插入到string对象中也有类似的效果。

phrase.insert(13, “into something”, 5);

这行代码把“into something”中的前5个字符组成的子字符串插入到phrase中的第13索引位置的字符之前。

 

如果需要把包含几个想吐字符的字符串插入到string对象中,可以用:

phrase.insert(16, 7, ‘*’);

这个语句将7个星号插入到phrase的第16索引位置的字符之前。即,“We can insert a *******string.”


替换子字符串

以下代码将名字Jones替换为一个不常见的名字(两个字符串有不同长度也可以替换),其中replace()函数的几个参数,第一个为索引位置,第二个为选定替换的子字符串长度,第三个为替换字符串。

         string text = "Smith, where Jones had had \"had had\", had had \"had\".""\n \"Had had\" had had the examiners' approval.";

         text.replace(13, 5, "Grutfuttock");

 

替换字符串可以是string对象或非空字符串,以下代码查找text中”Jones”的第一个字母的位置,并把索引值存储在start中,用find_first_of()函数搜索separators中的分隔符,查找”Jones”最后一个字符后面的字符。然后将索引值存储在end,然后两者相减得到字符串的长度。

[cpp] view plaincopy
  1.     string text = "Smith, where Jones had had \"had had\", had had \"had\".""\n \"Had had\" had had the examiners' approval.";  
  2. string separators=" ,.\" ";  
  3. size_t start = text.find("Jones");  
  4. size_t end = text.find_first_of(separators,start+1);  
  5. text.replace(start, end-start,” Gruntfuttock”);  

同样替换字符串用string 对象表示:

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <string>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     string text = "Smith, where Jones had had \"had had\", had had \"had\".""\n \"Had had\" had had the examiners' approval.";  
  9.     string separators=" ,.\" ";  
  10.     size_t start = text.find("Jones");                  //找到开头  
  11.     size_t end = text.find_first_of(separators,start+1);    //找到结尾  
  12.     text.replace(start, end-start, name, 5, 12);            //开始的位置,结束的位置,插入的字符串name,name的第5个位置的字符开始,一共12个字符。  
  13.     return 0;  
  14. }  

 

删除字符串中的字符

erase()函数

删除从索引值为0开始的6个字符

text.erase(0, 6);

 

通常使用这个函数删除以前搜索出来的某个字符串:

[cpp] view plaincopy
  1. string word = "rose";  
  2. size_t index = text.find(word);  
  3. if (index != string::npos)  
  4.     text.erase(index,word.length());  

chear()函数:删除字符串中的所有字符

text.clear();

 

3.       string类型的数组

使用string类型的对象数组与使用其他类型的数组是一样的。

string words[] = {“this”, “that”, “theother”};

数组words有3个元素,当然也可以显示指定数组的大小

string words[10] = {“this”, “that”, “theother”};

现在,该string类型数组有10个元素,前3个元素用花括号中的字符串字面量初始化,其他的7个是空字符串。

还可以引用string数组元素中的各个字符。

下面的语句中将第3个元素的第7个字符改为t:

words[2][6] = ‘t’;

然后输出:

cout << words[2];

显示为:

the otter

 

4.       宽字符的字符串(不理解)

声明用wstring ,字符串的字面量在双引号中包含wchat_t类型的字符串,添加一个前缀L,把它们与包含char字符的字符串字面量区分开。

输出用wcout流

在wstring对象的操作中,应使用wchat_t类型的字符变量,定义字符和字符串字面量时要加上前缀L。

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <string>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     wstring word = L"rose";  
  9.     wcout << word;  
  10.   
  11.     return 0;  
  12. }