STL-vector container

Preface

When I first heard the teacher talk about this in the online class, it was a vector and an iterator. I just barely know that we can use vectors to dynamically create arrays, and iterators can move like pointers (or even think that iterators are pointers). However, after studying C++ in depth, I recently re-learned this knowledge for some reasons. I found relevant information on the Internet and in books, and finally realized it, so I wanted to use the blog to make some notes.

STL provides a set of templates that represent containers, iterators, function objects, and algorithms. A container is a unit similar to an array, which can store thousands of values. STL containers are homogeneous, that is, the types of stored values ​​are the same; algorithms are prescriptions for completing specific tasks (such as sorting an array or finding specific values ​​in a linked list); iterators can be used to traverse the objects of the container, and can traverse Array pointers are similar and are generalized pointers; function objects are objects similar to functions, which can be class objects or function pointers (including function names, because function names are used as pointers). STL makes it possible to construct various containers (including arrays, queues, and linked lists) and perform various operations (including searching, sorting, and random arrangement).
Alex Stepanov and Meng Lee developed STL in Hewlett-Packard laboratory and released its implementation in 1994. The ISO/ANSIC++ committee voted to make it an integral part of the C++ standard. STL is not object-oriented programming, but-a different programming model-generic programming. This makes STL interesting in terms of function and method.
The above is taken from "C++ Primer Plus (6th Edition) Chinese Version"

vector

Vector is part of the C++ standard template library, Chinese occasionally translated as "container", but it is not accurate. It is a multifunctional template class and function library capable of operating a variety of data structures and algorithms. The reason why a vector is considered a container is that it can store various types of objects like a container. Simply put, a vector is a dynamic array that can store any type and can increase and compress data.
The above is taken from Baidu Encyclopedia

Use vector to dynamically add elements to add 10 elements to vec_num and clear all data
std::vector<int> vec_num;//声明名为vec_num的vector整型容器
	for (int i = 0; i < 10; i++) {
    
    //初始化容器元素
		vec_num.push_back(i + 1);//动态添加元素
	}
	vec_num.clear();//清空全部数据
vector delete element
Delete by value
std::vector<int> vec_num;//声明名为vec_num的vector整型容器
	for (int i = 0; i < 10; i++) {
    
    //初始化容器元素
		vec_num.push_back(i + 1);//动态添加元素
	}
	std::vector<int>::iterator iter = find(vec_num.begin(), vec_num.end(), 3);//按顺序删除第一个值为3的元素,若有多个值为3的元素则默认删除第一个
	vec_num.erase(iter);
Find function description

The find function is in the header file named algorithm, so to use it, please call this header file.

So how to use it

find(a,b,c), where a is the starting point of the search, b is the end of the search, and c is the value to be found.
In the container, it is expressed as find(iter1,iter2,x). It should be noted that iter1 and iter2 are iterators of the same container. When x is found, find will return an iterator at position x. If there are multiple elements with value x in the container, find will only return the first one.

Use find to delete the specified subscript element in the container
void deleteEle(vector<int>& vec,int index)
{
    
    
	vector<int>::iterator iter = vec.begin() + index;
	vec.erase(iter);
}
Points to note in the end() method in vector

In fact, end() is a very special place. For a brief explanation, here is a picture from "C++ Primer Plus (6th Edition) Chinese Version", as follows

Insert picture description here

It can be found intuitively from the figure that we think the container interval is within the range of 100-132, but in fact, there is still a super tail element in the container, and end() points to this super tail element. That is, things.end() in the figure does not point to 3, but the element of the empty character after it. So the total value range is [begin,end).
How to access 3? Obviously, only things.end()-1 is needed to move the iterator one step to the left.

Iterators and pointers

What is an iterator? It is a generalized pointer. In fact, it can be a pointer or
an object that can perform pointer-like operations on it, such as dereferencing (such as operator*( )) and incrementing (such as operator++( )).
The above is taken from "C++ Primer Plus (6th Edition) Chinese Version"

But in fact it just makes the behavior look like a pointer, but it's not actually a real pointer. The following code can reflect the difference.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    
    
	std::vector<int> vec_num;//声明名为vec_num的vector整型容器
	for (int i = 0; i < 10; i++) {
    
    //初始化容器元素
		vec_num.push_back(i + 1);//动态添加元素
	}
	for (vector<int>::iterator iter = vec_num.begin(); iter != vec_num.end(); iter++) {
    
    //由iter++可知iterator可像指针一样移动位置,但他们有着本质的不同
		cout << *(iter) << '\t';//解除引用,从此处可体现iterator有类似指针的一些性质(表现的像指针)
		printf("%p\t", iter) ;
		cout << *(vec_num.end() - 1);//从此处可看出end实际上指向的并不是我们初始化容器中的元素的最后一个,而是指向我们的最后一个元素后面一位,且不能直接访问
		//cout << *(vec_num.end());//若运行这句语句,即直接调用end()的话会抛出异常。
		cout << endl;
	}/*而从循环判断条件当中,vector.end()方法大有文章,end()字面理解应是所指的元素为容器的最后一个元素,
	 但其特殊的地方在于它比容器中所存的元素要多一个尾后元素(值为空),取值区间为[begin,end)
	 其优点暂且不知,但是如果不这样做,当begin()==end()的时候就无法判断是没有元素还是只有一个元素了
	 */
	cout<< endl;
	//从下面这段可以看出,指针移动时,其指向的内存地址也相应的改变,而iterator移动时,其地址并没有发生变化。
	int a[] = {
    
     1,2,3 };
	int* p_a = a;
	cout << *p_a << '\t';
	printf("%p\n", p_a);
	//cout << p_a<<endl;
	p_a++;
	cout << *p_a << '\t';
	printf("%p\n", p_a);
	//cout << p_a << endl;
	p_a++;
	cout << *p_a << '\t';
	printf("%p\n", p_a);
	//cout << p_a << endl;
	return 0;
}
After running the code, you can find that when the iterator moves, its address does not change, and every time the pointer moves, the address changes accordingly, so it is not a real pointer.

The above is my understanding of vector. If there is any mistake, I hope you can point it out.

Guess you like

Origin blog.csdn.net/WildSky_/article/details/104254353