C++ string类不能像C字符串能靠在i位赋值为‘\0’来截断,因为'\0'在C字符串中才具有字符结束符的意义
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main()
{
string s("abcdefg");
s[3] = '\0';
cout << s.size() << endl;
cout << s << endl;
cout << s.c_str() << endl;
char ss[10]={"abcdefg"};
ss[3] = '\0';
cout << ss << endl;
cout << string(ss) << endl;
return 0;
}
得结果为:
7
abcefg
abc
abc
abc
原文链接: https://www.cnblogs.com/jiangu66/archive/2013/04/07/3003817.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
- ’来截断" loading="lazy" src="https://www.ccppcoding.com/wp-content/uploads/2023020407005197.bmp"/>’来截断" loading="lazy" class="j-lazy" src="https://www.ccppcoding.com/wp-content/themes/justnews/themer/assets/images/lazy.png" data-original="https://www.ccppcoding.com/wp-content/uploads/2023020407005197.bmp"/>
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/83571
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!