0

I have a problem with pushing a class Point into a vector.

I have a class Point:

Point::Point(){
this->X=1000;
this->Y=1000;
}

Point::Point(int x1, int y1){
this->X = x1;
this->Y = y1;
}

I create a vector like this:

vector<Point> vecteurPTries(nombrePoint);
for(i = 0; i < nombrePoint; i++){
    vecteurPTries.push_back(pointsTries[i]);
    cout<<"vecteur "<<vecteurPTries.at(i).getX()<<" "<<vecteurPTries.at(i).getY()<<endl;
}

Instead of having:

point trie 487 3
point trie 492 42
point trie 430 272
point trie 440 165
point trie 423 209
point trie 327 229
point trie 307 249
point trie 340 112
point trie 44 378
point trie 73 158

I have:

vecteur 1000 1000
vecteur 1000 1000
vecteur 1000 1000
vecteur 1000 1000
vecteur 1000 1000
vecteur 1000 1000
vecteur 1000 1000
vecteur 1000 1000
vecteur 1000 1000
vecteur 1000 1000

Can someone explain me why? What's the problem?

Thanks.

user3314570
  • 237
  • 4
  • 14
  • Nothing has changed the values of your vector between when you construct it and when you display it. At least this is what your posted code shows. – Thomas Matthews Dec 03 '14 at 22:03
  • 1
    Where did `487` and `3` come from? Post a full example so that we see exactly what you're doing. – PaulMcKenzie Dec 03 '14 at 22:07
  • Where and how do you define `pointsTries`? – vsoftco Dec 03 '14 at 22:07
  • Nitpick: C++ programmers should get used to using initializer syntax http://stackoverflow.com/questions/4589237/in-this-specific-case-is-there-a-difference-between-using-a-member-initializer always, as it is necessary sometimes so it's just more consistent. – Zan Lynx Dec 03 '14 at 22:37

3 Answers3

2

You don't show all the code, but I think the issue is that you use push_back to add entries to the end of the vector, but you instantiated the vector with size nombrePoint, which will cause it to create nombrePoint values that are default initialized. You then push another nombrePoint values on the end but you're not seeing those because your loop index is from 0 to nombrePoint.

What you want to do instead is

vector<Point> vecteurPTries;
vecteurPTries.reserve(nombrePoint);

This will create an empty vector for which you have reserved space for nombrePoint elements (not strictly necessary but will reduce re-allocation and relocation during the loop). The loop as given will then populate the first nombrePoint elements of this vector as you were intending.

sfjac
  • 7,119
  • 5
  • 45
  • 69
0

I assume pointsTries[i] is some kind of array like structure of points that looks like the list you expect.

Anyway, you declare a vector of length nombrePoint. After this line

vector<Point> vecteurPTries(nombrePoint);

it is already nombrePoint elements long. You are then adding to it so you are cout-ing elements nombrePoint+i when you think you are outputting element i.

Try

vector<Point> vecteurPTries;
for(i = 0; i < nombrePoint; i++){
    vecteurPTries.push_back(pointsTries[i]);
    cout<<"vecteur "<<vecteurPTries.at(i).getX()<<" "<<vecteurPTries.at(i).getY()<<endl;
}
user3353819
  • 911
  • 2
  • 8
  • 21
0

You construct your vector with nombrePoints number of default constructed Point instances (x=1000, y=1000). You then add nombrePoint Point instances at the end of your vector (these are the ones you are copying from your array pointsTries[i]). However you are only displaying the elements from 0-nombrePoints (the default cosntructed ones).

Your vector after the definition looks like this: 0... Point (1000,1000) 1... Point (1000,1000) ... nombrePoints-1 ... (1000,1000)

You then add new Point instances at the end of the vector (with push_back).

Change the vector definition to:

vector<Point> vecteurPTries;
markus
  • 1,631
  • 2
  • 17
  • 31