c++---vector容器

c++---vector容器,第1张

1、vector基本概念

功能:vector数据结构和数组非常相似,也称为单端数组

vector与普通数组区别:

不同之处在于数组是静态空间,而vector可以动态扩展

动态扩展:

并不是在原有的空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间。

vector迭代器是支持随机访问的迭代器

2、vector构造函数

功能:创建vector容器

函数原型:

  • vector v; 采用模板实现类实现,默认构造函数
  • vector(v.begin(),v.end());将v.begin()和v.end()区间中的元素拷贝给本身
  • vector(n,elem);构造函数将n个elem拷贝给本身
  • vector(const vector &vec);拷贝构造函数
void printVector(vector&v)
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	vectorv2(v1.begin(), v1.end());
	printVector(v2);

	vectorv3(10, 100);
	printVector(v3);

	vectorv4(v3);
	printVector(v4);
}

	
int main()
{
	cout << "test01" << endl;
	test01();
	
	system("pause");
	return 0;
}

3、vector赋值 *** 作

函数原型:

  • vector& operator=(const vector &vec);重载等号运算符
  • assign(beg ,end);将[beg,end]区间中的数据拷贝给本身
  • assign(n,elem);将n个elem拷贝给本身
void printVector(vector&v)
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	vectorv2;
	v2 = v1;
	printVector(v2);

	vector v3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);

	vector v4;
	v4.assign(10, 100);
	printVector(v4);

}

	
int main()
{
	cout << "test01" << endl;
	test01();
	
	system("pause");
	return 0;
}

4、vector容量和大小

函数原型:

  • empty();判断容器是否为空
  • capacity();容器的容量
  • size();返回容器中元素的个数
  • resize(int num);重新指定容器的长度为num,若容器变长,则以默认值填充新位置,若容器变短,则末尾超出容器长度的元素被删除
  • resize(int num, elem);重新指定容器的长度为num,若容器变长,则以elem填充新位置,若容器变短,则末尾超出容器长度的元素被删除
void printVector(vector&v)
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	if (v1.empty())
	{
		cout << "有元素" << endl;
	}
	else {
		cout << "Kong" << endl;
	}

	cout << v1.capacity() << endl;
	cout << v1.size() << endl;
	v1.resize(20);
	printVector(v1);
	cout << v1.capacity() << endl;
	cout << v1.size() << endl;
	v1.resize(25, 1);
	printVector(v1);
	cout << v1.capacity() << endl;
	cout << v1.size() << endl;
}

	
int main()
{
	cout << "test01" << endl;
	test01();
	
	system("pause");
	return 0;
}
test01
0 1 2 3 4 5 6 7 8 9
Kong
13
10
0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0
20
20
0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1
30
25
请按任意键继续. . .


 5、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,count_iterator end);删除迭代器从start到end之间的元素
  • clear();删除容器中所有元素
void printVector(vector&v)
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);
	v1.push_back(3);
	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());
	printVector(v1);*/
	v1.clear();
	printVector(v1);


	
}

	
int main()
{
	cout << "test01" << endl;
	test01();
	
	system("pause");
	return 0;
}

6、vector数据存取

函数原型:

  • at(int idx);返回索引idx所指的数据
  • operator[ idx];返回索引idx所指的数据
  • front();返回容器中第一个数据元素
  • back();返回容器中最后一个数据元素
void printVector(vector&v)
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);
	cout << v1.at(1) << endl;
	cout << v1[1] << endl;
	cout << v1.front() << endl;
	cout << v1.back() << endl;

}

	
int main()
{
	cout << "test01" << endl;
	test01();
	
	system("pause");
	return 0;
}

7、vector互换容器

函数原型:

  • swap(vec);将vec与本身的元素互换
void printVector(vector&v)
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);
	vector v2;
	v2.push_back(10);
	v2.push_back(20);
	v2.push_back(30);
	v2.push_back(40);
	v2.push_back(50);
	printVector(v2);
	v1.swap(v2);
	printVector(v1);
	printVector(v2);
}

//实际用途
void test02()
{
	vector 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(v).swap(v);//vector(v)匿名对象,不是v,但是是按照v的大小进行初始化的
	cout << "v的容量 = " << v.capacity() << endl;
	cout << "v的大小 = " << v.size() << endl;
}
	
int main()
{
	cout << "test01" << endl;
	//test01();
	test02();
	system("pause");
	return 0;
}

8、vector预留空间

功能:减少vector在动态扩展容量时的扩展次数

函数原型:

  • reserve(int len);容器预留len个元素长度,预留位置不初始化,元素不可访问
void printVector(vector&v)
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{

	vector v1;
	v1.reserve(100000);//预留100000的空间,减少扩展内存的次数
	int num = 0;
	int * p = NULL;
	for (int i = 0; i < 100000; i++)
	{
		v1.push_back(i);
		if (p != &v1[0])
		{
			p = &v1[0];
			num++;
		}
	}
	cout << num << endl;
	cout << v1.capacity() << endl;
	
}

	
int main()
{
	cout << "test01" << endl;
	test01();
	
	system("pause");
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/717307.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-25
下一篇2022-04-25

发表评论

登录后才能评论

评论列表(0条)

    保存