[C++] STL container - vector dynamic array container ⑥ (Use iterator to traverse vector container steps | Get the iterator that points to the first element of the container begin function | Get the end iterator | * Iterator dereference)






1. Use iterator to traverse vector container steps



1. Steps to use iterator to traverse vector container


Use iterator to traverse vector container,

First, get the starting range iterator, std::vector<int> type container, its iterator type is vector<int>::iterator, call the begin() function of the vector class, you can get the pointer to the first one in the container iterator of elements;

vector<int>::iterator it = vec.begin();

Then, to get the content of the element pointed to by the iterator, use the * operator, which actually calls the overloaded * operator function;

*it

Then, perform an auto-increment operation on the iterator. The auto-increment ++ operation actually calls the overloaded ++ operator function, which is used to increment the iterator. After execution, the iterator points to the next element;

it++

Finally, determine whether the iterator has iterated to the end of the container. Call the end() function of the vector class to obtain the iterator pointing to the last element in the container. Determine whether the current iterator value is equal to the iterator value of the last element. If not, If equal to continue iteration, if equal to stop iteration;

it != vec.end();

2. Code example - using iterator to traverse vector container


Code example:

#include "iostream"
using namespace std;
#include "vector"

int main() {
    
    

    // 创建空的 vector 容器
    std::vector<int> vec{
    
    1, 2, 3};

    // 遍历打印 vector 容器的内容 
    for (int i = 0; i < vec.size(); i++) {
    
    
        std::cout << vec[i] << ' ';
    }
    std::cout << std::endl;

    // 通过迭代器遍历数组
    for (vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {
    
    
        std::cout << *it << ' ';
    }
    std::cout << std::endl;

	
	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

1 2 3
1 2 3
Press any key to continue . . .

Insert image description here





2. Introduction to iterator commonly used APIs



1. vector container class begin function - obtains the iterator pointing to the first element of the container


By calling the begin function of the vector container class, you can obtain the iterator pointing to the first element of the container;

iterator begin();  
const_iterator begin() const;

The above functions all return an iterator pointing to the first element in the container;

The first overloaded version function is a non-const iterator, which can be used to modify the elements in the container;

The second overloaded version function is a constant iterator and cannot be used to modify the elements in the container;

The returned iterator can be dereferenced using the * operator to obtain the value of the element pointed to by the iterator;


Code example:

#include "iostream"
using namespace std;
#include "vector"

int main() {
    
    

    // 创建空的 vector 容器
    std::vector<int> vec{
    
    1, 2, 3};

	// 获取首元素迭代器
	vector<int>::iterator it = vec.begin();

	// 打印首元素
	cout << *it << endl;
	
	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

Insert image description here


2. Vector container class end function - get the end iterator


You can get the end iterator by calling the end function of the vector container class. The function prototype is as follows:

iterator end() const noexcept;  
const_iterator end() const noexcept;

Both of the above functions return an iterator pointing to a position after the last element in the container. The returned iterator does not point to any valid element, but can be used to compare and traverse the end of the container;


Special note: After modifying the vector container, end()the iterator returned by the function will not be automatically updated when the container is modified; if the elements in the vector container change, the end() function needs to be called again to obtain the new end iterator;


Code example:

#include "iostream"
using namespace std;
#include "vector"

int main() {
    
    

    // 创建空的 vector 容器
    std::vector<int> vec{
    
    1, 2, 3};

	// 获取末尾迭代器
	vector<int>::iterator it = vec.end();

	// 该迭代器指向 容器中 最后一个元素 之后一个位置 
	// 下面的代码会造成异常 , 不能获取对应的元素值
	cout << *it << endl;
	
	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Execution result: The end iterator points to a position after the last element in the container, which cannot be dereferenced and will cause an exception;

Insert image description here


3. iterator iterator class dereference operation - operator* overloaded operator function


The * operator can be used to dereference the iterator object. In the iterator class, the * operator is overloaded. The function prototype is as follows:

reference operator*() const;

The operator*() function returns a reference to the element pointed to by the iterator; when dereferencing an iterator, the value of the element pointed to by it is obtained;

operator* returns a reference to the element, not a copy of the element; if the value of the element is modified through the obtained reference, the elements in the vector container will also be modified;


Special note: operator* only applies to non-const iterators;


Code example:

    // 创建空的 vector 容器
    std::vector<int> vec{
    
    1, 2, 3};

	// 获取末尾迭代器
	vector<int>::iterator it = vec.begin();

	// 迭代器解引用
	*it;

4. iterator iterator increment operation - operator++ overloaded operator function


The ++ operator can be used to increment the iterator object. In the iterator class, the ++ operator is overloaded. The function prototype is as follows:

// 前置 ++ 自增操作
iterator& operator++();  

// 后置 ++ 自增操作
iterator operator++(int);

Both of the above two function prototypes can increment the iterator object so that the iterator points to the next element. Both functions can only be used for non-const iterators;

  • Prefixed increment operator++: returns a reference to the modified iterator itself, allowing you to increment the iterator and use it in a statement;

  • Post-increment operator++: returns a new iterator that points to the next element, leaving the original iterator unchanged; this operator overloads the int parameter to avoid confusion with the precedence of the pre-increment operator


Code example:

    // 创建空的 vector 容器
    std::vector<int> vec{
    
    1, 2, 3};

	// 获取末尾迭代器
	vector<int>::iterator it = vec.begin();

	// 前置递增操作符
	it++;
	
	// 后置递增操作符
	it++;

Guess you like

Origin blog.csdn.net/han1202012/article/details/135092941