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

C++ 尝试用向量填充结构时,向量下标超出范围,c++,vector,struct,range,subscript,C++,Vector,Struct,Range,Subscript,我重写这个程序的次数比我愿意承认的要多,如果有人能帮助我,我愿意再做一次。我有一个程序,它利用.csv文件将1343名马拉松运动员的信息拉入一个向量。然后从向量到结构,这样我就可以根据年龄、性别等因素对它进行排序 #include "stdafx.h" #include "iostream"; #include "fstream"; #include "string"; #include "cstring"; #include "cmath"; #include "iomanip"; #

我重写这个程序的次数比我愿意承认的要多,如果有人能帮助我,我愿意再做一次。我有一个程序,它利用.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;
  }
#包括“stdafx.h”
#包括“iostream”;
#包括“fstream”;
#包括“字符串”;
#包括“cstring”;
#包括“cmath”;
#包括“iomanip”;
#包括“卡塞特”;
#包括“cstdlib”;
#包括“ctime”;
#包括“cctype”;
#包括“算法”;
#包括“sstream”;
#包括“time.h”;
#包括“向量”;
使用名称空间std;
结构转轮{
内部位置;
串时间;
智力年龄;
弦性;
字符串性别;
字符串名;
字符串lastName;
字符串城市;
字符串状态;
};
int main(){
向量r;
int i=0;
字符串计数器;
int holder=0;
字符串头;
ifstream-infle(“C:\\Users\\Anthony\\Desktop\\cmarathon.csv”);
if(infle.is_open()){
getline(填充、标题);

coutVector
r
没有带索引的元素
holder
,因此下面的语句

r.push_back(r[holder]);
是错的。也许你是指以下几点

r.resize( holder );
还有这个循环

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

对于(i=0;i有很多小问题…下面我为前4个字段提供了一个清理版本。一些注意事项:

  • 您不需要预先设定
    向量的大小
    …如果
    推回
    ,它将以透明和合理的效率(即以越来越大的步长)自动调整大小,以容纳额外的元素

  • 通常,您希望字段输入命令持续成功地控制while循环,该循环填充单个
    runner
    对象,然后将
    推回
    -ed到
    向量

    • 对处于错误状态的流进行的进一步调度操作将以无害的方式失败

    • 处于错误状态的流将在布尔计算上下文中计算
      false
      (例如
      while
      if
      条件)

  • 您可以使用
    >
    对数字进行解析并使用数字

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

  • 我提供了一个使用逗号的“helper”函数
    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 ...
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构转轮
{
内部位置;
串时间;
智力年龄;
弦性;
};
模板
std::istream&eat(std::istream&is)
{
字符c;
如果(!(is>>c&&c==c))
is.setstate(std::ios::failbit);
回报是;
}
int main()
{
std::istringstream输入(“12,4:45,45,M\n”//sample输入-更改为您的文件
“8,3:46,33,F\n”);
std::载体;
跑步者r;
同时(在>>右侧位置)
如果(在>>中)吃&&
getline(in,r.time,',')&&
在>>r.age>>吃>>r.sex中)
跑步者。向后推(r);
其他的
{

std::cerr当
r
是一个空向量(它位于
main
的开头)时,
r[holder]
对于
holder
的任何值都是无效的
毫无意义-它应该实现什么?holder在for循环之前从while循环收到~1343的值。我想通过使用push_back()可以将空间分配给{r}。我是否可以通过等待初始化向量直到获得行计数来解决此问题?您可能正在寻找我需要的
vector::resize
。但我仍然无法实现它。我尝试了“r.resize(holder)”希望将其调整到适当的长度,但我仍然面临相同的错误。我认为这与for()循环如何向结构添加数据有关。可能是这样吗?编辑:我可以通过调整大小到holder+1使其正常工作。非常感谢您的帮助。
#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; }
    );