[C++] STL container - vector dynamic array container ⑤ (vector container element access | at function | [] operator overloaded function | vector container head and tail element access)






1. Access to vector container elements



1. The vector container accesses the element at the specified index - at function


The vector container accesses the element at the specified index by using at()functions and []operators;


The at function of the vector class can access the element at the specified index position. The function prototype is as follows:

const_reference at(size_type pos) const;

This function returns a constant reference to the element at the specified position in the container;

Special note: If the specified position exceeds the scope of the container, atthe function will throw std::out_of_rangean exception. Before using the at function, it is best to check whether the position is within the scope of the container;

It is recommended to use [0, vec.size() - 1] as the index value between closed intervals;

When traversing, it is recommended to use

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

As a traversal condition;


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.at(i) << ' ';
    }
    std::cout << std::endl;

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

	return 0;
};

Results of the :

1 2 3
Press any key to continue . . .

Insert image description here


2. The vector container accesses the element at the specified index - [] operator overloaded function


The vector container can use the [] operator to access its elements. The [] operator overloaded function is called. The function prototype is as follows:

reference operator[](size_type pos);  

This function returns a reference to the element at the specified position in the vector container;

The [] operator overloaded function is the same as the at function. If the positional parameter exceeds the scope of the container, the [] operator overloaded function will throw an exception;

Therefore, before using the [] operator overload, you should also check whether the position is within the scope of the 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;

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

	return 0;
};

Insert image description here





2. Access to the first and last elements of the vector container



1. Access function for the first and last elements of vector container


Vector container head and tail element access function:

  • Access the first element of the vector container: The front() member function of the vector container class returns a constant reference, representing the first element in the container;
const_reference front() const noexcept;
  • Access the tail element of the vector container: the back() member function of the vector container class returns a constant reference, representing the last element in the container;
const_reference back() const noexcept;

2. Code example - accessing the first and last elements of the 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;

    std::cout << "首元素 : " << vec.front() << std::endl;
    std::cout << "尾元素 : " << vec.back() << std::endl;

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

	return 0;
};

Results of the :

1 2 3
first element: 1
last element: 3
Press any key to continue . . .

Insert image description here

Guess you like

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