一、C风格—>C++风格
char c[] = "hello world";
string s(c);
cout << s << endl;
//hello world
二、C++风格—>C风格
int i;
string s = "hello world";
const char *p = s.c_str();
//const不能省去!
for (i = 0; i < s.length(); i++) {
cout << p[i];
//hello world
}
三、C和C++在终端获取一整行
字符串
C++中:
string s;
getline(cin, s);
getline会读取一整行字符串(包括空格),而如果使用: cin >> s; 那么遇到空格将停止读取。
C中:
char c[100];
gets(c);
gets也会读取空格。利用puts输出即可。而如果使用: scanf(“%s”, c); 也是遇到空格停止读取。
结语:文章有错误或不足,欢迎评论,希望轻拍,一起进步哦!
END!
最后
以上就是有魅力电灯胆最近收集整理的关于C风格字符串和C++风格字符串转换的全部内容,更多相关C风格字符串和C++风格字符串转换内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复