常见用法:
int main()
{
string s1("hello world");
string s2("gm");
s1.insert(5,"x");
cout << s1 << endl;
s1.insert(6,s1,0);
cout << s1 << endl;
s1.insert(0,"x"); //
cout << s1 << endl;
s1.insert(0,1, 'x');
cout << s1 << endl;
s1.erase(5,4);
cout << s1 << endl;
s1.erase(5);
cout << s1 << endl;
return 0;
}
配合使用:
int main()
{
string s1("hello world hello world");
size_t pos = s1.find(' ',0);
while (pos != string::npos)
{
s1.replace(pos,1,"%20");
pos = s1.find(' ',pos+3);
}
cout << s1 << endl;
string s2("hello world hello world");
size_t pos1 = s2.find(' ', 0);
while (pos1 != string::npos)
{
s2.replace(pos1, 2, "%20");
pos1 = s2.find(' ', pos + 3);
}
cout << s2 << endl;
return 0;
}
int main()
{
string filename("test.cpp");
FILE* fout = fopen(filename.c_str(), "r");
char ch = fgetc(fout);
while (ch != EOF)
{
cout << ch;
ch = fgetc(fout);
}
cout << endl;
return 0;
}
原文地址:https://blog.csdn.net/kqs__/article/details/134563328
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_6027.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!