首页  

c++ 标准输入输出     所属分类 c 浏览量 622
#include "iostream"
#include "string"

int main(){
  std::string text;
  while(true){
    // std::cin >> text;
    getline(std::cin, text);
    std::cout << text << std::endl;
  }
  return 0; 
}

std::cin
读取数据 忽略前面的空白字符(空格 制表符 或 换行符)
从第一个非空格字符开始读取,读到下一个空白字符时,停止读取
如果使用 std::cin >> text;
输入 a b c
输出为
a
b
c

读取一行 使用 getline(std::cin, text);

输入 a b c
输出 a b c

getline 读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中

http://www.cplusplus.com/reference/string/string/getline/

istream& getline (istream&  is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);

Get line from stream into string
Extracts characters from is and stores them into str 
until the delimitation character delim is found or the newline character, '\n'

上一篇     下一篇
c++ rapidjson 使用

大端和小端模式

c++ header-only library

using namespace std 与 using std::cout

c c++ 网络库

Linux Shell 多个命令中的分号(;) && ||