零星笔记,待整理


//用 getline 函数从输入读取整行内容。然后为了获得每行中的单词,将一个
//istringstream 对象与所读取的行绑定起来,这样只需要使用普通的
//string 输入操作符即可读出每行中的单词。

string line, word;// will hold a line and word from input, rectively
while (getline(cin, line))
{ // read a line from the input into line
// do per-line processing
istringstream stream(line); // bind to stream to the line we read
while (stream >> word)
{ // read a word from line
// do per-word processing
}
}



//stringstream 对象的一个常见用法是,需要在多种数据类型之间实现自动格式化时
//使用该类类型
int val1 = 512, val2 = 1024;
ostringstream format_message;
// ok: converts values to a string representation
format_message << "val1: " << val1 << "\n"
<< "val2: " << val2 << "\n";

//相反,用 istringstream 读 string 对象,即可重新将数值型数据找回来
istringstream input_istring(format_message.str());
string dump; // place to dump the labels from the formatted message
input_istring >> dump >> val1 >> dump >> val2;
cout << val1 << " " << val2 << endl; // prints 512 1024

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值