Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想读取一个字符串,该字符串可以包含空格。
我试过了:
char* str; cin >> str; // but I have to allocate a memory by making new
所以我想:
string str; cin >> str;
但它会读到第一个空格。
有人可以知道我怎样才能得到它吗?也许getline?
getline
我只能使用iostream和的包含string。
iostream
string
你的怀疑是正确的。
string str; std::getline( cin, str );
这将一直读取,直到检测到换行符,而不仅仅是任何空白字符。