在尝试使用向量填充结构时,向量下标超出范围

时间:2014-12-10 06:04:04

标签: c++ vector struct range subscript

我重写了这个程序的次数比我承认的要多,如果有人可以帮助我,我愿意再做一次。我有一个程序利用.csv文件将有关1343马拉松运动员的信息拉入矢量。然后从矢量到结构,以便我可以根据年龄,性别等事情对其进行排序。这是我到目前为止所做的:

    #include "stdafx.h"
#include "iostream";
#include "fstream";
#include "string";
#include "cstring";
#include "cmath";
#include "iomanip";
#include "cassert";
#include "cstdlib";
#include "ctime";
#include "cctype";
#include "algorithm";
#include "sstream";
#include "time.h";
#include "vector";

using namespace std;

struct runner{
    int position;
    string time;
    int age;
    string sex;
    string gender;
    string firstName;
    string lastName;
    string city;
    string state;

};
int main(){

vector<runner> r;
  int i = 0;
  string counter;
  int holder = 0;

  string header;

  ifstream infile("C:\\Users\\Anthony\\Desktop\\cmarathon.csv");

  if (infile.is_open()) {
    getline( infile, header );
    cout<<header;

    while (getline(infile, counter)){
        holder++;
    }

    r.push_back(r[holder]);

    for (i = 0; i <= holder; i++){

        string dataChunk;
        int value;



        getline(infile, dataChunk, ',');
        value = atoi(dataChunk.c_str());
        r[i].position = value;

        getline(infile, dataChunk, ',');
        r[i].time = dataChunk;

        getline(infile, dataChunk, ',');
        value = atoi(dataChunk.c_str());
        r[i].age = value;

        getline(infile, dataChunk, ',');
        r[i].sex = dataChunk;

        getline(infile, dataChunk, ',');
        r[i].gender = dataChunk;

        getline(infile, dataChunk, ',');
        r[i].firstName = dataChunk;

        getline(infile, dataChunk, ',');
        r[i].lastName = dataChunk;

        getline(infile, dataChunk, ',');
        r[i].city = dataChunk;

        getline(infile, dataChunk, ',');
        r[i].state  = dataChunk;}

  }


    system("PAUSE");
    return 0;
  }

我对矢量非常新,我只是无法弄清问题是什么。我认为通过在for()循环之前使用push_back,我会有足够的插槽打开?提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

Vector r没有索引holder的元素所以下面的语句

r.push_back(r[holder]);

错了。也许你的意思是以下

r.resize( holder );

也是这个循环

for (i = 0; i <= holder; i++){

必须看起来像

for (i = 0; i < holder; i++){

这是我必须少于持有人

答案 1 :(得分:0)

有很多小问题......下面我提供了前4个字段的清理版本。一些说明:

  • 您不需要预设vector ...如果您push_back它将自动透明且合理有效地调整大小(即以越来越大的步骤)来容纳额外的元素

  • 您通常希望字段输入命令的持续成功来控制while循环,该循环会填充单个runner对象,然后push_back - 编辑到vector

    • 尝试对错误状态的流进行进一步操作将无害地失败

    • 处于错误状态的流将在布尔评估上下文中评估false(例如whileif条件)

  • 您可以使用>>来解析数字并使用数字

  • 使用getline()将除了尾随逗号之外的所有逗号读入字符串,从流中删除该逗号

  • 我提供了一个&#34;帮助&#34;使用逗号的函数eat() ...至关重要的是,如果逗号没有读取,它会将输入流设置为失败状态

代码:

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

using namespace std;

struct runner
{
    int position;
    string time;
    int age;
    string sex;
};

template <char C = ','>
std::istream& eat(std::istream& is)
{
    char c;
    if (!(is >> c && c == C))
        is.setstate(std::ios::failbit);
    return is;
}


int main()
{
    std::istringstream in("12, 4:45, 45, M\n"   // sample input - change to your file
                          "8, 3:46, 33, F\n");
    std::vector<runner> runners;

    runner r;
    while (in >> r.position)
        if (in >> eat &&
            getline(in, r.time, ',') &&
            in >> r.age >> eat >> r.sex)
            runners.push_back(r);
        else
        {
            std::cerr << "error in data for position " << r.position << '\n';
            exit(1);
        }

    // ...  use runners ...
}

然后您可以使用向量:

    // output all ages...
    for (auto& r : runners)
        std::cout << runners.age << '\n';

    // resort by age...
    std::sort(std::begin(runners), std::end(runners),
              [](const runner& x, const runner& y) { return x.age < y.age; }
    );