What is the purpose of STL iterators? Why did programmers even create this concept?
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.
std::for_each
std::transform
, .
. , , . , , . , , . , . ( ) STL. . SGI Documentation.
STL
: . (. boostcon.
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, " ") );
- , , 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 );
, , . , ( ).
,
vector<myclass>::iterator myClassVectorIterator; for(myClassVectorIterator = myClassVector.begin(); myClassVectorIterator != myClassVector.end(); myClassVectorIterator++)
for (int i=0;i<size;i++) { ...
STL . , (, , ) , (, RandomAccess vs. Input), ( ) .
, , merge vector a list ostream_iterator, / .
merge
vector
list
ostream_iterator
, ...
STL - ++. ( , "STL " ).
? , ?
C, for(i=0;i!=N;++i), , , , [i], .
for(i=0;i!=N;++i)
[i]
, , for(p=&arr[0]; p != p+N; ++p, , .
for(p=&arr[0]; p != p+N; ++p
, . , "foreach" Perl, Lisp -style "apply".
Source: https://habr.com/ru/post/1750130/More articles:A sort list based on dynamically generated numbers in C ++ - c ++C ++ debugging compared to C debugging - c ++Problem with the UINavigationController button in landscape mode? - iphone-sdk-3.0Does YourKit report ReentrantLocks? - javaИспользование действия контроллера .NET MVC как источника HTML - c#Need some serious help with the problem of self-joining - sqlsvn recursive directory move shell script - bashIs it possible to get a pointer to an internal String ^ array in C ++ / CLI? - stringHow to load a workflow after pausing / saving it? - c #NullPointerException in activity testing tutorial - androidAll Articles