Vector trouble
Jan 23, 2010 at 9:40pm UTC
Hello all, came back to c++ from doing c# for about a year and im having some trouble with my vectors. Perhaps its my mis-understanding of them that is fooling me but as my code shows (hopefully) im trying to display the contents of my vector. it should be myVector[0] = 75, myVector[1] = 74, and on but i keep showing the value as the index number. Debugging shows that im inputting the values correctly but i cannot get it to display properly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int counter = 75;
vector <int > myVector(50);
vector <int >::iterator it;
vector <int >::reverse_iterator rit;
// Fill vec
for (int a = 0; a < 50; a++)
{
myVector[a] = a;
}
// Display vec contents
for (int b = 0; b < myVector.size(); b++)
{
cout << b << " : " << myVector[b] << "\n" ;
}
// Clear vec
myVector.clear();
cout << "\nSize : " << myVector.size();
// Resize the vec
for (int c = 0; c < 75; c++)
{
myVector.push_back(c);
}
cout << "\n\nNew Size : " << myVector.size() << "\n\n" ;
// Fill vec
for (int e = 0; e < myVector.size(); e++)
{
myVector.at(e) = counter;
counter--;
}
// Display vec contents
for (int d = 0; d < myVector.size(); d++)
{
cout << "index : " << myVector.at(d) << " Value : " << myVector[d] << "\n" ;
}
return 0;
}
Jan 23, 2010 at 9:55pm UTC
1 2 3 4 5 6
// Display vec contents
for (int d = 0; d < myVector.size(); d++)
{
cout << "index : " << myVector.at(d) << " Value : " << myVector[d] << "\n" ;
}
You are printing the value of the vector at the current index twice - NOT the current index number followed by the vector value of that index.
What I'm trying to say is that it should be this:
cout << "index : " << d << " Value : " << myVector[d] << "\n" ;
Last edited on Jan 23, 2010 at 9:58pm UTC
Jan 23, 2010 at 10:21pm UTC
Wow i must be tired, it seems so obvious now. Think its time for a few drinks, ty for your help.
Topic archived. No new replies allowed.