YouTip LogoYouTip

Cpp Libs Iterator

C++ Standard Library `` | Rookie Tutorial The `` header in the C++ Standard Library provides a set of tools for traversing elements in containers. Iterators are one of the core concepts in the C++ Standard Template Library (STL), allowing programmers to access elements in containers in a uniform way without worrying about the specific implementation details of the containers. An iterator is an object that provides a method to traverse elements in a container. Iterators can be viewed as pointers to elements in a container, but they are more flexible and powerful than pointers. Iterators can be used to access and modify elements in containers, and can be used together with STL algorithms. Iterators are mainly divided into the following categories: 1. **Input Iterator**: Can only perform single read operations, cannot perform write operations. 2. **Output Iterator**: Can only perform single write operations, cannot perform read operations. 3. **Forward Iterator**: Can perform read and write operations, and can move forward. 4. **Bidirectional Iterator**: In addition to all operations of forward iterators, can also move backward. 5. **Random Access Iterator**: In addition to all operations of bidirectional iterators, can also perform random access, such as accessing elements by index. ## Common Functions (Key Points) | Function | Purpose | Example | Note | | --- | --- | --- | --- | | `std::advance(it, n)` | Move iterator n steps | `advance(it, 2);` | Modifies original iterator | | `std::distance(a, b)` | Calculate distance between two iterators | `distance(v.begin(), v.end());` | Returns number of elements | | `std::next(it, n)` | Return new iterator n steps forward | `auto it2 = next(it, 2);` | Recommended, doesn't modify original | | `std::prev(it, n)` | Return new iterator n steps backward | `auto it2 = prev(it, 1);` | Supported since C++11 | ## Iterator Adapters (Very Important) Iterator adapters can change the behavior of iterators to adapt to different usage scenarios. | Adapter | Purpose | Example | | --- | --- | --- | | `std::back_inserter` | Insert at end (calls push_back) | `back_inserter(vec)` | | `std::front_inserter` | Insert at beginning (calls push_front) | `front_inserter(list)` | | `std::inserter` | Insert at specified position | `inserter(vec, it)` | ## Stream Iterators (IO Simplification Tool) | Type | Purpose | Example | | --- | --- | --- | | `std::istream_iterator` | Read data from input stream | `istream_iterator(cin)` | | `std::ostream_iterator` | Write to output stream | `ostream_iterator(cout, " ")` | ## Iterator Syntax The syntax for iterators is usually as follows: #include <iterator>// Traverse container using iteratorfor (ContainerType::iterator it = container.begin(); it != container.end(); ++it) { // Access element *it} ## Example Below is an example using the `` header and iterators to traverse `std::vector`: ## Example #include #include #include #include int main(){ // Create and initialize a vector container std::vector vec ={1, 2, 3, 4, 5}; // Traverse vector using iterator for(std::vector::iterator it = vec.begin(); it != vec.end();++it){ std::cout<<*it <<" "; } std::cout<< std::endl; // Simplify iterator type using auto keyword for(auto it = vec.begin(); it != vec.end();++it){ std::cout<<*it <<" "; } std::cout<< std::endl; // Use C++11 range-based for loop for(int elem : vec){ std::cout<< elem <<" "; } std::cout<< std::endl; // Use back_inserter for automatic insertion std::vector v2; std::fill_n(std::back_inserter(v2), 3, 100); // Use ostream_iterator for output std::copy(v2.begin(), v2.end(), std::ostream_iterator(std::cout, " ")); std::cout<< std::endl; return 0; } Output: 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 100 100 100 ## Summary The core functions of **** can be summarized in three points: * Unified access method: Different containers use the same traversal logic * Decoupling containers and algorithms: Algorithms only depend on iterators * Improved code reusability: The same code can be applied to multiple data structures For beginners, it is recommended to prioritize mastering the following: * `next / prev / distance` * `back_inserter` * Iterator traversal methods
← Cpp Libs NumericMongodb User β†’