Understanding Iterators in STL

Hi, so I got a little confused about iterators and actually ... in C ++ STL

In this case, im uses a list, and I don't understand why you should make an iterator:
std::list <int>::const_iterator iElementLocator;

to omit the contents of the list by the dilution operator:
cout << *iElementLocator;

after assigning it, possibly list.begin ();

Please explain what exactly is an iterator and why should I use / use it!
Thank!!

+5
source share
6 answers

There are three building blocks in STL:

  • Containers
  • Algorithms
  • iterators

. , - ; , , , . . , - . , .

, , , , . , , , , . , . , , , "" , , , - .

, , . : , . . " ", . , , . , , .

.

, . , . . copy():

template<class In, class Out>
Out copy(In first, In last, Out res)
{
    while( first != last ) {
        *res = *first;
        ++first;
        ++res;
    }
    return res;
}

copy() , templated In Out. , first last, res. , ++first ++res. , x = *first , *res = x. . , In Out, .

+10

. , , , .

, , :

MSDN ,

- , , ++ . . , , . , . [...]

, , MSDN , ++ Standard, §24.1/1, ,

- , C + + () . , , , . , * i, , T, . (* i).m , i- > m , (*. X , .

cplusplus ,

++ , (, ), ( , (++) (*) ).

[...]

:

. , , , ++. ++ .

+6

STL, . - STL. (, *iElementLocator, iElementLocator++). (http://www.cplusplus.com/reference/std/iterator).

+2

. , .

. size , , [], .

" " . : c[1000000], , , , .

"" . , start more next, :

c.start();
while (c.more()) 
{
    item_t item = c.next();

    // use the item somehow
}

" " . . , ? , . , - .

, . :

container_t::iterator i = c.begin();

i - , . , :

item_t item = *i;

:

i++;

:

i += 1000;

, :

item_t item = i[1000];

.

, , end:

while (i != c.end())

end , , .

, ( ++ ), , . , , : , , . undefined - !

+1

. .

One example .

If there is anything specific you don’t understand, go back and ask.

0
source

I would suggest reading about operator overloading in C ++. This will say why *and ->can mean essentially anything. Only then should you read about iterators. Otherwise, this may seem very confusing.

0
source

All Articles