3.3 STL commonly used containers -- 2.vector container

2. vector container

2.1 Basic concept of vector

Function:

  • The vector data structure is very similar to an array , also known as a single-ended array

The difference between vector and ordinary array:

  • The difference is that the array is a static space, and the vector can be dynamically expanded

Dynamic expansion:

  • It is not to continue the new space after the original space, but to find a larger memory space , and then copy the original data to the new space to release the original space
  • The iterator of the vector container is an iterator that supports random access

2.2 vector constructor

Function description:

  • Create a vector container

Function prototype:

  • vector v; // use template implementation class implementation, default constructor
  • vector(v.begin(), v.end()); // copy the elements in the interval v[begin(), end()) to itself
  • vector(n, elem); // The constructor copies n elems to itself
  • vector(const vector &vec); // copy constructor

Code example:

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

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

void test_01()
{
    
    
	// 1. 采用模板实现类实现,默认构造参数
	vector<int> v1; // 无参构造
	for (int i = 0; i < 10; i++)
	{
    
    
		v1.push_back(i);
	}
	printVector(v1);
	
	// 2. 将v[begin(), end())区间中的元素拷贝给本身
	vector<int> v2(v1.begin(), v1.end());
	printVector(v2);

	// 3. 构造函数将n个elem拷贝给本身
	vector<int> v3(10, 100);
	printVector(v3);

	// 4.拷贝构造函数
	vector<int> v4(v3);
	printVector(v4);
}

int main() 
{
    
    
	test_01();
	system("pause");
	return 0;
}
// 0 1 2 3 4 5 6 7 8 9
// 0 1 2 3 4 5 6 7 8 9
// 100 100 100 100 100 100 100 100 100 100
// 100 100 100 100 100 100 100 100 100 100

2.3 Vector assignment operation (=, assign)

Function description:

  • Assign a value to the vector container

Function prototype:

  • vector& operator=(const vector &vec); // overloaded equals operator
  • assign(beg, end); // assign the data copy in the range [beg, end) to itself
  • assign(n, elem); //Assign n copies of elem to itself

Sample code:

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

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

// 赋值操作
void test_01()
{
    
    
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
    
    
		v1.push_back(i);
	}
	printVector(v1);
	// 1. 重载符号操作符
	vector<int> v2;
	v2 = v1;
	printVector(v2);
	
	// 2. assign(begin, end) //将[begin, end)区间中的数据拷贝赋值给本身
	vector<int> v3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);

	// 3.assign(n, elem) // 将n个elem拷贝赋值给本身
	vector<int> v4;
	v4.assign(10, 100);
	printVector(v4);
}
int main() 
{
    
    
	test_01();
	system("pause");
	return 0;
}
// 0 1 2 3 4 5 6 7 8 9
// 0 1 2 3 4 5 6 7 8 9
// 0 1 2 3 4 5 6 7 8 9
// 100 100 100 100 100 100 100 100 100 100

2.4 Vector capacity and size (empty, capacity, size, resize)

Function description:

  • Capacitance and size operations on vector containers

Function prototype:

  • empty(); // Check if the container is empty
  • capacity(); //The capacity of the container
  • size(); // returns the number of elements in the container
  • resize(int num); // Re-designate the length of the container as num, if the container side is long, the new position will be filled with the default value, if the container becomes shorter, the elements at the end exceeding the container length will be deleted
  • resize(int num, elem); // Re-designate the length of the container as num, if the container becomes longer, fill the new position with the value of elem, if the container becomes shorter, delete the elements at the end that exceed the length of the container

Sample code:

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

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

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

	if (v1.empty())
	{
    
    
		cout << "v1为空" << endl;
	}
	else
	{
    
    
		cout << "v1不为空" << endl;
		cout << "v1的容量 = " << v1.capacity() << endl;
		cout << "v1的大小为 = " << v1.size() << endl;
	}

	//resize重新指定大小, 若指定的更大,默认用0填充新位置,可以利用重载版本替换默认填充
	v1.resize(15, 10);
	printVector(v1);
	// resize重新指定大小,若指定更小,超出部分元素删除
	v1.resize(5);
	printVector(v1);
}
int main()
{
    
    
	test_01();
	system("pause");
	return 0;
}
// 0 1 2 3 4 5 6 7 8 9
// v1不为空
// v1的容量 = 13
// v1的大小为 = 10
// 0 1 2 3 4 5 6 7 8 9 10 10 10 10 10
// 0 1 2 3 4

2.5 vector insertion and deletion (push_back, pop_back, insert, erase, clear)

Function description:

  • Insert and delete operations on vector containers

Function prototype:

  • push_back(ele); // insert element ele at the end
  • pop_back(); // delete the last element
  • insert(const_iterator pos, ele); // the iterator points to position pos to insert element ele
  • insert(const_iterator pos, int count, ele); // The iterator points to position pos to insert count elements
  • erase(const_iterator pos); // delete the element pointed to by the iterator
  • erase(const_iterator start, const_iterator end); // Delete the elements between start and end of the iterator
  • clear(); // delete all elements in the container

code example

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

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

// 插入和删除
void test_01()
{
    
    
	vector<int> v1;
	// 尾插
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	printVector(v1);

	// 尾删
	v1.pop_back();
	printVector(v1);
	// 插入
	v1.insert(v1.begin(), 100);
	printVector(v1);
	v1.insert(v1.begin(), 2, 1000);
	printVector(v1);
	// 删除
	v1.erase(v1.begin());
	printVector(v1);
	// 清空
	v1.erase(v1.begin(), v1.end());
	v1.clear();
	printVector(v1);
}

int main() 
{
    
    
	test_01();
	system("pause");
	return 0;
}
// 10 20 30 40 50
// 10 20 30 40
// 100 10 20 30 40
// 1000 1000 100 10 20 30 40
// 1000 100 10 20 30 40
// 

2.6 vector data access (at, [], front, back)

Function description:

  • Access to data in vector

Function prototype:

  • at(int index); // Return the data pointed to by the index idx
  • operator[]; // Return the data pointed to by the index idx
  • front(); // returns the first data element in the container
  • back(); // return the last data element in the container

Code example:

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

void test_01()
{
    
    
	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的第一个元素为: " << v1.front() << endl;
	cout << "v1的最后一个元素为:" << v1.back() << endl;
}

int main()
{
    
    
	test_01();
	system("pause");
	return 0;
}
// 0 1 2 3 4 5 6 7 8 9
// 0 1 2 3 4 5 6 7 8 9
// v1的第一个元素为: 0
// v1的最后一个元素为:9

2.7 vector swap container (swap)

Function description:

  • To achieve the exchange of elements in the two containers
  • Clever use can achieve a practical effect of shrinking memory

Function prototype:

  • swap(vec); // swap vec with its own elements

Code example:

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

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

void test_01()
{
    
    
	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);
}

void test_02()
{
    
    
	// 创建vector容器,存放一个比较大的数
	vector<int> v;
	for (int i = 0; i < 100000; i++)
	{
    
    
		v.push_back(i);
	}
	cout << "1. v的容量为:" << v.capacity() << endl;
	cout << "1. v的大小为:" << v.size() << endl;
	v.resize(3);
	// resize后,容量不变,造成资源浪费
	cout << "2. v的容量为:" << v.capacity() << endl;
	cout << "2. v的大小为:" << v.size() << endl;
	// 收缩内存  
	// vector<int>(v) 匿名对象,拷贝构造
	vector<int>(v).swap(v); 
	cout << "3. v的容量为:" << v.capacity() << endl;
	cout << "3. v的大小为:" << v.size() << endl;
}

int main()
{
    
    
	test_01();
	test_02();
	system("pause");
	return 0;
}
// 互换容器前
// 0 1 2 3 4 5 6 7 8 9
// 10 9 8 7 6 5 4 3 2 1
// 互换容器
// 10 9 8 7 6 5 4 3 2 1
// 0 1 2 3 4 5 6 7 8 9
// 1. v的容量为:138255
// 1. v的大小为:100000
// 2. v的容量为:138255
// 2. v的大小为:3
// 3. v的容量为:3
// 3. v的大小为:3

2.8 vector reserved space (reserve)

Function description:

  • Reduce the number of expansions of vector when dynamically expanding capacity

Function prototype:

  • reserve(int len); // Reserve len element space in the container, the reserved position is not initialized, and the element is inaccessible

Code example:

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

void test_01()
{
    
    
	vector<int> v;
	// 预留空间
	v.reserve(100000);
	// num统计扩展空间次数
	int num = 0;
	int* p = NULL;
	for (int i = 0; i < 10000; i++)
	{
    
    
		v.push_back(i);
		if (p != &v[0])
		{
    
    
			p = &v[0];
			num++;
		}
	}
	cout << "num: " << num << endl;
}
int main()
{
    
    
	test_01();
	system("pause");
	return 0;
}
// num: 1

Guess you like

Origin blog.csdn.net/yewumeng123/article/details/131145904