The 123rd learning record: C++ improvement: STL-vector container (below) (dark horse teaching video)

vector insertion and deletion

Function description:
Insert and delete operations on vector containers

Function prototype:

push_back(ele); //尾部插入元素ele
pop_back(); //删除最后一个元素
insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele
insert(const_iterator pos, int count ,ele); //迭代器指向位置pos插入count个元素ele
erase(const_iterator pos); //删除迭代器指向的元素
erase(const_iterator start, const_iterator end); //删除迭代器从start到end之间的元素
clear(); //删除容器中所有元素
#include<iostream>
using namespace std;
#include<vector>

//vector插入和删除
/*
push_back(ele); //尾部插入元素ele
pop_back(); //删除最后一个元素
insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele
insert(const_iterator pos, int count ,ele); //迭代器指向位置pos插入count个元素ele
erase(const_iterator pos); //删除迭代器指向的元素
erase(const_iterator start, const_iterator end); //删除迭代器从start到end之间的元素
clear(); //删除容器中所有元素
*/


void printVector(vector<int>& v)
{
    
    
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
    
    
	vector<int>v1;
	//尾插
	v1.push_back(10);
	v1.push_back(40);
	v1.push_back(30);
	v1.push_back(50);
	v1.push_back(20);

	//遍历
	printVector(v1);

	//尾删
	v1.pop_back();
	printVector(v1);

	//插入  第一个参数是迭代器
	v1.insert(v1.begin(), 100);
	printVector(v1);

	v1.insert(v1.begin(), 2, 333);
	printVector(v1);

	//删除
	v1.erase(v1.begin());
	printVector(v1);

	//清空
	//v1.erase(v1.begin(),v1.end());
	v1.clear();
	printVector(v1);
}

int main()
{
    
    
	test01();
	return 0;
}

insert image description here
Summary:
tail insert push_back
tail delete pop_back
insert insert (position iterator)
delete erase (position iterator)
clear clear

vector data access

Function description:
access to the data in the vector

Function prototype:

at(int idx); //返回索引idx所指的数据
operator[]; //返回索引idx所指的数据
front(); //返回容器中第一个数据元素
back(); //返回容器中最后一个数据元素
#include<iostream>
using namespace std;
#include<vector>

//vector容器 数据存取



void printVector(vector<int>& v)
{
    
    
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
    
    
	vector<int>v1;
	for (int i = 0; i < 10; ++i)
	{
    
    
		v1.push_back(i);
	}

	//利用[]方式访问数组中元素
	for (int i = 0; i < v1.size(); ++i)
	{
    
    
		cout << v1[i] << " ";
	}
	cout << endl;

	//利用at方式访问元素
	for (int i = 0; i < v1.size(); ++i)
	{
    
    
		cout << v1.at(i) << " ";
	}
	cout << endl;

	//获取第一个元素
	cout << "第一个元素为:" << v1.front() << endl;

	//获取最后一个元素
	cout << "最后一个元素为:" << v1.back() << endl;
}

int main()
{
    
    
	test01();
	return 0;
}

insert image description here
Summary:
In addition to using iterators to obtain elements in the vector container, [] and at can also
return the first element of the container to the front
and return the last element of the container

vector swap container

Function description:
realize the exchange of elements in two containers

Function prototype:
swap(vec); //Swap vec with its own elements

insert image description here

#include<iostream>
using namespace std;
#include<vector>

//vector容器互换

void printVector(vector<int>& v)
{
    
    
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
    
    
		cout << *it << " ";
	}
	cout << endl;
}

//1、基本使用

void test01()
{
    
    
	vector<int>v1;
	for (int i = 0; i < 10; ++i)
	{
    
    
		v1.push_back(i);
	}

	cout << "交换前:" << endl;
	printVector(v1);

	vector<int>v2;
	for (int i = 10; i > 0; --i)
	{
    
    
		v2.push_back(i);
	}

	printVector(v2);

	cout << "交换后:" << endl;
	v1.swap(v2);
	printVector(v1);
	printVector(v2);
}

//2、实际用途
//巧用swap可以收缩内存空间
void test02()
{
    
    
	vector<int>v;
	for (int i = 0; i < 100000; i++)
	{
    
    
		v.push_back(i);
	}

	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;

	v.resize(3); //重新指定大小
	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;

	//巧用swap收缩内存
	vector<int>(v).swap(v);
	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;

}




int main()
{
    
    
	test01();
	test02();
	return 0;
}

insert image description here
Summary: swap can make two containers interchangeable, which can achieve a practical effect of shrinking memory

Vector reserved space

Function description:
reduce the expansion times of vector when dynamically expanding capacity
Function prototype:
reserve(int len); // The container reserves len element length, the reserved position is not initialized, and the element is inaccessible

#include<iostream>
using namespace std;
#include<vector>

//vector容器 预留空间


void printVector(vector<int>& v)
{
    
    
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
    
    
	vector<int>v;
	
	//int num = 0;//统计开辟次数
	//int* p = NULL;
	//for (int i = 0; i < 100000; ++i)
	//{
    
    
	//	v.push_back(i);
	//	if (p != &v[0])
	//	{
    
    
	//		p = &v[0];
	//		num++;
	//	}
	//}
	//cout << "num = " << num << endl;//30

	//利用reserve预留空间
	v.reserve(100000);

	int num = 0;//统计开辟次数
	int* p = NULL;
	for (int i = 0; i < 100000; ++i)
	{
    
    
		v.push_back(i);
		if (p != &v[0])
		{
    
    
			p = &v[0];
			num++;
		}
	}
	cout << "num = " << num << endl;//1
}

int main()
{
    
    
	test01();
	return 0;
}

Summary: If the amount of data is large, you can use reserve to reserve space at the beginning

Guess you like

Origin blog.csdn.net/weixin_45694614/article/details/132155799