请问有哪些常用的STL string函数需要整理?

2026-05-22 05:431阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计860个文字,预计阅读时间需要4分钟。

请问有哪些常用的STL string函数需要整理?

1. 长度:s.length(), s.size()

2.比较:直接使用四则运算符

3.复制,连接:=, +

4.是否为空:s.empty()

5.求子串:s.substr(pos=0, pos!=0.size(int pos=0, int n=npos)) 返回pos开始的n个字符组成的字符串

1.长度:s.length(),s.size();

2.比较:直接用四则运算符即可

3.复制,连接:=,+;

4.是否空:s.empty();

5.求子串:s.substr(pos=0,pos!=0.size(int pos=0,int n=npos)返回pos开始的n个字符组成的字符串

1 int main (){ 2 string s = "hello, world!"; 3 string ss1 = s.substr(2); //llo, world! 4 string ss2 = s.substr(2,3); //llo 5 cout << ss1 << endl << ss2 << endl; 6 }

6.删除字符:s.erase(int pos=0,int n=npos)删除pos开始的n个字符,返回修改后的字符串,同时源字符串也被修改

1 int main (){ 2 string s = "hello,world!"; 3 string ss1 = s.erase(6); //删除下标为6的字符开始的所有字符,hello, 4 string ss2 = s.erase(1,2); //删除下标为2的字符开始的2个字符,删除el 5 cout << s << endl << ss1 << endl << ss2 << endl; 6 }

7.s.insert()插入字符串

1 string s=","; 2 s.insert(0,"heo"); cout<<s<<endl;//整个字符串 3 s.insert(4,"world",2); cout<<s<<endl;//world的前2个字符 4 s.insert(2,2,'l');cout<<s<<endl;//插入单个字符2次 5 s.insert(s.end(),'r');cout<<s<<endl;//使用迭代器 6 s += "ld!";cout<<s<<endl;//在开头或者末尾插入最好还是运算符 7

8.s.replace()字符串替代

用str替代指定字符串从起始位置pos开始长度为len的字符

#include<iostream> #include<string> using namespace std; int main() { string str = "abcdefghigk"; str=str.replace(str.find("c"),2,"#"); //从第一个a位置开始的两个字符替换成# cout<<str<<endl; return 0; }

9.s.substr()字符串替代

用substr的指定字串(给定起始位置和长度)替换从指定位置上的字符串

#include<iostream> #include<string> using namespace std; int main() { string str = "he is@ a@ good boy"; string substr = "12345"; str=str.replace(0,5,substr,substr.find("1"),4); //用substr的指定字符串替换str指定字符串 cout << str << endl; return 0; }

10.s.find()字符串查找

s.find()用来找出字符在字符串中的位置

s.find(str,position)

str表示要查找的元素

position表示字符串的某个位置,表示从这个位置开始的字符串中找指定元素

这个可以不填,那就默认从字符串的开始进行查找了

返回值是目标字符的位置,没有找到目标元素就返回string::npos。(或-1)

1 #include<iostream> 2 #include<bits/stdc++.h> 3 using namespace std; 4 typedef long long ll; 5 int main(){ 6 int a[10]={1,2,3,4,5,6}; 7 string s="abcdefg"; 8 cout << find(a,a+10,3)-a << endl; 9 cout << s.find('e') << endl; 10 cout << s.find("bcd") << endl; 11 cout << s.find('e',4) << endl;//从下标为4开始搜索,输出-1; 12 return 0; 13 }

11.字符串的反转函数s.reverse()

请问有哪些常用的STL string函数需要整理?

#include<iostream> #include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ char a[8]={"abcdefg"}; string s="abcdefg"; cout << "string reverse用法:reverse(s.begin(),s.end());" << endl; reverse(s.begin(),s.end()); cout << s << endl; cout << "字符串数组 reverse用法:reverse(a,a+10);" << endl; reverse(a,a+7); for(int i=0;i<7;i++){ cout << a[i]; } return 0; }

----------------------------------先更到这,回宿舍在更

本文共计860个文字,预计阅读时间需要4分钟。

请问有哪些常用的STL string函数需要整理?

1. 长度:s.length(), s.size()

2.比较:直接使用四则运算符

3.复制,连接:=, +

4.是否为空:s.empty()

5.求子串:s.substr(pos=0, pos!=0.size(int pos=0, int n=npos)) 返回pos开始的n个字符组成的字符串

1.长度:s.length(),s.size();

2.比较:直接用四则运算符即可

3.复制,连接:=,+;

4.是否空:s.empty();

5.求子串:s.substr(pos=0,pos!=0.size(int pos=0,int n=npos)返回pos开始的n个字符组成的字符串

1 int main (){ 2 string s = "hello, world!"; 3 string ss1 = s.substr(2); //llo, world! 4 string ss2 = s.substr(2,3); //llo 5 cout << ss1 << endl << ss2 << endl; 6 }

6.删除字符:s.erase(int pos=0,int n=npos)删除pos开始的n个字符,返回修改后的字符串,同时源字符串也被修改

1 int main (){ 2 string s = "hello,world!"; 3 string ss1 = s.erase(6); //删除下标为6的字符开始的所有字符,hello, 4 string ss2 = s.erase(1,2); //删除下标为2的字符开始的2个字符,删除el 5 cout << s << endl << ss1 << endl << ss2 << endl; 6 }

7.s.insert()插入字符串

1 string s=","; 2 s.insert(0,"heo"); cout<<s<<endl;//整个字符串 3 s.insert(4,"world",2); cout<<s<<endl;//world的前2个字符 4 s.insert(2,2,'l');cout<<s<<endl;//插入单个字符2次 5 s.insert(s.end(),'r');cout<<s<<endl;//使用迭代器 6 s += "ld!";cout<<s<<endl;//在开头或者末尾插入最好还是运算符 7

8.s.replace()字符串替代

用str替代指定字符串从起始位置pos开始长度为len的字符

#include<iostream> #include<string> using namespace std; int main() { string str = "abcdefghigk"; str=str.replace(str.find("c"),2,"#"); //从第一个a位置开始的两个字符替换成# cout<<str<<endl; return 0; }

9.s.substr()字符串替代

用substr的指定字串(给定起始位置和长度)替换从指定位置上的字符串

#include<iostream> #include<string> using namespace std; int main() { string str = "he is@ a@ good boy"; string substr = "12345"; str=str.replace(0,5,substr,substr.find("1"),4); //用substr的指定字符串替换str指定字符串 cout << str << endl; return 0; }

10.s.find()字符串查找

s.find()用来找出字符在字符串中的位置

s.find(str,position)

str表示要查找的元素

position表示字符串的某个位置,表示从这个位置开始的字符串中找指定元素

这个可以不填,那就默认从字符串的开始进行查找了

返回值是目标字符的位置,没有找到目标元素就返回string::npos。(或-1)

1 #include<iostream> 2 #include<bits/stdc++.h> 3 using namespace std; 4 typedef long long ll; 5 int main(){ 6 int a[10]={1,2,3,4,5,6}; 7 string s="abcdefg"; 8 cout << find(a,a+10,3)-a << endl; 9 cout << s.find('e') << endl; 10 cout << s.find("bcd") << endl; 11 cout << s.find('e',4) << endl;//从下标为4开始搜索,输出-1; 12 return 0; 13 }

11.字符串的反转函数s.reverse()

请问有哪些常用的STL string函数需要整理?

#include<iostream> #include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ char a[8]={"abcdefg"}; string s="abcdefg"; cout << "string reverse用法:reverse(s.begin(),s.end());" << endl; reverse(s.begin(),s.end()); cout << s << endl; cout << "字符串数组 reverse用法:reverse(a,a+10);" << endl; reverse(a,a+7); for(int i=0;i<7;i++){ cout << a[i]; } return 0; }

----------------------------------先更到这,回宿舍在更