Reference

Constructors

vector

Constructs a vector of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other vector.

Typedefs

allocator_type

A type that represents the allocator class for the vector object.

const_iterator

A type that provides a random-access iterator that can read a const element in a vector.

const_pointer

A type that provides a pointer to a const element in a vector.

const_reference

A type that provides a reference to a const element stored in a vector for reading and performing const operations.

const_reverse_iterator

A type that provides a random-access iterator that can read any const element in the vector.

difference_type

A type that provides the difference between the addresses of two elements in a vector.

两个元素在vector中的地址差,不是真实的元素地址差

iterator

A type that provides a random-access iterator that can read or modify any element in a vector.

pointer

A type that provides a pointer to an element in a vector.

reference

A type that provides a reference to an element stored in a vector.

reverse_iterator

A type that provides a random-access iterator that can read or modify any element in a reversed vector.

size_type

A type that counts the number of elements in a vector.

value_type

A type that represents the data type stored in a vector.

Member Functions

assign

Erases a vector and copies the specified elements to the empty vector.

清除原有的vector容器,然指定特定的元素到vector

例如:

vector<int>vec;

vec.assign(10,250);擦除原有容器的内容,插入10个值为250的元素

vec1(vec.rbegin,vec,rend);将vec1某个迭代范围内的元素插入到vec1

at

Returns a reference to the element at a specified location in the vector.

例如:

vec.at(3);返回第三个数的引用

back

Returns a reference to the last element of the vector.

如果vector为空,使用导致coredump)

begin

Returns a random-access iterator to the first element in the container.

如果vector为空,begin返回的迭代器与end返回的迭代器相同。

capacity

Returns the number of elements that the vector could contain without allocating more storage.

获取在不重新分配内存的情况下可容纳的元素最大数量

clear

Erases the elements of the vector.

清空整个容器

vec.clear();

empty

Tests if the vector container is empty.

end

Returns a random-access iterator that points just beyond the end of the vector.

返回的迭代器指向vector的“末端元素的下一个”。
通常称为超出末端迭代器(off-the-end iterator),表明它指向了一个不存在的元素。

erase

Removes an element or a range of elements in a vector from specified positions.

可通过迭代器清除某个元素或者某一迭代范围内的元素,返回原元素后一个元素在删除后的vector中的迭代器或者end()。

例如:

vector<int>::iterator iter = ++vec.begin();

vec.erase(iter);

vec.erase(vec.begin(),--vec.end());

front

Returns a reference to the first element in a vector.

如果vector为空,使用导致coredump

get_allocator

Returns an object to the allocator class used by a vector.

insert

Inserts an element or a number of elements into the vector at a specified position.

max_size

Returns the maximum length of the vector.

vector可容纳元素的最大数量,vs08在win64测试最大貌似是1073741823

pop_back

Deletes the element at the end of the vector.

push_back

Add an element to the end of the vector.

调用元素类型的拷贝构造函数以其值为模板初始化新变量,放入到vector的末尾。

rbegin

Returns an iterator to the first element in a reversed vector.

返回反向迭代器,指向vector的最后一个元素

rend

Returns an iterator to the end of a reversed vector.

reserve

Reserves a minimum length of storage for a vector object.

改变当前vector的预留容量,在vs08上测试,如果预留容量小于当前元素个数不会改变,在大于的时候才会改变。

resize

Specifies a new size for a vector.

在vs08上测试,如果当前的元素size大于这个resize值,会从头到尾截断resize大小,后面的元素舍弃,当前size元素小于的话

多出来的新元素都需以元素类型的default构造函数构造完成。

void resize(size_type n, value_type x);
将元素数量改为n,如果vector因此变大了,多出来的元素都是x的副本

size

Returns the number of elements in the vector.

swap

Exchanges the elements of two vectors.

交换两个vec

campare function(非成员函数)   这个时候就要注意元素的比较重载函数了

inline bool operator == (const vector<T> & ls, const vector<T> & rs)
非成员函数,判断两个vector是否相等。首先比较二者的元素个数是否相同;如果个数相同则遍历vector,调用元素的“==”运算符比较是否相同。

inline bool operator == (const vector<T> & ls, const vector<T> & rs)
非成员函数,判断两个vector是否相等。首先比较二者的元素个数是否相同;如果个数相同则遍历vector,调用元素的“==”运算符比较是否相同。

inline bool operator != (const vector<T> & ls, const vector<T> & rs)
非成员函数,判断两个vector是否不等。结果为对“==”运算符的取反。

inline bool operator < (const vector<T> & ls, const vector<T> & rs)
非成员函数,判断前者是否小于后者。遍历vector,返回第一对不满足“==”运算符的元素的“<”比较结果。

inline bool operator > (const vector<T> & ls, const vector<T> & rs)
非成员函数,判断前者是否大于后者。遍历vector,返回第一对不满足“==”运算符的元素的“>”比较结果。

inline bool operator <= (const vector<T> & ls, const vector<T> & rs)
非成员函数,判断前者是否"<="后者。结果为"!(rs < ls)"。

inline bool operator >= (const vector<T> & ls, const vector<T> & rs)
非成员函数,判断前者是否">="后者。结果为"!(ls < rs)"。

Operators

operator[]

Returns a reference to the vector element at a specified position.

vector的用法总结的更多相关文章

  1. c++中vector的用法详解

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

  2. STL中的Vector相关用法

    STL中的Vector相关用法 标准库vector类型使用需要的头文件:#include <vector>. vector 是一个类模板,不是一种数据类型,vector<int> ...

  3. C++学习二 vector的用法(使用sort对于vector排序)

    一.vector的介绍 vector是C++里面的一个容器,也是我们数学上面理解的向量,有一些比较常见的操作. 二.vector的定义 #include<vector> using nam ...

  4. STL vector常见用法详解

    <算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...

  5. POJ 1230 Pass-Muraille#贪心+vector迭代器用法

    (- ̄▽ ̄)-* (注意下面代码中关于iterator的用法,此代码借鉴某大牛) #include<iostream> #include<cstdio> #include< ...

  6. C++:vector的用法详解

    原文地址:http://blog.csdn.net/hancunai0017/article/details/7032383 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于 ...

  7. STL————vector的用法

    一.什么是vector? 向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container).跟任意其它类型容器一样,它能够存放各种类型的对象.可以简单的认为,向量是一个能 ...

  8. C++序列容器之 vector常见用法总结

    一.关于vector 本文默认读者具有一定的c++基础,故大致叙述,但保证代码正确. vector是一个动态的序列容器,相当于一个size可变的数组. 相比于数组,vector会消耗更多的内存以有效的 ...

  9. C++ STL(二)vector的用法

    ##### vector的定义 ```#include <iostream>#include <string>#include <vector>using name ...

  10. vector基本用法

    Vector作为STL容器中的一员,使用频率非常高,因此对其基本用法和实用技巧进行记录,便于后期查询使用. 基本用法 #include <iostream> #include <ve ...

随机推荐

  1. Python基础第三天

    三元运算 三元运算又叫三目运算,是对简单的条件语句的缩写,例如if判断 # 标准if判断语法 if 1 == 1: name = "yes" else: name = " ...

  2. IOS开发教程之put上传文件的服务器的配置及实例分享-备用

    感谢大神分享 1,HTTP常见的方法 GET 获取指定资源 POST 2M 向指定资源提交数据进行处理请求,在RESTful风格中用于新增资源 HEAD 获取指定资源头部信息PUT 替换指定资源(不支 ...

  3. android textview 行间距

    设置行间距:android:lineSpacingExtra 设置行间距的倍数:android:lineSpacingMultiplier 如下:设置行间距为3,行间距倍数为1.5 <TextV ...

  4. BZOJ1638: [Usaco2007 Mar]Cow Traffic 奶牛交通

    1638: [Usaco2007 Mar]Cow Traffic 奶牛交通 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 571  Solved: 199 ...

  5. Eclipse无法打开“Failed to load the JNI shared library”

    解决方案一 这是因为JDK配置错误所导致的现象. 一般说来,新购笔记本会预装64位的windows系统,而在网上下载软件时,32位会优先出现在页面中(现在来说是这个情况,但我认为未来64位会越来越普及 ...

  6. 【转】Android将Activity打成jar包供第三方调用(解决资源文件不能打包的问题)

    Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法) 鼠标右键项目,然后属性,然后java buildpath 然后order and ex ...

  7. MapReduce流程、如何统计任务数目以及Partitioner

    核心功能描述 应用程序通常会通过提供map和reduce来实现 Mapper和Reducer接口,它们组成作业的核心. Map是一类将输入记录集转换为中间格式记录集的独立任务. 这种转换的中间格式记录 ...

  8. IDF实验室解题学习笔记1

    1.图片里的英文 图片可以有很多种打开方式,破解该题,需将图片下载下来. 对于图片,我们可以使用图片编辑软件,进行各种调明暗,变色调等操作. 我们还可以使用2进制或者16进制的文件打开方式打开.该图使 ...

  9. hdu 5012 Dice

    Problem Description There are 2 special dices on the table. On each face of the dice, a distinct num ...

  10. C/C++基础概念

    1.类占用的内存大小: 1)在不同位数的操作系统下,各种数据类型所占用的内存大小:32位和64位操作系统 http://blog.csdn.net/b_zhang/article/details/68 ...