簡體   English   中英

讀取文件后提取子向量時出現C ++錯誤

[英]C++ Error when extracting subvector after reading file

我正在讀取文件並從中創建向量,然后,我嘗試如下提取子向量:

vector<double> v = {1, 2, 3, 4, 5};
displayVector(v);
vector<double> v2(&v[1], &v[v.size()]);
displayVector(v2);

在這種情況下,代碼可以正常工作,而我只是從原始向量中刪除了第一個功能,我還可以執行以下操作:

v.erase(v.begin());

這也給了我想要的輸出,但是對於我的應用來說,第一種方式很有趣。

這里的問題是Im從文件中讀取並構建矢量。 這樣的文件如下所示:

1000025,5,1,1,1,2,1,3,1,1,2
1002945,5,4,4,5,7,10,3,2,1,2
1015425,3,1,1,1,2,2,3,1,1,2
1016277,6,8,8,1,3,4,3,7,1,2
1017023,4,1,1,3,2,1,3,1,1,2
1017122,8,10,10,8,7,10,9,7,1,4
1018099,1,1,1,1,2,10,3,1,1,2
1018561,2,1,2,1,2,1,3,1,1,2
1033078,2,1,1,1,2,1,1,1,5,2
1033078,4,2,1,1,2,1,2,1,1,2
1035283,1,1,1,1,1,1,3,1,1,2
1036172,2,1,1,1,2,1,2,1,1,2
1041801,5,3,3,3,2,3,4,4,1,4
1043999,1,1,1,1,2,3,3,1,1,2
1044572,8,7,5,10,7,9,5,5,4,4
1047630,7,4,6,4,6,1,4,3,1,4
1048672,4,1,1,1,2,1,2,1,1,2
1049815,4,1,1,1,2,1,3,1,1,2
1050670,10,7,7,6,4,10,4,1,2,4
1050718,6,1,1,1,2,1,3,1,1,2
1054590,7,3,2,10,5,10,5,4,4,4
1054593,10,5,5,3,6,7,7,10,1,4
1056784,3,1,1,1,2,1,2,1,1,2

第一個坐標對我而言並不有趣,這是我的代碼:

void displayVector (std::vector<double> &v) {
    for (auto &feature : v) {
        std::cout << feature << " ";
    }
    std::cout << std::endl;
}

void displayTrainingSet(vector<FeaturedVector> &data) {
    for (auto a : data) {
        displayVector(a);
    }
}

int main(int argc, char const *argv[]) {

    ifstream file;
    string filename = "breast-cancer.data";

    file.open(filename, std::ifstream::in);

    vector<FeaturedVector> data;

    while (file.good()) {
        string line;
        getline(file, line);
        istringstream buffer(line);

        double feature;
        vector<double> v;

        while (buffer >> feature) {
            if (buffer.peek() == ',') {
                buffer.ignore();
            }
            v.push_back(feature);
        }

        vector<double> v1(&v[1], &v[v.size()]);
        data.push_back(v1);
    }

    file.close();

    //displayTrainingSet(data);
    return 0;
}

上面的代碼可以編譯,但是當我嘗試運行它時,出現了奇怪的錯誤,這是由行向量v1(&v [1],&v [v.size()])引起的:

libc++abi.dylib: terminating with uncaught exception of type std::length_error: vector
Abort trap: 6

有人知道為什么會這樣嗎? 我知道從文件中讀取並創建向量v是可行的,因為我已經嘗試在屏幕上顯示向量,並且可以正常工作。 先謝謝您的幫助!

getline(file, line);

當此代碼讀取文件的最后一行時,將讀取文件中的最后一個字符(最后一個換行符)。 該代碼從這個角度繼續,但file沒有 eof設置。 僅在由於文件結尾而導致嘗試從文件讀取失敗后才將eof位置1。

這還沒有發生。 getline()僅讀取文件中的最后一個字符,最后的換行符,然后停止。

在循環的下一個迭代中:

while (file.good()) {

這將成功。 正如我所解釋的,尚未設置eof標志。

    string line;
    getline(file, line);

現在,這將無法從文件中讀取任何內容,從而將文件的eof位置1。 line是一個空字符串。

由於line現在為空,因此內部while循環將不會提取任何內容,並且

vector<double> v;

將保持為空向量。

vector<double> v1(&v[1], &v[v.size()]);

鑒於v現在是一個空向量, v.size()將為0,那么您認為現在在這里會發生什么?

I modified your code a little bit to compile on my machine. i can display the data on your sample file.

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

using namespace std;

void displayVector (std::vector<double> &v) {
    for (auto &feature : v) {
        std::cout << feature << " ";
    }
    std::cout << std::endl;
}

void displayTrainingSet(std::vector< std::vector<double> > &data) {
    for (auto a : data) {
        displayVector(a);
    }
}

int main(int argc, char const *argv[]) {

    ifstream file;
    string filename = "C:/temp/2.txt";

    file.open(filename, std::ifstream::in);

    vector< vector<double> > data;

    while (file.good()) {
        string line;
        getline(file, line);
        istringstream buffer(line);

        double feature;
        vector<double> v;

        while (buffer >> feature) {
            if (buffer.peek() == ',') {
                buffer.ignore();
            }
            v.push_back(feature);
        }

        vector<double> v1(&v[1], &v[v.size()]);
        data.push_back(v1);
    }

    file.close();

    displayTrainingSet(data);
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM