原创作品,转载请注明出处:http://www.cnblogs.com/shrimp-can/p/5280566.html

iterator类型:

iterator:到value_type的访问,value_type是模板的类型

const_iterator:到const value_type的访问

reverse_iterator:reverse_iterator<iterator>

const_reverse_iterator:reverse_iterator<const_iterator>

其中我们常用的是iterator。

函数:

一、构造、赋值相关函数:

1.构造函数

explicit vector (const allocator_type& alloc = allocator_type());
默认构造函数,空容器
explicit vector (size_type n);
vector (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type());
用n个val填充容器,含一个参数的是C++11定义的
template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
范围构造,[first,last)这么多个,并且填充
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
拷贝构造,两个参数的是C++11
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
C++11
vector (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type());
C++11

如:

  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);

2.=操作:vector& operator= (const vector& x);

可以将一个vector通过=符号赋给另一个vector

  std::vector<int> foo (3,0);
std::vector<int> bar (5,0); bar = foo;
foo = std::vector<int>(); std::cout << "Size of foo: " << int(foo.size()) << '\n';
std::cout << "Size of bar: " << int(bar.size()) << '\n';

输出为:

Size of foo: 0
Size of bar: 3

二、Iterator操作相关函数:

1.begin:iterator begin();

const_iterator begin() const;

返回指向第一个元素的iterator指针

2.end:用法与begin同,通常是一起使用来遍历容器中的元素

返回指向最后一个元素再后面一个的iterator指针

3.rbegin:reverse_iterator rbegin();

const_reverse_iterator rbegin() const;

返回指向最后一个元素的reverse_iterator指针

4.rend:用法与rbegin相同

返回指向第一个元素前面一个元素的reverse_iterator指针,通常与rbegin一起使用来反向遍历容器

5.cbegin、cend返回值类型为const_iterator,功能同begin、end

6.crbegin、crend返回值类型为const_reverse_iterator,功能同crbegin、crend

常用来遍历表,例如:

vector<int>::iterator Iter;

vector<int> c;

for(Iter=c.begin();Iter!=c.end();Iter++)

{}

三、capacity相关函数

1.size:size_type size() const;

容器中元素个数。c.size();

2.max_size():size_type max_size() const;

返回最大容量。c.maxsize();

3.resize:void resize (size_type n, value_type val = value_type());

改变容器可以容纳元素的个数为n。如果n小于当前的容器大小,则保留前面n个元素,移除后面的。如果n大于当前容器大小,就扩展容器。value是初始值,如果n大于当前容器大小,则新增加的元素的值为value,若value省略,会自动调用一个默认值。

std::vector<int> c;

for(int i=0;i<10;i++)

c.push_back(i);

c.resize(5);

c.resize(8,10);

c.resize(12);

for(int i=0;i<c.size();i++)

std::cout<<c[i]<<"  ";

结果为:1  2  3  4  5  10  10  10  0  0  0  0

4.capacity:size_type capacity() const;

当前分配给容器的存储空间大小(元素个数),这并不限制容器的扩展,理论限制容器扩展大小是max_size的值

5.empty:bool empty() const;

返回容器是否为空while(!c.empty()){sum+=c.back();
c.pop_back();}

6.reserve:void reserve (size_type n);

使得capacity至少能容纳n个元素。

7.shrink_to_fit(C++11):void shrink_to_fit();

减小capacity,使其与容器大小相同

四、元素访问相关函数

1.[ ]操作:获取特定位置的元素。c[i];

2.at:reference at (size_type n);

const_reference at (size_type n) const;

返回位置n处的元素。c.at(i),与c[i]差不多

3.front:reference front();

const_reference front() const;

返回容器中的第一个元素。c.front();

4.back:reference back()

const_reference back() const;

返回容器中最后一个元素。c.back();

5.data(C++11):value_type* data() noexcept;

const value_type* data() const noexcept;

返回一个指向容器中数组的指针c.data()

int *p=c.data();

*p=10;

++p;

*p=20;

p[2]=100;

则c中存储的前面三个数据为10、20、100

五、更新操作

1.assign:template <class InputIterator> void assign (InputIterator first, InputIterator last);

void assign (size_type n, const value_type& val);

void assign (initializer_list<value_type> il);(C++11)

给容器分配新的内容,并替换当前内容,同时修改大小,val为初始值

std::vector<int>first;

std::vector<int>second;

std::vector<int>third;

first.assign(7,100);

std::vector<int>::iterator it;

it=first.begin()+1;

second.assign(it,first.end()-1);

int myints[]={1776,7,4};

third.assign(myints,myints+3);

std::cout<<int(first.size())<<"  "<<int (second.size())<<"  "<<int (third.size())<<endl;

结果为:7  5  3

2.push_back: void push_back (const value_type& val);

在容器最后增加一个新的元素。c.push_back(n)

3.pop_back:void pop_back();

移除最后一个元素。c.pop_back()

4.insert:iterator insert(iterator position,const value_type& val):在position处插入元素val

void insert(iterator position,size_type n,const value_type& val):在position处插入n个元素,插入的元素值初始化为val

void insert(iterator position,InputIterator first,InputIterator last):在position处插入数组中从first到last的元素

vector<int> c(3,100);

vector<int>::iterator it;

it=c.begin();

it=c.insert(it,200);

c.insert(it,2,300);

it=c.begin();

vector<int> d(2,400);

c.insert(it+2,d.begin(),d.end());

int s[]={501,502,503};

c.insert(c.begin(),s,s+3);

此时c中元素为:501  502  503  300  300  400  400  200  100  100  100

5.erase:iterator erase(iterator position):删除position处的元素

iterator erase(iterator first,iterator last):删除first到last的元素

std::vector<int> myvector;
for (int i=1; i<=10; i++) myvector.push_back(i);
myvector.erase (myvector.begin()+5);
myvector.erase (myvector.begin(),myvector.begin()+3);
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); ++i)
std::cout << ' ' << myvector[i];

结果为:4  5  7  8  9  10

6.swap:void swap(vector &x)

交换两个容器的内容,两个容器中的元素不一定要相等。c.swap(d);

7.clear:void clear():清楚容器中的所有元素。c.clear();

8.emplace:iterator emplace(const_iterator position,args&& args):在position处插入一个新的元素。

std::vector<int> myvector = {10,20,30};

auto it = myvector.emplace ( myvector.begin()+1, 100 );

myvector.emplace ( it, 200 );

myvector.emplace ( myvector.end(), 300 );

容器中元素为:10  200  100  20  30  300

9.emplace_back:void emplace_back(args&& args):在最后插入一个元素

六、分配器

allocator_type get_allocator() const:给容器分配空间

std::vector<int> myvector;
int * p;
unsigned int i;
p = myvector.get_allocator().allocate(5);
for (i=0; i<5; i++) myvector.get_allocator().construct(&p[i],i);
std::cout << "The allocated array contains:";
for (i=0; i<5; i++) std::cout << ' ' << p[i];
std::cout << '\n';
for (i=0; i<5; i++) myvector.get_allocator().destroy(&p[i]);
myvector.get_allocator().deallocate(p,5)

七、重载函数

1.重载了操作符"=="  '!='  '<'  '<='  '>'  '>=':对于==来说,首先比较它们的大小,若size相等,则依次比较元素;对于其他运算符,依次比较元素。c==d;

参考:http://www.cplusplus.com/reference/vector/vector/

C++标准库之vector(各函数及其使用全)的更多相关文章

  1. C++ Primer 第三章 标准库类型vector+迭代器iterator 运算

    1.vector: 标准库类型vector表示对象的集合,其中所有对象的类型都相同,集合中的每个对象都有一个与之对应的索引,索引用于访问对象.因为vector“容纳着”其他对象,所以它也常被称作容器( ...

  2. 标准库类型vector

    标准库类型vector表示对象的集合,其中所有对象的类型都相同.集合中的每个对象都有一个与之对应的索引,索引用于访问对象.因为vector“容纳着”其他对象,所以它被称为容器. 要想使用vector, ...

  3. C++ Primer笔记1_转义字符_标准库类型string_标准库类型vector

    1.转义字符 一般有两种方式: \x后紧跟1个或多个十六进制数字.或\后紧跟1.2.3个八进制数字,当中数字部分是字符相应的数值. #include <iostream> using na ...

  4. Python标准库:内置函数hasattr(object, name)

    Python标准库:内置函数hasattr(object, name) 本函数是用来判断对象object的属性(name表示)是否存在.如果属性(name表示)存在,则返回True,否则返回False ...

  5. C/C++ 错误笔记-解决swap函数与标准库的std::swap函数冲突的问题

    下午写了一份代码: #include <iostream> using namespace std; // 模板1:交换基本类型的值 template<typename T> ...

  6. C 和 C++ 的标准库分别有自己的 locale 操作方法,C 标准库的 locale 设定函数是 setlocale(),而 C++ 标准库有 locale 类和流对象的 imbue() 方法(gcc使用zh_CN.GBK,或者zh_CN.UTF-8,VC++使用Chinese_People's Republic of China.936或者65001.)

    转自:http://zyxhome.org/wp/cc-prog-lang/c-stdlib-setlocale-usage-note/ [在此向原文作者说声谢谢!若有读者看到文章转载时请写该转载地址 ...

  7. Python标准库-数字的处理函数(math模块)

    Python标准库-数字的处理函数(math模块) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. #!/usr/bin/env python #_*_conding:utf-8_* ...

  8. C++ Pirmer : 第十四章 : 重载运算符与类型转换之函数调用运算符与标准库的定义的函数对象

    函数调用运算符 struct test { int operator()(int val) const { return (i > 0 ? i : -i); } }; 所谓的函数调用就是一个类重 ...

  9. 标准库中 vector list等排序

    1.list自带有排序函数sort():可以定义自己的排序规则,如: struct stTest { int count; wstring str; }; bool SortByNum(const s ...

  10. STL标准库-容器-vector

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性. 向量容器vector是一个动态数组,内存连续,它是动态分配内存,且每次扩张的原来的二倍. 他的结构如下 一 定义 vector ...

随机推荐

  1. ASP.NET Forms身份认证

    asp.net程序开发,用户根据角色访问对应页面以及功能. 项目结构如下图: 根目录 Web.config 代码: <?xml version="1.0" encoding= ...

  2. SQL 增删改查45道题

    create database School use School go create table Student --1.学生表 ( Sno ) not null primary key,--学号( ...

  3. 框架篇:Spring+SpringMVC+hibernate整合开发

    前言: 最近闲的蛋疼,搭个框架写成博客记录下来,拉通一下之前所学知识,顺带装一下逼. 话不多说,我们直接步入正题. 准备工作: 1/ IntelliJIDEA的安装配置:jdk/tomcat等..(本 ...

  4. 数据库--iOS

    1.创建表 @"create table if not exists Person(id integer primary key autoincrement,name text,gender ...

  5. MATLAB 单变量函数一阶及N阶求导

    1 对一维函数的求导及求特定函数处的变量值 %%最简单的一阶单变量函数进行求导 function usemyfunArray() %主函数必须位于最上方 clc clear syms x %syms ...

  6. java-7继承

    请自行编写代码测试以下特性(动手动脑):在子类中,若要调用父类中被覆盖的方法,可以使用super关键字. public class QWE {    public void main(String[] ...

  7. react-router3.x hashHistory render两次的bug,及解决方案

    先写一个简单App页面,其实就是简单修改了react-router的官方例子中的animations例子,修改了两个地方: 1.路由方式由browserHistory修改为hashHistory 2. ...

  8. asp.net core mvc实现伪静态功能

    在大型网站系统中,为了提高系统访问性能,往往会把一些不经常变得内容发布成静态页,比如商城的产品详情页,新闻详情页,这些信息一旦发布后,变化的频率不会很高,如果还采用动态输出的方式进行处理的话,肯定会给 ...

  9. 简述public private protected internal修饰符的访问权限

    public 关键字是类型和类型成员的访问修饰符.公共访问是允许的最高访问级别.对访问公共成员没有限制. protected 关键字是一个成员访问修饰符.受保护成员在它的类中可访问并且可由派生类访问. ...

  10. [故障公告]博客站点遭遇超过20G的流量攻击被阿里云屏蔽

    2017年2月21日17:34,突然收到阿里云的通知: 您的IP受到攻击流量已超过云盾DDoS基础防护的带宽峰值,服务器的所有访问已被屏蔽,如果35分钟后攻击停止将自动解除否则会延期解除... 紧接着 ...