0
点赞
收藏
分享

微信扫一扫

vector用法

一条咸鱼的干货 2022-01-06 阅读 47
c++

目录

构造对象

  • 定义一个空元素的vector对象
#include <iostream>
#include <vector>

int main()
{
	std::vector<int> vecInt;
}
  • 拷贝定义一个vector对象,元素与另一vector对象相同且相等
#include <iostream>
#include <vector>

int main()
{
	std::vector<int> vecInt1;
	vecInt1.push_back(1);
	vecInt1.push_back(2);

	std::vector<int> vecInt2(vecInt1);
	std::vector<int> vecInt3 = vecInt1;
	std::vector<int> vecInt4(vecInt1.begin(),vecInt1.end());
}
  • 定义一个元素数量为nNum(容量也为nNum)的vector对象;int类型默认值为0,std::string类型默认值为""
#include <iostream>
#include <vector>
#include <string>

int main()
{
	int nNum = 10;
	std::vector<std::string> vecData(nNum);
}
  • 定义一个元素数量为nNum(容量也为nNum),默认值为"你好世界"的vector对象
#include <iostream>
#include <vector>
#include <string>

int main()
{
	int nNum = 10;
	std::string strValue = "你好世界";
	std::vector<std::string> vecData(nNum, strValue);
}
  • 定义时初始化元素
#include <iostream>
#include <vector>

int main()
{
	std::vector<int> vecData = { 1,2,3,4 };
}
举报

相关推荐

0 条评论