Take the 2-minute tour ×

I am trying to load some data from a text file into a vector of structures. My question is, how do you indicate the size of the vector? Or should I be using a the vector push_back function to do this dynamically, and if so, how does this work when populating a structure?

The full program is outlined below:

My structure is defined as

struct employee{
    string name;
    int id;
    double salary;

and the text file (data.txt) contains 11 entries in the following format:

Mike Tuff
1005 57889.9

where "Mike Tuff" is the name, "1005" is the id, and "57889.9" is the salary.

I am trying to load the data into a vector of structs using the following code:

#include "Employee.h" //employee structure defined in header file

using namespace std;

vector<employee>emps; //global vector 

// load data into a global vector of employees.
void loadData(string filename)
    int i = 0;
    ifstream fileIn;

    if( ! fileIn )  // if the bool value of fileIn is false
         cout << "The input file did not open.";

        fileIn >> emps[i].name >>emps[i].id >> emps[i].salary ;


When I execute this, I get an error that says: "Debug Assertion Failed! Expression: vector subscript out of range."

share|improve this question

2 Answers 2

up vote 2 down vote accepted

A vector is expandable, but only through push_back(), resize() and a few other functions - if you use emps[i] and i is greater than or equal to the size of the vector (which is initially 0), the program will crash (if you're lucky) or produce weird results. If you know the desired size in advance, you can call e.g. emps.resize(11) or declare it as vector<employee> emps(11);. Otherwise, you should create a temporary employee in the loop, read into it, and pass it to emps.push_back().

share|improve this answer
Oh my god, you're an angel! Got it to work perfectly with the following new while loop: while(fileIn) { employee temp; getline(fileIn, temp.name); fileIn >> temp.id; fileIn >> temp.salary; fileIn.ignore(1); emps.push_back(temp); i++; } –  Kelly King Feb 24 '11 at 21:11
you can also use insert to place your structs into a specified position whereas push_back always appends to the end of the vector. –  AJG85 Feb 24 '11 at 21:44
@user633055: Glad to hear it; the code you show here is what I had in mind. If you are satisfied, you should mark your preferred answer as accepted; this will make it more likely that future questions from you will be answered. :-) –  Aasmund Eldhuset Feb 25 '11 at 0:20
@AJG85: Indeed - but be cautious when using insert() to insert into large vectors (unless you always insert close to the end), as the performance of such operations is very bad (because all elements to the right of the insert position must be moved one slot to the right). (I assume you know this, @AJG85; this info is directed towards the OP.) –  Aasmund Eldhuset Feb 25 '11 at 0:25
@Aasmund yes of course, I was just mentioning one of those "few other functions" worth knowing about :P –  AJG85 Feb 25 '11 at 0:38
std::istream & operator >> operator(std::istream & in, employee & e)
  return in >> e.name >> e.id >> e.salary; // double not make good monetary datatype.

int main()
  std::vector<employee> emp;
  std::copy(std::istream_iterator<employee>(std::cin), std::istream_iterator<employee>(), std::back_inserter(emp));
share|improve this answer
Nice idea, but the original code used an ifstream, not cin. –  aschepler Feb 24 '11 at 21:36
You're kidding, right? –  Crazy Eddie Feb 24 '11 at 23:38

Your Answer


