vector的使用

1.vector的使用
vector学习时一定要学会查看文档:vector的文档介绍,vector在实际中非常的重要,在实际中我们熟悉常见的接口就可以,下面列出了哪些接口是要重点掌握的。

2.vector的定义
在这里插入图片描述

// constructing vectors
#include <iostream>
#include <vector>
int main ()
{
 // constructors used in the same order as described above:
 std::vector<int> first; // empty vector of ints
 std::vector<int> second (4,100); // four ints with value 100
 std::vector<int> third (second.begin(),second.end()); // iterating through second
 std::vector<int> fourth (third); // a copy of third
 // 下面涉及迭代器初始化的部分,我们学习完迭代器再来看这部分
 // the iterator constructor can also be used to construct from arrays:
 int myints[] = {16,2,77,29};
 std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
 std::cout << "The contents of fifth are:";
 for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
 std::cout << ' ' << *it;
 std::cout << '\n';
 return 0; }

3.vector iterator 的使用
在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <vector>
using namespace std;
void PrintVector(const vector<int>& v) {
 // const对象使用const迭代器进行遍历打印
 vector<int>::const_iterator it = v.begin();
 while (it != v.end())
 {
 cout << *it << " ";
 ++it;
 }
 cout << endl; }
int main()
{
 // 使用push_back插入4个数据
 vector<int> v;
 v.push_back(1);
 v.push_back(2);
 v.push_back(3);
 v.push_back(4);
 // 使用迭代器进行遍历打印
 vector<int>::iterator it = v.begin();
 while (it != v.end())
 {
 cout << *it << " ";
 ++it;
 }
 cout << endl;
 // 使用迭代器进行修改
 it = v.begin();
 while (it != v.end())
 {
 *it *= 2;
 ++it;
 }
 // 使用反向迭代器进行遍历再打印
 vector<int>::reverse_iterator rit = v.rbegin();
 while (rit != v.rend())
 {
 cout << *rit << " ";
 ++rit;
 }
 cout << endl;
 PrintVector(v);
 return 0; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值