C++ 如何在不知道有多少元素的情况下阅读一行文本中的每个元素?(C+;+;)
我在别处找不到我问题的答案,所以我想我应该提出一个新问题。我有一个文本文件,每一行都是一系列的数字。我需要把每一行加起来。但我不知道每条线上有多少个元素。比如: 1231 3 2 1 2 2 3 1 5 2 1 应输出: 第1行:10 第2行:8 第3行:14C++ 如何在不知道有多少元素的情况下阅读一行文本中的每个元素?(C+;+;),c++,readline,C++,Readline,我在别处找不到我问题的答案,所以我想我应该提出一个新问题。我有一个文本文件,每一行都是一系列的数字。我需要把每一行加起来。但我不知道每条线上有多少个元素。比如: 1231 3 2 1 2 2 3 1 5 2 1 应输出: 第1行:10 第2行:8 第3行:14 我知道当每行的数字数量已知时,该怎么做,但似乎无法解决其他问题。感谢您的帮助。让文件成为一个std::ifstream对象,即可使用: std::string line; while (std::getline(file, line))
我知道当每行的数字数量已知时,该怎么做,但似乎无法解决其他问题。感谢您的帮助。让
文件成为一个std::ifstream
对象,即可使用:
std::string line;
while (std::getline(file, line)) { // for each line
std::istringstream iss(line);
int i;
while (iss >> i) { // for each int in line
// use i
}
}
如果愿意的话,可以将整数存储在数组中。它将在需要时动态增长。让文件成为一个std::ifstream
对象,即可使用:
std::string line;
while (std::getline(file, line)) { // for each line
std::istringstream iss(line);
int i;
while (iss >> i) { // for each int in line
// use i
}
}
如果愿意的话,可以将整数存储在数组中。它将在需要时动态增长。将每一行存储为stringstream,然后使用它提取每个元素,您将知道它何时为空,因为STL定义了一个名为empty
的成员方法,在这种情况下返回true 获取每一行并将其存储为stringstream,然后使用它提取每个元素,您将知道它何时为空,因为STL定义了一个名为empty
的成员方法,在这种情况下返回true 典型的方法是使用std::getline()
读取一行,然后解码该行。这有点无聊,因为它需要另一条流。这是可以不做的
不首先读取行的最简单方法可能是创建一个跳过空白但在检测到换行时失败的操纵器('\n'
)。然后使用操纵器,读取int
,如果成功,则处理该值。例如:
std::istream& skip_non_newline(std::istream& in) {
for (int c; std::isspace(c = in.peek()); ) {
if (c == '\n') {
in.setstate(std::ios_base::failbit);
}
else {
in.ignore();
}
}
}
// ....
while (in) {
int total = 0, value = 0;
while (in >> skip_non_newline >> value) {
tota += value;
}
if (!in.eof()) {
in.clear(); // failure was due to a format error or a newline being hit...
if (in.peek() != '\n') {
in.setstate(std::ios_base::failbit); // OK, not due to a newline...
}
}
}
典型的方法是使用std::getline()
读取一行,然后解码该行。这有点无聊,因为它需要另一条流。这是可以不做的
不首先读取行的最简单方法可能是创建一个跳过空白但在检测到换行时失败的操纵器('\n'
)。然后使用操纵器,读取int
,如果成功,则处理该值。例如:
std::istream& skip_non_newline(std::istream& in) {
for (int c; std::isspace(c = in.peek()); ) {
if (c == '\n') {
in.setstate(std::ios_base::failbit);
}
else {
in.ignore();
}
}
}
// ....
while (in) {
int total = 0, value = 0;
while (in >> skip_non_newline >> value) {
tota += value;
}
if (!in.eof()) {
in.clear(); // failure was due to a format error or a newline being hit...
if (in.peek() != '\n') {
in.setstate(std::ios_base::failbit); // OK, not due to a newline...
}
}
}
如果您只需要输出每行中的数字之和,那么您可以按照以下方式进行
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <numeric>
#include <iterator>
//...
std::string line;
size_t n = 0;
while ( std::getline( file, line ) )
{
std::istringstream is( line );
std::cout << "Line " << ++n << ": "
<< std::accumulate( std::istream_iterator<int>( is ),
std::istream_iterator<int>(), 0 )
<< std::endl;
}
#包括
#包括
#包括
#包括
#包括
#包括
//...
std::字符串行;
尺寸n=0;
while(std::getline(文件,行))
{
std::istringstream是(行);
std::cout如果您只需要输出每行中的数字之和,那么您可以通过以下方式完成
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <numeric>
#include <iterator>
//...
std::string line;
size_t n = 0;
while ( std::getline( file, line ) )
{
std::istringstream is( line );
std::cout << "Line " << ++n << ": "
<< std::accumulate( std::istream_iterator<int>( is ),
std::istream_iterator<int>(), 0 )
<< std::endl;
}
#包括
#包括
#包括
#包括
#包括
#包括
//...
std::字符串行;
尺寸n=0;
while(std::getline(文件,行))
{
std::istringstream是(行);
你能解释为什么会有输出“第1:13行”吗?糟糕的数学,我编辑了它。谢谢你指出它。你能解释为什么会有输出“第1:13行”吗?糟糕的数学,我编辑了它。谢谢你指出。如果一行上有'\n'
以外的尾随空格,这行行吗?@jrok:代码未经测试,但我认为如果我使用了循环,它会行得通……我会纠正错误。如果一行上有'\n'
以外的尾随空格,这行行吗?@jrok:代码未运行已测试,但我认为如果我使用了循环,它会起作用…我将更正错误。谢谢你,这是有效的。有一个问题,你如何添加ints以外的内容?我尝试了Double,但它会将0打印到控制台。(我知道它不是0)@user3426380如果你想用int代替double,那么在算法调用中使用std::istream_迭代器,而不是0.0。谢谢你,这是有效的。一个问题,你如何添加int以外的内容?我尝试过double,但它会将0打印到控制台。(我知道它不是0)@user3426380如果您想用int替换double,那么在算法调用中使用std::istream_迭代器,而不是0.0。