sigsegv segmentation fault

hello guys, when debugging with GNU debugger i got "sigsegv segmantation fault" error .the program craches whener run. it is simple program which reads integer from text file and stores in vector container.
but when using an array there is no problem.is there wrong with vector initialization?..

#include <iostream>
#include <vector>
#include <fstream>
typedef long long int c;
using namespace std;
int main(int argc,char *argv[])
{
fstream infile("pi_data.txt",ios::in),outfile("output.txt",ios::out);
vector<c> v1;
int i=0;
while(i<=90000)
{
infile>>v1[i];
i++;
}
infile.close();
return 0;
}

i'm using Codeblocks with mingw (4.4.1) in windows 7.
You're trying to access elements of v1, even though you never added any.
Whoever/whatever told you that vector existed should have told you how to add elements to it.

typedef long long int c;
Seriously?
Last edited on
Regarding vectors -- take a look at the example program for vector::push_back() found in the tutorials on this site: http://www.cplusplus.com/reference/vector/vector/push_back/

You can add elements to your vector using push_back(). E.g:

1
2
3
4
5
6
7
8
9
10
11
12
int c;
vector<int> vec1;

for(int i=0; i<5; i++)
{
    vec1.push_back(i);
}

for(unsigned int i=0;i<vec1.size();i++)
{
    cout << vec1[i] << endl; // will output the values 0, 1, 2, 3, 4
}


Last edited on
Topic archived. No new replies allowed.