【问题标题】:Extract words from a file and put them into a `std::vector` of `std::array<string,4>`从文件中提取单词并将它们放入 `std::array<string,4>` 的 `std::vector`
【发布时间】:2020-11-21 23:35:53
【问题描述】:

以下是我要从中读取行的文件(ka.txt):-

ASD|BSD|CSdsa|ood
fmads|aok|pdski
kdijf|okmdsomf|opkasd|okd
asdas
kamkd|aoda|kked|ok

以下是我为将这些数据放入 std::array<:string> 的向量而编写的代码。

#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <fstream>
#include <sstream>
int main(){
        std::fstream file("ka.txt");
        std::vector<std::array<std::string, 4>> darr; darr.reserve(5);
        std::array<std::string, 4> istd;
        std::string * line = new std::string; std::string * word = new std::string;
        while(std::getline(file, *line)){
                std::stringstream ss(*line);
                int per = 0;
                while(!ss.eof()){
                        std::getline(ss, *&istd[per], '|');
                        per++;
                }       
                darr.emplace_back(istd);
        }       
        file.close();
        
        for(int i = 0; i < darr.size(); i++){
                for(int j = 0; j < 4; j++){
                        std::cout << darr[i][j] << "\t";
                }       
                std::cout << "\n";
        }       
  
        return 0;       
}

基本上,我有一个具有std::array 的向量,每个std::array 的大小为4。所以我正在使用| 分隔符从文件中读取数据。我想将文本文件每一行中的每个元素存储为std::array。并将这些std::arrays 存储在一个向量中。另外,如果一行有 4 个元素,由| 分隔,那么std::array 中应该有四个元素。如果行中只有一个元素,那么std::array 中应该只有一个元素 所以当我运行这段代码时,我得到以下输出:-

ASD BSD CSdsa   ood 
fmads   aok pdski       
kdijf   okmdsomf    opkasd  okd 
asdas       opkasd  okd 
kamkd   aoda    kked    ok  

所以,第四行有一个错误的std::array,因为它应该只有一个元素,但它应该只有一个。我怎样才能做到这一点?

另外,我认为可能的问题出在代码第 12 行开始的 while 循环中。

【问题讨论】:

  • 绝对没有理由在这个程序中使用new
  • std::string line, word; -- 这就是你需要做的。
  • 也可以将*&amp;istd[per] 简称为istd[per]

标签: c++ arrays c++11 while-loop


【解决方案1】:

我测试了下面的代码。 istd 的使用使它显示错误的输出,因为它记住了以前的值。我将其更改为使用向量而不是数组。 正如保罗所指出的,我们不需要使用 new 作为字符串。所以删除了它。

#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <fstream>
#include <sstream>
int main(){
        std::fstream file("file.txt");
        vector<vector<string>> darr;
        std::string  line,word;
        while(std::getline(file, line))
        {
            std::stringstream ss(line);
            vector<string> temp;
            string str;
            while(!ss.eof())
            {
                std::getline(ss, str, '|');
                temp.push_back(str);
            }
            darr.emplace_back(temp);
        }
        file.close();
        for(int i = 0; i < darr.size(); i++)
        {
            for(int j = 0; j < 4; j++)
                    std::cout << darr[i][j] << " ";
            printf("\n");
        }
        return 0;
}

如果您不想存储,我也有相同代码的缩短版本。

int main(){
        std::fstream file("file.txt");
        std::string  line,word;
        while(std::getline(file, line))
        {
            std::stringstream ss(line);
            vector<string> temp;
            string str;
            while(!ss.eof())
            {
                std::getline(ss, str, '|');
                std::cout << str << " ";
            }
            printf("\n");
        }
        file.close();
        return 0;
}

输出:

ASD BSD CSdsa ood 
fmads aok pdski  
kdijf okmdsomf opkasd okd 
asdas    
kamkd aoda kked ok

【讨论】:

  • std::string * line = new std::string; std::string * word = new std::string; -- 你至少应该解决这个问题。
  • 编辑了帖子。
【解决方案2】:

这是因为数组istd 是在没有初始化的情况下使用的,所以当那里没有读取数据时,前几行的数据会保留。

std::array<std::string, 4> istd;

应该在

while(std::getline(file, *line)){

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多