C++ 从迭代器中提取数字
对于这个问题,@HowardHinnant给出了一个精彩的答案,它可以非常高效地将数字从文本文件提取到向量中。以下是简短的代码:C++ 从迭代器中提取数字,c++,iterator,C++,Iterator,对于这个问题,@HowardHinnant给出了一个精彩的答案,它可以非常高效地将数字从文本文件提取到向量中。以下是简短的代码: #include <fstream> #include <iostream> #include <vector> #include <string> #include <sstream> #include <iterator> // g++ -std=c++11 test.cpp -o t i
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
// g++ -std=c++11 test.cpp -o t
int main()
{
std::ifstream theStream("data.txt");
if( ! theStream )
std::cerr << "data.txt\n";
while (true)
{
std::string line;
std::getline(theStream, line);
if (line.empty())
break;
std::istringstream myStream( line );
std::istream_iterator<int> begin(myStream), eof;
std::vector<int> numbers(begin, eof);
// process line however you need
std::copy(numbers.begin(), numbers.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
}
我已经研究这段代码两天了,还不能完全掌握如何从每一行中提取数字。那么,我如何访问,比如说,第二行(103)上的第三个数字,或者更一般地说,第m行上的第n个数字
任何关于这个解决方案如何工作的解释都会很有帮助 使用
向量
中的向量
分别存储每一行号:
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
int main()
{
std::ifstream theStream("data.txt");
if (!theStream)
std::cerr << "data.txt\n";
std::vector<std::vector<int>> data; // vector to hold the numbers of each line seperately
while (true)
{
std::string line;
std::getline(theStream, line);
if (line.empty())
break;
std::istringstream myStream(line);
std::istream_iterator<int> begin(myStream), eof;
std::vector<int> numbers(begin, eof);
// process line however you need
data.push_back(numbers); // add numbers of current line to data
}
std::cout << data[1][2] << '\n'; // 2nd row, 3rd number: 103
}
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
std::ifstream流(“data.txt”);
如果(!流)
代码正是这样做的,从每一行提取数字。我不确定有什么不清楚?std::vectormatrix;
然后用matrix替换std::vector number(begin,eof)
向量编号
在每次迭代时都会重新声明。如果以后想访问每一行,可以将其存储在向量向量中。@维克托古宾请将其填入答案中。如果我读对了,这正是提问者要找的。假设只进行一次查询,你可以通过co一次查询解压时(以及每次while循环迭代时)解开所处的行和列;然后从那里中断。这样您就不必读取整个文件。附录:虽然编译器可能会发现优化,但您应该能够消除在数据处不必要复制的机会。向后推(数字)
使用std::move
emplace\u back
也不是一个坏主意。也就是说,这些调整可能会在读取文件的IO开销中丢失。
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
int main()
{
std::ifstream theStream("data.txt");
if (!theStream)
std::cerr << "data.txt\n";
std::vector<std::vector<int>> data; // vector to hold the numbers of each line seperately
while (true)
{
std::string line;
std::getline(theStream, line);
if (line.empty())
break;
std::istringstream myStream(line);
std::istream_iterator<int> begin(myStream), eof;
std::vector<int> numbers(begin, eof);
// process line however you need
data.push_back(numbers); // add numbers of current line to data
}
std::cout << data[1][2] << '\n'; // 2nd row, 3rd number: 103
}
#include <cstddef>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::ifstream theStream("data.txt");
if (!theStream)
std::cerr << "data.txt\n";
std::size_t target_row = 2; // 1-based
std::size_t target_col = 3; // 1-based
int value = 0;
int valid = false;
for (std::size_t current_row = 1; true; ++current_row)
{
std::string line;
if (!std::getline(theStream, line) || line.empty())
break;
if (current_row != target_row)
continue;
std::istringstream myStream(line);
for (std::size_t current_col = 1; myStream >> value; ++current_col)
if (current_col == target_col) {
valid = true;
break;
}
break;
}
if (!valid)
std::cerr << "No such row and column!\n\n";
else
std::cout << value;
}