栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ vector容器

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ vector容器

#include 
#include 
#include 	
using namespace std;

int main() {
	vector  ret;//默认构造
	//前端封闭后端修改
	ret.push_back(1);
	ret.push_back(2);
	ret.push_back(3);
	for (auto a : ret)
		cout << a << " ";
	cout << endl;
	ret.pop_back();
	for (auto a : ret)
		cout << a << " ";
	cout << endl;
	vector  v1(10, 100);//10个100
	for (auto a : v1)
		cout << a << " ";
	cout << endl;
	vector  v2(v1);
	for (auto a : v2)
		cout << a << " ";
	cout << endl;
}

赋值
#include 
#include 
#include 	
using namespace std;

int main() {
	vector  ret1;
	for (int i = 0; i < 10; i++)
		ret1.push_back(i + 1);
	for (auto a : ret1)
		cout << a << " ";
	cout << endl;
	//赋值
	vector  v1;
	v1 = ret1;
	for (auto a : v1)
		cout << a << " ";
	cout << endl;
	vector  v2;
	v2.assign(v1.begin(), v1.end());//[1, ... , 10+1)
	for (auto a : v2)
		cout << a << " ";
	cout << endl;
	vector  v3;
	v3.assign(10, 1);//这个是赋值
	for (auto a : v3)
		cout << a << " ";
	cout << endl;
}

#include 
#include 
#include 	
using namespace std;

int main() {
	//是否为空empty
	vector  a1;
	a1.empty()  ? cout << "容器为空" : cout << "容器不为空";
	//判断大小
	cout << endl;
	for (int i = 0; i < 10; i++)
		a1.push_back(i);
	a1.empty() ? cout << "容器为空" : cout << "容器不为空,";
	cout << "容器的容量是:" << a1.capacity() ;
	cout << ",容器大小是:" << a1.size() << endl;
	//重新指定大小
	a1.resize(5);
	for (auto a : a1)
		cout << a << " ";
	cout << endl;
	a1.resize(15);//不指定默认值为0
	for (auto a : a1)
		cout << a << " ";
	cout << endl;
	a1.resize(20,10);//指定默认值
	for (auto a : a1)
		cout << a << " ";
}

插入和删除
#include 
#include 
#include 	
using namespace std;

class show {
public:
	void Show(vector  aa) {
		for (int a : aa)
			cout << a << " ";
		cout << endl;
	}
};
int main() {
	show aaa;
	vector  a1;
	for (int i = 0; i < 10; i++)
		a1.push_back(i);
	aaa.Show(a1);
	//删除时pop_back(),不再举例
	//插入
	a1.insert(a1.begin(), 1);
	aaa.Show(a1);
	a1.insert(a1.begin(),2 ,3);//两个3
	aaa.Show(a1);
	a1.erase(a1.begin());
	aaa.Show(a1);
	a1.erase(a1.begin(), a1.end());
	//等于 a1.clear();
	a1.empty() ? cout << "容器为空" : cout << "容器不为空,";
	aaa.Show(a1);
}

利用swap交换
#include 
#include 
#include 	
using namespace std;

class show {
public:
	void Show(vector  aa) {
		for (int a : aa)
			cout << a << " ";
		cout << endl;
	}
};
void ceshi(vector  a1) {
	show cs;
	cs.Show(a1);
}
int main() {
	vector  a1;
	vector  a2;
	for (int i = 0; i < 1000000; i++)
		a1.push_back(i);
	for (int i = 9; i >= 0; i--)
		a2.push_back(i);
	ceshi(a1);
	ceshi(a2);
	cout << endl;
	//收缩内存
	vector (m);//匿名对象
	vector (m).swap(m);
	a1.swap(a2);
	ceshi(a1);
	ceshi(a2);
}

【不执行】

预留空间
#include 
#include 
#include 	
using namespace std;

class show {
public:
	void Show(vector  aa) {
		for (int a : aa)
			cout << a << " ";
		cout << endl;
	}
};
void ceshi(vector  a1) {
	show cs;
	cs.Show(a1);
}
int main() {
	//预留空间
	vector  a1;
	int num = 0;
	int* p = NULL;
	//利用reserve预留空间
	//有这行就只有1次动态拓展
	a1.reserve(1000000);
	for (int i = 0; i < 1000000; i++) {
		a1.push_back(i);
		if (p != &a1[0]) {
			//插满后又指向首地址重新开辟内存
			p = &a1[0];
			num++;
		}
	}
	//否则35次
	cout << "共动态拓展" << num << "次" << endl;

}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/928866.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号