空格隔开的字符串自动拼接
string s = "123" "234";
cout << s << endl;
cin.getline()和cin.get()
cin,get不会丢掉换行符
getline会丢掉
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
char s1[20], s2[20];
// 自动扔掉缓冲区里的换行
cin.getline(s1, 20).getline(s2,20);
cout << s1 << endl << s2 << endl;
//缓冲区里的换行符依旧存在,利用返回的cin对象再次get()吃掉单个字符
cin.get(s1,20).get();
cin.get(s2,20).get();
cout << s1 << endl << s2 << endl;
}
string的一些小细节


C++ 11 的原始字符串

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
cout << R"(Jim "King" Tutt uses "\n" instead of endl.)" << '\n';
//等价于
cout << "Jim \"King\" Tutt uses \" \\n\" instead of endl." << '\n';
}

C++ 动态开空间
int size;
cin >> size;
int * pz = new int [size]; // dynamic binding, size set at run time
delete [] pz; // free memory when finished
指针和字符串
用引号引起来的字符串和数组名一样,也是第一个字符的地址
cout << "asdf"[0] << endl;
语法成立,输出字符a
本文介绍了C++中如何进行字符串拼接,包括使用getline函数处理输入时的换行问题,C++11引入的原始字符串字面量,以及动态内存分配和释放。同时,文章提到了指针与字符串的关系,强调了字符串在内存中的表示方式。
3635

被折叠的 条评论
为什么被折叠?



