
string str();
string str("hello")
string str("hello word",5);
- string str("hello");
- string str2(str)
- string str("hello");
- string str1(str,3,2); ///lo
string (size_t n, char c); 初始化string对象10个c字符
- string str(10,'x');
- string str(10,42);42的ascll是'*';
- #include
- #include
- #include
- using namespace std;
- int main()
- {
- string str("hello word");
- string str1(str);
- string str2(str,5,2);
- string str3("hello word",5);
- string str4(10,42);
- cout<<"str:"<
"\nstr1:"<"\nstr2:"<"\nstr3:"<"\nstr4:"< -
- return 0;
- }

2. string::append
在当前字符串末尾附加字符串

用法基本都差不多
这里就直接上代码了
- #include
- #include
- using namespace std;
- int main()
- {
- string str("hello");
- str.append("ZZZZ");
- str.append("BBBBBBBB",2,4);
- str.append("CCCC");
- str.append("GGGGGGGGG",4);
- str.append(4,'H');
- cout<
- return 0;
- }
3.string::assgin
为字符串指定一个新的值,替换当前内容

- #include
- using namespace std;
- int main()
- {
- string str("hello");
- str.assign("AAAA");
-
- cout<
- str.assign("BBBBBBBB",2,4);
- cout<
- str.assign("XXXXXXXX");
- cout<
- str.assign("GGGGGGGG",4);
- cout<
- str.assign(4,'K');
- cout<
- return 0;
- }

4.string::at
返回string中pos位置的字符

5. string::back
返回最后一个字符

6. string::begin(如果是rbegin表示指向字符串最后一个字符)
返回指向第一个字符的迭代器

7.string::end(如果是rend表示指向第一个字符)
指向结尾字符后面的位置

- #include
- using namespace std;
- int main()
- {
- string str("hello word");
- for(auto it=str.begin();it!=str.end();it++)//返回值是迭代器类型所以用auto自动推>导
- {
- printf("%c",*it);
- }
- return 0;
- }
8. string::c_str
获取c字符串

- #include
- #include
- using namespace std;
- int main()
- {
- string str("hello word bit");
- char* cstr=new char[str.length()+1];
- strcpy(cstr,str.c_str());
- cout<
- return 0;
- }
9.string::clear
删除字符串变成空

10.string::copy
将字符串对象当前值的子字符串复制到由s指向的数组中。该子字符串包含从位置pos开始的len字符。

- #include
- using namespace std;
- int main()
- {
- string str("hello word");
- char buf[20];
- size_t length=str.copy(buf,4,6);
- buf[length]='\0';
- cout<
-
- return 0;
- }
11.string ::erase
删除元素

- #include
- using namespace std;
- int main()
- {
- string str("this ccc an bbbb apple");
- cout<
- str.erase(5,4);
- cout<
- str.erase(str.begin()+8);//删除第8个字符
- cout<
- str.erase(str.begin()+8,str.end()-6);
- cout<
- return 0;
- }

12string::find

return:
成功:第一个匹配的第一个字符的位置。
失败:函数返回string::npos。
- #include
- using namespace std;
- int main()
- {
- string str("The journey has not been made has in vain");
- string str2("has");
- size_t found=str.find(str2);
- if(found!=string::npos)
- {
- cout<<"first 'has' found at:"<
- }
- found=str.find("has",found+1,3);
- if(found!=string::npos)
- {
- cout<<"second 'has' found at:"<
- }
- found=str.find("not");
- if(found!=string::npos)
- {
- str.replace(found,str2.length(),"DDDDD");
-
- }
- cout<
- return 0;
- }
13.string::find_frist_not_of
在字符串中搜索与参数中指定的任何字符都不匹配的第一个字符。

- #include
- using namespace std;
- int main()
- {
- string str("hello word");
- string str1("has");
- size_t found= str.find_first_not_of(str1,0);
- cout<
- found= str.find_first_not_of("has",0);
- cout<
- found=str.find_first_not_of("has",2,3);
- cout<
- found=str.find_first_not_of('l',0);
- cout<
-
- return 0;
- }
13.string::find_last_not_of
在字符串中搜索与参数中指定的任何字符都不匹配的最后一个字符。

14.string::insert

- #include
- using namespace std;
- int main()
- {
- string str("hello word");
- str.insert(0,"BBB");
- cout<
- str.insert(0,"AAACCCDDD",3,3);
- cout<
- str.insert(0,"GGGGGGG",3);
- cout<
- str.insert(0,3,'E');
- cout<
- str.insert(str.begin(),'Q');
-
-
相关阅读:
MySQL优化01-索引
搭建CNFS文件系统
做过启动盘的U盘怎么复原?三种方法教你
华为L3VPNv4 over SRv6 BE配置案例
Qml-跨窗口拖动图片、物体
PwnTheBox 刷题记录crypto篇
综述向:强化学习方法梳理(持续更新)
死锁的Demo,产生条件
官网下载mysql 8.0.27及安装
黑苹果安装常见问题汇总
-
原文地址:https://blog.csdn.net/weixin_58389786/article/details/126404825