vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的.

用法:

1. 首先在程序开头处加上#include<vector>以包含所需要的类文件vector

还有vector 是属于namespace std;

  2.函数调用 : 比如 vector <int> c

  (1)c.at(idx)   传回索引idx所指的数据,如果idx越界,抛出out_of_range。

  (2)c.front()   传回第一个数据。

  (3)c.back()   传回最后一个数据,不检查这个数据是否存在

  (4)c.clear()      移除容器中所有数据。

  (5)c.empty()    判断容器是否为空。

  (6)c.erase(pos)  删除pos位置的数据,传回下一个数据的位置。
      c.erase(beg,end)删除[beg,end)区间的数据,传回下一个数据的位置。

  (7)c.capacity()  返回容器中数据个数。

      c.size()返回容器中实际数据的个数。

  (8)c.pop_back() 删除最后一个数据。
      c.push_back(elem)在尾部加入一个数据。

  (9)c.insert(pos,elem)在pos位置插入一个elem拷贝,传回新数据位置。
      c.insert(pos,n,elem)在pos位置插入n个elem数据。无返回值。
      c.insert(pos,beg,end)在pos位置插入在[beg,end)区间的数据。无返回值。

  (10)c.resize(num)重新指定队列的长度。
        c.reserve(num)保留适当的容量。

  3.访问vector中的数据

使用两种方法来访问vector。

1、   vector::at()

2、   vector::operator[]

operator[]主要是为了与C语言进行兼容。它可以像C语言数组一样操作。但at()是我们的首选,因为at()进行了边界检查,如果访问超过了vector的范围,将抛出一个例外。由于operator[]容易造成一些错误,所有我们很少用它,下面进行验证一下:

 分析下面的代码:

 vector<int> v;

 v.reserve();

 for(int i=; i<; i++)
v.push_back(i); try
{
int iVal1 = v[]; // not bounds checked - will not throw int iVal2 = v.at(); // bounds checked - will throw if out of range
} catch(const exception& e)
{
cout << e.what();
}

我们使用reserve()分配了10个int型的空间,但并不没有初始化。

你可以在这个代码中尝试不同条件,观察它的结果,但是无论何时使用at(),都是正确的。

  4.迭代器的使用    

vector<int>::iterator it; 

for(it=vec.begin();it!=vec.end();it++)
cout<<*it<<endl

  5.算法 

  (1) 使用reverse将元素翻转:需要头文件#include<algorithm>

        reverse(vec.begin(),vec.end());将元素翻转(在vector中,如果一个函数中需要两个迭代器,

     一般后一个都不包含.)

  (2)使用sort排序:需要头文件#include<algorithm>,

    sort(vec.begin(),vec.end());(默认是按升序排列,即从小到大).

    可以通过重写排序比较函数按照降序比较,如下:

    定义排序比较函数:

    bool Comp(const int &a,const int &b)
    {
        return a>b;
    }
    调用时:sort(vec.begin(),vec.end(),Comp),这样就降序排序。

c++ vector 的使用的更多相关文章

  1. c++ vector 使用

    1. 包含一个头文件: 1 #include <vector> 2. 申明及初始化: std::vector<int> first; // empty vector of in ...

  2. Vector Tile

    Mapbox Vector Tile Specification A specification for encoding tiled vector data. <?XML:NAMESPACE ...

  3. ArrayList、Vector、LinkedList的区别联系?

    1.ArrayList.Vector.LinkedList类都是java.util包中,均为可伸缩数组. 2.ArrayList和Vector底层都是数组实现的,所以,索引数据快,删除.插入数据慢. ...

  4. ArrayList、Vector、HashMap、HashSet的默认初始容量、加载因子、扩容增量

    当底层实现涉及到扩容时,容器或重新分配一段更大的连续内存(如果是离散分配则不需要重新分配,离散分配都是插入新元素时动态分配内存),要将容器原来的数据全部复制到新的内存上,这无疑使效率大大降低. 加载因 ...

  5. Java中Vector和ArrayList的区别

    首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存放多个元素,能够维护元素的次序,并且允许元素的重复.3个具体 ...

  6. C++使用vector

    #include <iostream> #include <string> #include <vector> using namespace std; void ...

  7. [LeetCode] Flatten 2D Vector 压平二维向量

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  8. C++ 数组array与vector的比较

    转:http://blog.csdn.net/yukin_xue/article/details/7391897 1. array 定义的时候必须定义数组的元素个数;而vector 不需要: 且只能包 ...

  9. vector定义初始化

    头文件 #include<vector> using std::vector; vector<T> v1; vector<T> v2(v1); vector< ...

  10. vector迭代器用法

    #include<iostream> #include<vector> using namespace std; int main() { vector<int> ...

随机推荐

  1. c# 如何使用wlanapi连接电脑到wifi

    http://www.codeproject.com/Articles/72105/Manage-WiFi-with-Native-API-WIFI-on-Windows-XP-SP Introduc ...

  2. Resumable Media Uploads in the Google Data Protocol

    Eric Bidelman, Google Apps APIs team February 2010 Introduction The Resumable Protocol Initiating a ...

  3. Flatten Binary Tree to Linked List

    Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...

  4. Best Time to Buy and Sell Stock | & || & III

    Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...

  5. spring mvc 406 (Not Acceptable) json转换错误

    spring mvc通过@RequestMapping("/register")和@ResponseBody返回json格式的字符串时出现如下异常: The resource id ...

  6. Java for LeetCode 068 Text Justification

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  7. codeforces A. Group of Students 解题报告

    题目链接:http://codeforces.com/problemset/problem/357/A 题目意思:将一堆人分成两组:beginners 和 intermediate coders .每 ...

  8. GLSL

    变量修饰符 修饰符给出了变量的特殊含义,GLSL中有如下修饰符: ·const – 声明一个编译期常量. ·attribute– 随不同顶点变化的全局变量,由OpenGL应用程序传给顶点shader. ...

  9. The Unique MST(poj 1679)

    题意:求次小生成树,若权值和与最小生成树相等,输出"Not Unique!" :否则,输出mst /* 次小生成树 首先明白一点,次小生成树是由最小生成树改变一条边得来的,然后我们 ...

  10. 菜鸟学Linux命令:find命令 查找文件

    find命令是Linux下最常用的命令之一,灵活的使用find命令,你会发现查找文件变得十分简单. 命令格式 find [指定查找目录]  [查找规则(选项)]  [查找完后执行的动作] 参数规则 - ...