### 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.
 ``1234567`` `````` vector vec; vector::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:
 ``12345678`` ``````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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include 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 seq = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; typedef std::vector::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 seq = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; typedef std::list::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.