forked from learncppnow/9E
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.1 ContainerIteratorRelationship.cpp
45 lines (36 loc) · 1.19 KB
/
15.1 ContainerIteratorRelationship.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main ()
{
// A dynamic array of integers
vector<int> intArray;
// Insert sample integers into the array
intArray.push_back (50);
intArray.push_back (2991);
intArray.push_back (23);
intArray.push_back (9999);
cout << "The contents of the vector are: " << endl;
// Walk the vector and read values using an iterator
vector<int>::iterator arrIterator = intArray.begin ();
while (arrIterator != intArray.end ())
{
// Write the value to the screen
cout << *arrIterator << endl;
// Increment the iterator to access the next element
++ arrIterator;
}
// Find an element (say 2991) in the array using the 'find' algorithm...
vector<int>::iterator elFound = find (intArray.begin (),
intArray.end (), 2991);
// Check if value was found
if (elFound != intArray.end ())
{
// Value was found... Determine position in the array:
int elPos = distance (intArray.begin (), elFound);
cout << "Value "<< *elFound;
cout << " found in the vector at position: " << elPos << endl;
}
return 0;
}