STL Iterators - The Goal

What is the purpose of STL iterators? Why did programmers even create this concept?

+3
source share
10 answers

Iterators allow you to separate the algorithms from the container. As long as you have an iterator of start and end and know the capabilities of the iterator (random access, etc.), you can perform operations in the range indicated by iterators. For example, see. std::for_each, std::transform. Thus, you can implement only one version of the algorithm, and not write a new one for each container on which you want it to work.

+24
source

, .

+4

. , , . , , . , , . , . ( ) STL. . SGI Documentation.

: . (. boostcon.

+4

STL?

  • , , , .

:

std::vector<int> cont;
auto position = std::find(cont.begin(), cont.end(), 1); // forward search
auto position = std::find(cont.rbegin(), cont.rend(), 1); // backward search
  • ( , ) .

  • .

-, , :

std::vector<int> cont;
copy( cont.begin(), cont.end(), std::ostream_iterator<int>(std::cout, " ") );
+3

- , , C. BTW, , .

++, :

char buf1[3] = { 1, 2, 3 };
std::vector<char> buf2;
buf2.resize(sizeof(buf1));
std::copy( buf1, buf1+sizeof(buf1), buf2.begin() );
std::copy( buf2.begin(), buf2.end(), buf1 );
+2

, , . , ( ).

+1

,

vector<myclass>::iterator myClassVectorIterator;
for(myClassVectorIterator = myClassVector.begin(); 
        myClassVectorIterator != myClassVector.end();
        myClassVectorIterator++)

for (int i=0;i<size;i++) { ...
+1

STL . , (, , ) , (, RandomAccess vs. Input), ( ) .

, , merge vector a list ostream_iterator, / .

+1

, ...

STL - ++. ( , "STL " ).

0

? , ?

C, for(i=0;i!=N;++i), , , , [i], .

, , for(p=&arr[0]; p != p+N; ++p, , .

, . , "foreach" Perl, Lisp -style "apply".

0

Source: https://habr.com/ru/post/1750130/


All Articles