服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++中 string 中的常用方法使用心得

C++中 string 中的常用方法使用心得

2021-08-29 15:25maoqifan C/C++

这篇文章主要介绍了C++中 string 中的常用方法使用心得,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

string 字符串在所有的语言中都非常重要,c++也不例外,接下来我们将介绍string中的常用方法

1. size() 和 length() 函数 : 他们返回字符串的真实长度,且不会因为空格而截断,这两个方法完全等价,使用及输出如下:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string s = "dasddasd";
  8. printf("size()返回的长度为:%lu\nlength()返回的长度为:%lu",s.size(),s.length());
  9. return 0;
  10. }

C++中 string 中的常用方法使用心得

2. find()函数和rfind()函数 : 这两个函数用于查找字串在母串中的位置,并且返回该位置,当然如果找不到就会返回一个特别的标记string::nops,而find()函数是从字符串开始指针向后进行查找,rfind()函数是从字符串的结束指针开始向前查找,其使用及输出如下:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string s = "hello worldh";
  8. int index = s.find("h"); // 从串首向后查找
  9. int index2 = s.find("h",2) // 固定位置后子串在母串的位置
  10. int index1 = s.rfind("h"); // 从串尾向前查找
  11. printf("(find()):字母h在母串中的位置为:%d\n", index);
  12. printf("(rfind()):字母h在母串中的位置为:%d", index1);
  13. return 0;
  14. }

C++中 string 中的常用方法使用心得

值得注意的是我们可以通过组合使用这两个函数来实现判断该子串是否唯一存在于母串中,其实现代码如下:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. inline bool whetherOnly(string &str,string &base){
  6. return base.find(str) == base.rfind(str);
  7. }

3. find_last_of()函数和find_first_of()函数:从函数名我们也可以知道find_last_of()函数是找这个子串在母串中最后一次出现的位置并且将该位置返回;而find_first_of()函数是找这个子串在母串中最后一次出现的位置并将该位置返回,其使用及输出如下:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string s = "hello worldh";
  8.  
  9. int index = s.find_first_of("h");
  10. int index1 = s.find_last_of("h");
  11. printf("(find_first_of()):字母h在母串中的位置为:%d\n", index);
  12. printf("(find_last_of()):字母h在母串中的位置为:%d", index1);
  13. }

C++中 string 中的常用方法使用心得

4.assign()函数:该函数用于将目标串的值复制到该串上,并且只复制值,其使用及输出如下:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string s = "hello worldh";
  8. s.clear();
  9. s.assign("hello world");
  10. cout<<s<<endl;
  11.  
  12. }

C++中 string 中的常用方法使用心得

5.clear()函数,把当前字符串清空,这时候如果调用string::size()函数或string::length()函数将返回0,其使用及输出如下:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string s = "hello worldh";
  8. s.clear();
  9. cout<<"clear后的串的长度"<<s.size()<<endl;
  10. }

C++中 string 中的常用方法使用心得

6.resize()函数,该函数可以将字符串变长到指定长度,若小于原本字符串的长度,则会截断原字符串;这个函数的一个重载形式是str.resize(length,'s') 可以用该输入字符's'来对字符串进行扩充至length的长度,该函数的使用及输出如下:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string s = "hello worldh";
  8. s.resize(5); // s会变为 hello
  9. cout<<s<<endl;
  10. s.resize(10,'C'); // s 会变为 helloCCCCC
  11. cout<<s<<endl;
  12.  
  13. }

C++中 string 中的常用方法使用心得

7. replace(pos,len,dist)函数: 该函数用于将该串从pos位置开始将长度为len的字串替换为dist串,值得注意的是该函数只替换一次,这与市面上的py和java等语言不一样,需要留意,该函数的使用和输出如下:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string s = "hello worldh";
  8. s.replace(s.find("h"),2,"#"); // 把从第一个h开始的两个字符变为一个字符 #
  9. cout<<"替换后的字符串为: "<<s<<endl;
  10.  
  11. }

C++中 string 中的常用方法使用心得

那么既然C++本身不提供,替换所有子串的函数,我们就自己实现一个,其代码如下:

  1. // 替换字符串里的所有指定字符
  2. string replace(string &base, string src, string dst) //base为原字符串,src为被替换的子串,dst为新的子串
  3. {
  4. int pos = 0, srclen = src.size(), dstlen = dst.size();
  5. while ((pos = base.find(src, pos)) != string::npos)
  6. {
  7. base.replace(pos, srclen, dst);
  8. pos += dstlen;
  9. }
  10. return base;
  11. }

8. erase(index,length)函数:该函数删除index位置后length长度的子串,其代码及输出如下:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string s = "hello worldh";
  8. s.erase(s.find("h"),3);
  9. cout<<"擦除过后的串"<<s<<endl; // 将会输出lo worldh
  10. }

C++中 string 中的常用方法使用心得

9.substr(index,length)函数:该函数从index开始截断到长度为length并返回截断的子串;值得注意的是,该函数不改变母串的值,其使用及输出如下:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. s = s.substr(0,5);
  8. cout<<"截断并赋值后的字符串为:"<<s<<endl; // 会输出hello
  9. }

C++中 string 中的常用方法使用心得

10 . push_back(char c)函数,pop_back()函数,append(string s)函数:push_back(char c)函数往该字符串的尾端加入一个字符;pop_back()函数从该字符串的尾端弹出一个字符;而apend(string s)函数将会在该字符串的末尾添加一个字符串,并且返回添加后字符串的引用。他们的使用及输出如下图所示:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string s = "hello worldh";
  8. // s.erase(s.find("h"),3);
  9. s.pop_back(); //弹出串的最后一个元素
  10. cout<<"弹出串尾元素后的字符串为: "<<s<<endl;
  11. s.push_back('s'); // 在串的最后添加一个字符
  12. cout<<"往串尾添加字符后的字符串为: "<<s<<endl;
  13. s.append("hhh"); // 在串的最后添加一个字符串
  14. cout<<"往串尾添加字符串后的字符串为: "<<s<<endl;
  15. }

C++中 string 中的常用方法使用心得

以上就是string中比较重要的函数的全部内容了,既然我们学完了该内容,那我们接下来做一条题来熟悉一下这些函数中的一些吧(题目与代码如下代码块,题目出自leetcode):

  1. // 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。
  2. // 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。
  3.  
  4. // 注意:每次拼写时,chars 中的每个字母都只能用一次。
  5. // 返回词汇表 words 中你掌握的所有单词的 长度之和。
  6.  
  7. // 输入:words = ["cat","bt","hat","tree"], chars = "atach"
  8. // 输出:6
  9. // 解释:
  10. // 可以形成字符串 "cat" 和 "hat",所以答案是 3 + 3 = 6。
  11.  
  12. // 输入:words = ["hello","world","leetcode"], chars = "welldonehoneyr"
  13. // 输出:10
  14. // 解释:
  15. // 可以形成字符串 "hello" 和 "world",所以答案是 5 + 5 = 10。
  16.  
  17. #include <iostream>
  18. #include <vector>
  19. #include <string>
  20. using namespace std;
  21. class Solution
  22. {
  23. public:
  24. int countCharacters(vector<string> &words, string chars)
  25. {
  26. int count = 0;
  27. bool flag = false; // 标记
  28. string c_chars(chars); // 构造c_chars保存chars
  29. for (int i = 0; i < words.size(); i++) // 迭代单词表
  30. {
  31. if (c_chars.size() < words[i].size()) //如果单词的字母多于可选字母,则跳过这个单词
  32. continue;
  33. for (int j = 0; j < words[i].size(); j++) // 迭代可选择的字母
  34. {
  35. int index = c_chars.find(words[i][j]);
  36. if (index != c_chars.npos) // 能找到这个字母
  37. {
  38. flag = true;
  39. c_chars.erase(index, 1); // 从c_chars()删除这个字母
  40. }
  41. else
  42. {
  43. flag = false; // 不能找到,意味着不能组成这个单词
  44. break; //跳出这次循环
  45. }
  46. }
  47. if (flag) // 如果符合则计数加1
  48. count += words[i].size();
  49. c_chars.assign(chars); // 把chars的值在再次赋值给c_chars
  50. }
  51. return count;
  52. }
  53. };

最后感谢大家的阅读,string中这些的函数组合起来可以说是威力无穷,所以还是要好好掌握的。

总结

到此这篇关于C++中 string 中的方法的使用详解(心得)的文章就介绍到这了,更多相关C++ string方法使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/maoqifansBlog/archive/2020/03/25/12571138.html

延伸 · 阅读

精彩推荐