C++ function iterator is a type of tool that is used to traverse through a container and access its elements. It enables us to access elements of an array without knowing the internal index of the element or the length of the array. This can be extremely useful when dealing with large collections of data.
Here are some code examples using C++ function iterator:
Example 1: In this example, we have a vector containing integers, and we use the iterator to loop through the elements of the vector.
```c++ #include #include
int main() { std::vector myVec = {1, 2, 3, 4, 5};
for(auto it = myVec.begin(); it != myVec.end(); ++it) { std::cout << *it << " "; // prints 1 2 3 4 5 }
return 0; }
Example 2:
In this example, we use the iterator to loop through the elements of a string.
c++ #include #include
int main() { std::string myString = "Hello World!";
for(auto it = myString.begin(); it != myString.end(); ++it) { std::cout << *it << " "; // prints H e l l o W o r l d ! }
return 0; }
Example 3:
In this example, we use the iterator to find the minimum and maximum element in a vector.
c++
#include
#include
#include
int main()
{
std::vector myVec = {3, 5, 1, 4, 2};
auto minElement = std::min_element(myVec.begin(), myVec.end());
std::cout << "Minimum element is: " << *minElement << std::endl; // prints 1
auto maxElement = std::max_element(myVec.begin(), myVec.end());
std::cout << "Maximum element is: " << *maxElement << std::endl; // prints 5
return 0;
}
```
C++ function iterator is part of the standard C++ library, which means it is included in the standard C++ package.
C++ (Cpp) iterator - 30 examples found. These are the top rated real world C++ (Cpp) examples of function::iterator extracted from open source projects. You can rate examples to help us improve the quality of examples.