代码之家  ›  专栏  ›  技术社区  ›  AngusTheMan

从迭代器中提取数字

  •  1
  • AngusTheMan  · 技术社区  · 6 年前

    this 所以问题是,@HowardHinnant有一个很好的答案,它可以非常有效地将数字从文本文件中提取到向量中。以下是简短的代码:

    #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 
    

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

    1 回复  |  直到 6 年前
        1
  •  2
  •   Swordfish    6 年前

    使用 vector vectors

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

    假设查询只进行一次,则可以通过计算提取时所使用的行和列(以及while循环的每次迭代)一次完成;那就从那里离开。这样您就不必读取整个文件。

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