c++_vector

本文详细介绍了C++中vector容器的使用方法,特别是assign方法的两种使用方式,包括通过迭代器指定范围赋值和指定元素数量及值进行赋值。并通过示例展示了如何访问和操作vector中的元素,如访问特定元素、访问首元素和尾元素,以及如何在vector中插入元素。同时,还提供了遍历vector的不同方法,并解释了如何使用vector的size方法获取元素数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//IDE是vs2013.

vector矢量是c++里一种容器。

assign分配新的内容到容器里,取代旧的内容。

assign用法有两种。

void assign (InputIterator first, InputIterator last);
void assign (size_type n, const value_type& val);

#include "stdafx.h"
#include<iostream>
#include<vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> first;
	vector<int> second;
	int a[5] = { 1,2, 3, 4, 5};
	first.assign(a, a + 5); //assign(InputIterator first, InputIterator last);
	second.assign(3, 1);//assign (size_type n, const value_type& val)
	for (auto p : first)
	{
		cout << p << ' ';
	}
	cout << endl;
	for (auto p :second)
	{
		cout << p << ' ';
	}
	cout << endl;
	system("pause");
	return 0;
}
结果:


myvector.assign(6,7);//赋值

myvector.push_back(6);//在末尾添加元素

myvector.pop_back();//移除最后一个元素

myvector.size();//返回vector中的元素个数

<pre name="code" class="cpp">#include <iostream>  
#include <vector>  
using namespace std;  
int _tmain(int argc, _TCHAR* argv[])  
{  
	vector<int> v;  
	vector<int>::iterator it;  
	int ar[5] = { 1, 2, 3, 4, 5 };  
	v.assign(ar,ar+5);//赋值  
	//myvector.assign(6,7);//或者赋值6个7  
	v.push_back(6);//在末尾添加元素  
	v.pop_back();//移除最后一个元素  
	cout << v.size()<<endl;//返回vector中的元素个数  
	for (it =v.begin(); it < v.end(); it++)  
	{  
		std::cout << *it<<' ';  
	}  
	cout<<endl;  
	system("pause");  
	return 0;  
}  

 

 运行结果: 

=============

v.at(i);//访问vector中的i个元素

v.front();//访问vector第一个元素
v.back();//访问vector最后一个元素
v.insert(const_iterator position,const value_type& val); 
//insert(位置,值)在vector某位置插入一个元素
v.insert(const_iterator position, size_type n, const value_type& val);
//insert(位置,数量n,值)在vector某个位置插入n个元素
v.insert (const_iterator position, 迭代器首地址InputIterator first, 迭代器尾InputIterator last);
//insert(位置,迭代器首地址,迭代器尾)

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> v;
	vector<int>::iterator it;
	int ar[3] = { 1, 2, 3 };
	it = v.begin();
	it=v.insert(it, 5);// insert(位置,值)
	it=v.insert(it, 1, 4);//insert(位置,数量n,值)
	it=v.insert(it,ar,ar+3 );//insert(位置,迭代器首地址,迭代器尾)
	cout <<"front:"<< v.front()<<endl;
	cout << "back:" << v.back() << endl;
	for (auto p : v)
	{
	cout << p << ' ';
	}
	cout << endl;
	system("pause");
	return 0;
}

==================

遍历vector的方法

<pre name="code" class="cpp">#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> v;
	vector<int>::iterator it;
	int ar[5] = { 1, 2, 3, 4, 5 };
	int i=0;
	it=v.begin();
	it=v.insert(it,ar,ar+5);
	for (auto p : v)//第一种
	{
	cout << p << ' ';
	}
	cout << endl;
	for (i = 0; i < v.size(); i++)//第二种
	{
		cout << v.at(i) << ' ';
		//cout << v[i] << ' ';
	}
	cout << endl;
	system("pause");
	return 0;
}


 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值