[Help]Vectors.

Posts 1–2 of 2 · Page 1 of 1
[Help]Vectors.
D: i didn't post here in a while, feels bad

Alright, so i think i got this, but i'm not too sure, if i'm wrong, tell me on what

[php]#include <algorithm>[/php]
Needed for iterators?


[php]#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
int number;
vector <int> vecIntArray;
vecIntArray.push_back (99);
vecIntArray.push_back (759);
vecIntArray.push_back (9539);
vecIntArray.push_back (2);
cin >> number;
vecIntArray.push_back (number);[/php]

so .push_back adds values starting from the beginning?

[php]cout << "Contents of the vector\n";
vector <int>::iterator iGoThruVector = vecIntArray.begin ();

while (iGoThruVector != vecIntArray.end ())
{
cout << *iGoThruVector << '\n';
++iGoThruVector;
}[/php]


vecIntArray.begin starts from the beginning.

then while the iterator is not at the end, print value, then increment to the next value in the vector

[php] vector <int>::iterator iElement = find (vecIntArray.begin (),vecIntArray.end (), 9539);
if (iElement != vecIntArray.end ())
{

int placeInVector = distance (vecIntArray.begin (), iElement);
cout << "value: " << *iElement;
cout << " value was in position: " << placeInVector << '\n';
}

return 0;
}[/php]

So it finds the value specified between beginning and end.

then uses distance from the beginning to iElement to determine position?


Amiright? /













The colors made me quit reading somewhere near the start, however the code itself seems fine(I never use vectors so no idea if it's correct or not)
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?