vectors

Just start learning vectors. can some one explain these to me in detail and if it is possible to code any real world example

vector<double>::size_type i = 0

and

vector<int>::iterator i

Please explain these two line . please i need to clear my concept.
Last edited on
http://www.cplusplus.com/reference/vector/vector/?kw=vector

From that page:
size_type - an unsigned integral type that can represent any non-negative value of difference_type. usually the same as size_t


iterator - a random access iterator to value_type

In other words, a pointer to an element of the vector.
I thought Iteration mean loop. and Iterator is something like loop
Iterators are commonly used as the control variable in loops.
1
2
3
4
5
6
7
  vector<int> vec;
  vector<int>::iterator  iter;  

  for (iter=vec.begin(); iter!=vec.end(); iter++)
  {  // print each element
      cout << *iter << endl;
  }

vector<int>::iterator iter
means iter is a pointer veriable point to vector of integer type and that is vec in this case ?
std::vector<>, std::list<> etc. are sequence containers.
A sequence container holds a sequence of elements.

Iteration through a sequence involves accessing each element in turn:
1
2
3
4
5
6
7
8
0. start by intialising a 'pointer' to 'point' to the first element

loop:
    1. dereference the 'pointer' to get the 'pointed' element
    2. do something with it
    3. move the 'pointer' to 'point' to the next element
    4. if we have gone past the last element in the sequence break
       else continue with the loop


An iterator is a 'pointer' like object that can 'point' to a specific element in the sequence.

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
#include <vector>
#include <list>

int main()
{
    // add 5 to every element in a sequence

    {
        int seq[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
        typedef int* pointer ;
        for( pointer ptr = seq+0 ; // 0. start by intialising a pointer
                                     // to point to the first element
             ptr != seq+10 ; // 4. if we have gone past the last element, break
             ++ptr ) // 3. move the pointer to point to the next element
        {
            int& v = *ptr ; // 1. dereference the pointer to get the pointed element
            v += 5 ; // 2. do something with it
        }
    }


    {
        std::vector<int> seq = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
        typedef std::vector<int>::iterator pointer ;
        for( pointer ptr = seq.begin() ; // 0. start by intialising a 'pointer'
                                           // to 'point' to the first element
             ptr != seq.end() ; // 4. if we have gone past the last element, break
             ++ptr ) // 3. move the 'pointer' to 'point' to the next element
        {
            int& v = *ptr ; // 1. dereference the 'pointer' to get the 'pointed' element
            v += 5 ; // 2. do something with it
        }
    }


    {
        std::list<int> seq = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
        typedef std::list<int>::iterator pointer ;
        for( pointer ptr = seq.begin() ; // 0. start by intialising a 'pointer'
                                           // to 'point' to the first element
             ptr != seq.end() ; // 4. if we have gone past the last element, break
             ++ptr ) // 3. move the 'pointer' to 'point' to the next element
        {
            int& v = *ptr ; // 1. dereference the 'pointer' to get the 'pointed' element
            v += 5 ; // 2. do something with it
        }
    }
}
Last edited on
Topic archived. No new replies allowed.