为什么在c ++中向量结束后插入数据的错误

时间:2014-05-22 22:10:42

标签: c++ vector insert

#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
string** DataStorage;

string line;
vector<vector<string>> extractingData(200, vector<string>(10));
vector<vector<string>> extractingData2(200, vector<string>(10));

string pathExtract;
string pathStorage;

ofstream storageFile;
ifstream storageFileRead;
fstream extractingFile;
fstream extractingFile2;


void ReadData(fstream& extractingFile, vector<vector<string>> extractingData, string pathExtract)
{
    extractingFile.open(pathExtract, ios::in | ios::out | std::ios_base::app);

    int i = 0;
    int index;
    if (extractingFile.is_open())
    {
        while (getline(extractingFile, line))
        {
            istringstream iss(line);
            int j = 0;
            while (!iss.eof()){
                iss >> extractingData[i][j];
                j++;
            }
            i++;
        }

        extractingFile.close();
    }
}


int main() {
    pathExtract = "sample.txt";
    pathStorage = "mergedData.txt";
    ReadData(extractingFile, extractingData, pathStorage);
    ReadData(extractingFile2, extractingData2, pathExtract);
    mergeData(extractingData, extractingData2, pathStorage);

}


void mergeData(vector<vector<string>> extractingData, vector<vector<string>> extractingData2, string pathStorage){

    int i = 0;
    int j = 0;
    while (i<extractingData.size())
    {
        while (extractingData[i][0] != extractingData2[j][0])
        {
            j++;
        }
        if (extractingData[i][0] == extractingData2[j][0])
        {
            //extractingData[i].insert
            extractingData[i].insert(extractingData[i].end(), extractingData2[j].pop_back()); // here is error
        }   
        i++;
    }

//  storageFileRead.open(pathStorage, std::ios_base::out | std::ios_base::app);
    storageFileRead.open(pathStorage);
    i = 0;
    if (storageFileRead.is_open())
    {
        ofstream outFile(pathStorage);
        while (getline(storageFileRead, line)){

            std::vector<int>::const_iterator col;   
            for (col = extractingData[i].begin(); col !=extractingData[i].end(); ++col) { // here is errors, too
                outFile << *col;
                }
            outFile << endl;
            i++;
        }
    }
}

我在名为mergedData.txt的文本文件中有一个如下所示的数据集

country OD DM              
Croatia 36 15
Spain 35 26
Belgium 32 38
USA 25 57
France 24 29
Estonia 24 19

并且在名为sample.txt的文本文件中给出了新的数据集。

country VT
Estonia 21%
Belgium 26%
Croatia 6%
France 29%
Spain 13%
USA 42%

我想在每行的末尾添加一些名为VT(志愿者时间)的新变量。

country OD DM VT             
Croatia 36 15 6
Spain 35 26 13
Belgium 32 38 26
USA 25 57 42
France 24 29 29
Estonia 24 19 21

但有一些错误。我会很高兴有任何帮助。

1 个答案:

答案 0 :(得分:0)

extractingData[i].insert(extractingData[i].end(), extractingData2[j].pop_back()); // here is error

std::vector<>::pop_back不会返回值。它只是删除元素。首先获取值,然后调用pop_back

auto back = std::move(extractingData2[j].back());
extractingData2[j].pop_back();
extractingData[i].push_back(std::move(back));