所以我需要像C++多行输入
first line
line with some number like 5
line 3
empty line
什么是从用户获得这样的字符缓冲区,以及如何获得它的长度的正确方法创建一个包含4行文本(EN_US)的字符缓冲区?
所以我需要像C++多行输入
first line
line with some number like 5
line 3
empty line
什么是从用户获得这样的字符缓冲区,以及如何获得它的长度的正确方法创建一个包含4行文本(EN_US)的字符缓冲区?
非但没有这样的一个缓冲的,它可能是更容易使用getline
标准输入读取四行成独立string
秒(如果你喜欢使用一个循环):
那么总的数据长度为总长度为个人string
。也可以使用此方法从用户检索数据,然后将它们连接成四行stringstream
。
组合代码示例:
#include <string>
#include <sstream>
#include <iostream>
std::string s[4];
size_t length(0);
std::ostringstream output;
for (size_t index = 0; index < 4; ++index)
{
getline(std::cin, s[index]);
length += s[index].length();
output << s[index] << std::endl;
}
output.flush();
streamoff streamLength = output.tellp();
不错,但我会去一个'std :: string行[4];对于(...)std :: getline(std :: cin,lines [i]);'使它更容易改变,减少写入的痛苦! – 2010-11-16 23:04:27
@André,同意,我会改变这一点。 – 2010-11-16 23:06:27
这是功课?它很酷,如果它是,但我们想知道。 – 2010-11-16 22:41:14
@John,我不这么认为,最近来自@Kabumbus的很多qs。 – 2010-11-16 22:45:56
你一般都想阅读,直到某种输入终止,还是只是这四条固定线? – Pyjong 2010-11-16 22:49:40