#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<int> vt;
int main()
{
 int i;
 vt v1(10); //定义向量10个元素,注意每个元素初始化为了0
 for (i = 0; i < 10; i++)
  cout << v1[i] << " ";
 cout << endl;
//---输出0 0 0 0 0 0 0 0 0 0 
 int a[9] = { 1, 3, 2, 4, 6, 7, 8, 3,6 };
 vt v2(a, a + 8);//将数组a的第一个元素到第8个元素作为v2初始化值
 for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
  cout << *it << "  ";
 cout << endl;
//使用迭代器iterator遍历的方法输出向量元素,输出为1 3 2 4 6 7 8 3
//插入函数insert(),能在向量中指定位置插入一个元素
//--------------------------------------------------------------------------------------
//插入前结果  1 3 2 4 6 7 8 3
 v2.insert(v2.begin(), 4);//在v2开头(下标为0的元素)前面插入数据4
//插入后结果  4 1 3 2 4 6 7 8 3
 v2.insert(v2.begin() + 2, 5);//把数据5插入到下标为2处
//插入后结果  4 1 5 3 2 4 6 7 8 3
 v2.insert(v2.end(), 18);//在v2最后插入数据18
//插入后结果  4 1 5 3 2 4 6 7 8 3 18
//---------------------------------------------------------------------------------------
//删除函数erase(),能在向量中指定位置插入几个元素
    vt v3(a, a + 8);
 v3.erase(v3.begin()+2);//删除下标为2的元素
 for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++)
  cout << *it << "  ";
  cout << endl;
//----输出结果1 3 4 6 7 8 3
 v3.erase(v3.begin() + 1, v3.begin() +3);//左闭右开删除下标为1(包括1)到下标为3(不保存3)之间元素
 for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++)
  cout << *it << "  ";
 cout << endl;
//----输出结果1 6 7 8 3
 v3.erase(v3.end()-1);//删除末尾元素,由于右开特性,*(v3.end()-1)才是左后一个元素
 for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++)
  cout << *it << "  ";
 cout << endl;
//----输出结果1 6 7 8
//---调用clear()函数清空向量
 v3.clear();
 cout << v3.size() << endl; //输出为0
//------------------------------------------------------------------------------------------
//--sort()函数对向量进行(默认)升序排序包含于头文件#include<algorithm>中
 vt v4(a, a + 9);
 sort(v4.begin(), v4.end());
 for (i = 0; i < 9; i++)
  cout << v4[i] << " ";
 cout << endl;
//输出1 2 3 3 4 6 6 7 8
}

最新文章

  1. OC中的内存管理
  2. ICMP type code 对应表(转)
  3. javascript实例学习之一——联动下拉框
  4. Codeforces Gym 100015C City Driving 离线LCA
  5. [CF]codeforces round 369(div2)
  6. Use windows batch script to create menu
  7. Dictionary通过Value找到它的key
  8. 对象比较中 &quot;相等性&quot;和&quot;同一性&quot; 生动地解释
  9. JavaScript设计模式_02_策略模式
  10. 剑指 offer代码解析——面试题39推断平衡二叉树
  11. Win7删除右键菜单中“图形属性”和“图形选项”
  12. Android 第四次作业
  13. GALV_maptravel研究分析(2)
  14. RabbitMQ的应用场景以及基本原理简介
  15. 《Miracle_House》团队项目系统设计改进
  16. 对象奔驰E2000
  17. python 文件描述符
  18. Angular4.x 中的服务
  19. springboot学习章节代码-Spring MVC基础
  20. [UE4]响应鼠标点击

热门文章

  1. SSM之秒杀系统
  2. poj 1716 Integer Intervals (差分约束 或 贪心)
  3. BZOJ 2223 [Coci 2009]PATULJCI | 主席树练习 (好像是个权限题啊)
  4. CF992E Nastya and King-Shamans 解题报告
  5. [TJOI2007] 线段 (动态规划)
  6. hibernate用注解的方式实现orm
  7. HDU 1171Big Event in HDU(转01背包)
  8. 【CF1020E】Sergey&#39;s problem(构造)
  9. 创建型设计模式之工厂模式(Abstract Factory)
  10. [ CodeVS冲杯之路 ] P3145