vector的用法总结

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.

时间: 2024-07-29 15:18:36

vector的用法总结的相关文章

STL中的Vector相关用法

STL中的Vector相关用法 标准库vector类型使用需要的头文件:#include <vector>. vector 是一个类模板,不是一种数据类型,vector<int>是一种数据类型. Vector的存储空间是连续的,list不是连续存储的. 1. 定义和初始化 vector< typeName > v1; //默认v1为空,故下面的赋值是错误的v1[0]=5;//v2是v1的一个副本,若v1.size()>v2.size()则赋值后v2.size()被

c++中vector的用法详解

c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. 用法: 1.文件包含: 首先在程序开头处加上#include<vector>以包含所需要的类文件vector 还有一定要加上using namespace std; 2.变量声明: 2.1 例:声明一个int向量以替代一维的数组:vector <int> a;(等于声明了一个

vector数组用法及举例

vector详解 1.调用vector的头文件 注意:c++编程中填写这个头文件 2.vector的用法 注意:大概来讲,vector就是一个变长数组,与a[]这种普通数组不同的是,他不用定义多长,是根据用户的用法变化的,同时它的用法及方法也与普通数组不同 3.vector的存取方法及特殊 注意:vector的存取与栈相同(即一种无盖有底的“洞”,从顶端存入,从顶端取出): 3.vector数据的读取及其他方法 注意:iterator是一种迭代器,即一种读取器,其功能类似for循环:利用iter

C++:vector的用法详解(转载)

原文地址:http://blog.csdn.net/hancunai0017/article/details/7032383 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. 用法: 1.文件包含: 首先在程序开头处加上#include<vector>以包含所需要的类文件vector 还有一定要加上using namespace std; 2.变量声明: 2.1 例:

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

(- ̄▽ ̄)-* (注意下面代码中关于iterator的用法,此代码借鉴某大牛) #include<iostream> #include<cstdio> #include<vector> #include<cstring> using namespace std; struct Wall { int row;//表示墙在哪行 int left; int right; }; int col[105];//保存每列的墙数 vector<Wall> w

C++ vector容器用法

在C++中,vector是个十分有用的容器,掌握这个容器的基础的用法十分有必要. 一.基本操作 头文件:#include<vector> 创建vector对象:vector<type> vec; 尾部插入变量:vec.push_back(a); 去掉最后一个数据:vec.pop_back(); 使用下标访问元素:vec[0],下标从0开始 使用迭代器访问元素: 1 vector<int> vec; 2 vector<int>::iterator i; 3 f

c++ STL库 vector 结构用法简介

STL是什么就不介绍了,这里主要介绍库中vector的常用用法方便以后查阅. Vector成员函数 函数 表述 c.assign(beg,end) c.assign(n,elem) 将[beg; end)区间中的数据赋值给c. 将n个elem的拷贝赋值给c. c.at(idx) 传回索引idx所指的数据,如果idx越界,抛出out_of_range. c.back() 传回最后一个数据,不检查这个数据是否存在. c.begin() 传回迭代器重的可一个数据. c.capacity() 返回容器中

STL里容器vector部分用法

基础用法在代码注释中说明 //test.cpp #include<iostream> #include<vector> using namespace std; int main() { vector<int> v; vector<int> v1(10);//为向量v1赋值10个0 vector<int> v3(10, 100);//为向量v3赋值10个100 for(int i=1; i<8; ++i) { v.push_back(i);

C++ STL(二)vector的用法

##### vector的定义 ```#include <iostream>#include <string>#include <vector>using namespace std;struct stu{ int age;};class xx{ string s;};void vectorDefine(){ vector<int> vec; vector<struct stu> vec2; vector<xx> vec3; vect

STL————vector的用法

一.什么是vector? 向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container).跟任意其它类型容器一样,它能够存放各种类型的对象.可以简单的认为,向量是一个能够存放任意类型的动态数组. 二.容器特性 1.顺序序列 顺序容器中的元素按照严格的线性顺序排序.可以通过元素在序列中的位置访问对应的元素. 2.动态数组 支持对序列中的任意元素进行快速直接访问,甚至可以通过指针算述进行该操作.操供了在序列末尾相对快速地添加/删除元素的操作. 3.能够感知内存分配器的