gpt4 book ai didi

c++ - 从迭代器中提取数字

转载 作者:行者123 更新时间:2023-11-30 03:20:48 26 4
gpt4 key购买 nike

this SO 问题,@HowardHinnant 给出了一个绝妙的答案,它非常有效地将文本文件中的数字提取到 vector 中。这是简洁的代码:

#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';
}
}

数据样本是

800 170 84 439 
129 902 103 492 394 140 496 893
229 645
164 389 74 208 726 315
291 421 230 789 246 791 762
416 241 538 810 605
714 555 54 863
288 465 563 831
0 339 740 427 718
449 675 545 842 779 607
274 958

这段代码我已经研究了两天了,不太明白如何从每一行中提取数字。那么,比方说,我如何访问第二行的第 3 个数字 (103),或者更一般地说,第 m 行的第 n 个数字?

任何关于此解决方案如何工作的解释也会很有帮助!

最佳答案

使用 vectorvector 分别存储每行编号:

#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
}

Assuming the query will only be made once, you can do it in one pass by counting which row and column you're on while you're extracting (and with each iteration of the while loop); then break from there. This is so that you don't have to read the entire file.

可能看起来像:

#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;
}

关于c++ - 从迭代器中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52543440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com