vector
– 一.  vector可以模拟动态数组
– 二.  vector的元素可以是任意类型T,但必须具备赋值和拷贝能力(具有public

拷贝构造函数和重载的赋值操作符)

三.必须包含的头文件#include <vector>

–        四.  vector支持随机存取
–        五.  vector的大小(size)和容量(capacity)通常是不同的,size返回实际元素个数,

capacity返回vector能容纳的元素最大数量。如果插入元素时,元素个数超过capacity,

需要重新配置内部存储器。

->构造、拷贝和析构

->非变动操作

eg.

   vector<int> v1(10);
cout << "The capacity of v1 is " << v1.capacity() << endl;
cout << "The size of v1 is " << v1.size() << endl;
vector<int> v2;
v2.reserve(20);
cout << "The capacity of v2 is " << v2.capacity() << endl;
cout << "The size of v2 is " << v2.size() << endl;
output :
   The capacity of v1 is 10
The size of v1 is 10
The capacity of v2 is 20
The size of v2 is 0

->赋值操作

所有的赋值操作都有可能调用元素类型的默认构造函数,拷贝构造函数,赋值操作符和析构函数

如:

  std::list<T> l;
  std::vector<T> v;
  …
  v.assign(l.begin(),l.end());

eg.

   vector<int> v;
v.assign( 10, 42 );
for( vector<int>::size_type i = 0; i < v.size(); i++ ) {
cout << v[i] << " ";
}
cout << endl;

OutPut :

42 42 42 42 42 42 42 42 42 42

   vector<int> v1;
for( int i = 0; i < 10; i++ ) {
v1.push_back( i );
}
 
vector<int> v2;
v2.assign( v1.begin(), v1.end() );
 
for( vector<int>::size_type i = 0; i < v2.size(); i++ ) {
cout << v2[i] << " ";
cout << endl; 

output :

0 1 2 3 4 5 6 7 8 9

元素存取

 

下面的操作是错误的:

std::vector<T> v;//empty

 
  v[5]= t; //runtime error
  std::cout << v.front(); //runtime error

eg.

  vector<string> words;
    string str;
 
while( cin >> str ) words.push_back(str);
 
sort( words.begin(), words.end() );
 
cout << "In alphabetical order, the first word is '" << words.front() << "'." << endl;
假设输入是:  now is the time for all good men to come to the aid of their country
output:
In alphabetical order, the first word is 'aid'.

   vector<int> v;
for( int i = 0; i < 5; i++ ) {
v.push_back(i);
}
cout << "The first element is " << v.front()
<< " and the last element is " << v.back() << endl;
output:
The first element is 0 and the last element is 4
 

迭代器相关函数

使用迭代器时应注意:

迭代器持续有效,除非发生以下两种情况:

1.) 或插入元素
2.) 容量变化而引起内存重新分配

eg.

    vector<string> words;
string str;
 
while( cin >> str ) words.push_back(str);
 
for( vector<string>::const_iterator iter = words.begin();
iter != words.end(); ++iter ) {
cout << *iter << endl;
}

假设输入是 :  hey mickey you're so fine

output:

    hey  
    mickey
you're
so
fine
 
 

插入(insert)元素

eg.

   vector<char> alphaVector;
for( int i=0; i < 10; i++ ) {
alphaVector.push_back( i + 'A' );
}
 
// Insert four C's into the vector
vector<char>::iterator theIterator = alphaVector.begin();
alphaVector.insert( theIterator, 4, 'C' );
 
// Display the vector
for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); ++theIterator ) {
cout << *theIterator;
}

output:

CCCCABCDEFGHIJ

    vector<int> v1;
v1.push_back( 0 );
v1.push_back( 1 );
v1.push_back( 2 );
v1.push_back( 3 );
 
vector<int> v2;
v2.push_back( 5 );
v2.push_back( 6 );
v2.push_back( 7 );
v2.push_back( 8 );
 
cout << "Before, v2 is: ";
for( vector<int>::size_type i = 0; i < v2.size(); i++ ) {
cout << v2[i] << " ";
}
cout << endl;
 
v2.insert( v2.end(), v1.begin(), v1.end() );
 
cout << "After, v2 is: ";
for( vector<int>::size_type i = 0; i < v2.size(); i++ ) {
cout << v2[i] << " ";
   cout << endl; 

output :

    Before, v2 is: 5 6 7 8 
    After, v2 is: 5 6 7 8 0 1 2 3 
删除(remove)元素

eg.

    vector<char> alphas;
for( int i=0; i < 10; i++ ) {
static const char letters[] = "ABCDEFGHIJ";
alphas.push_back( letters[i] );
}
vector<char>::size_type size = alphas.size();
vector<char>::iterator startIterator;
vector<char>::iterator tempIterator;
for( vector<char>::size_type i=0; i < size; i++ ) {
startIterator = alphas.begin();
alphas.erase( startIterator );
// Display the vector
for( tempIterator = alphas.begin(); tempIterator != alphas.end(); ++tempIterator ) {
cout << *tempIterator;
}
cout << endl;
}

output:

    BCDEFGHIJ
CDEFGHIJ
DEFGHIJ
EFGHIJ
FGHIJ
GHIJ
HIJ
IJ 
    J
    vector<char> alphas;
for( int i=0; i < 10; i++ ) {
static const char letters[] = "ABCDEFGHIJ";
alphas.push_back( letters[i] );
}
// display the complete vector
for( vector<char>::size_type i = 0; i < alphas.size(); i++ ) {
cout << alphas[i];
}
cout << endl;
 
// use erase to remove all but the first two and last three elements
// of the vector
alphas.erase( alphas.begin()+2, alphas.end()-3 );
// display the modified vector
for( vector<char>::size_type i = 0; i < alphas.size(); i++ ) {
cout << alphas[i];
    cout << endl;

output:

    ABCDEFGHIJ 
    ABHIJ 
#include <iostream>
#include <vector>
#include <iterator>
 
using namespace std;
 
int main()
{
vector<char> alphas;
for( int i=0; i < 10; i++ ) {
static const char letters[] = "ABCDEFGHIJ";
alphas.push_back( letters[i] );
}
 
vector<char>::iterator iter = alphas.begin();
while( iter != alphas.end() )
{
if (*iter == 'B' || *iter == 'D')
iter = alphas.erase( iter );
else
++iter;
}
 
copy(alphas.begin(), alphas.end(), ostream_iterator<char>(cout, ""));
cout << endl;
  } 

output :

ACEFGHIJ

转载自:http://www.cppblog.com/MiYu/archive/2010/08/31/125459.html

作者:ACShiryu 
出处:http://www.cnblogs.com/ACShiryu/ 
若非注明,本博客文章均为原创,版权归作者和博客园共有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。 
该文章也同步发布在我的新浪微博中-ACShiryu's weibo,欢迎收听。

标准模板库(STL)学习指南之vector向量的更多相关文章

  1. 标准模板库(STL)学习探究之vector容器

    标准模板库(STL)学习探究之vector容器  C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被 ...

  2. 标准模板库(STL)学习指南之sort排序

    对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算法也基本固定下来,不需要你再去花费心思 ...

  3. 标准模板库(STL)学习指南之List链表

    本文转载自天极网,原文地址:http://www.yesky.com/255/1910755.shtml.转载请注明 什么是STL呢?STL就是Standard Template Library,标准 ...

  4. 标准模板库(STL)学习指南之priority_queue优先队列

    转载自CSDN博客:http://blog.csdn.net/suwei19870312/article/details/5294016 priority_queue 调用 STL里面的 make_h ...

  5. 标准模板库(STL)学习指南之set集合

    set是关联容器.其键值就是实值,实值就是键值,不可以有重复,所以我们不能通过set的迭代器来改变set的元素的值,set拥有和list相同的特性:当对他进行插入和删除操作的时候,操作之前的迭代器依然 ...

  6. 标准模板库(STL)学习指南之map映射

    转载自CSDN博客:http://blog.csdn.net/bat603/article/details/1456141 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关 ...

  7. 标准模板库(STL)学习探究之stack

    标准模板库(STL)学习探究之stack queue priority_queue list map/multimap dequeue string

  8. 标准模板库(STL)学习探究之Multimap容器

    C++ Multimaps和maps很相似,但是MultiMaps允许重复的元素.(具体用法请参考map容器)     函数列表:     begin() 返回指向第一个元素的迭代器      cle ...

  9. C++的标准模板库STL中实现的数据结构之顺序表vector的分析与使用

    摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解.即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第一篇,主要针对线性表中的顺序表(动 ...

随机推荐

  1. 【BZOJ4537】[Hnoi2016]最小公倍数 分块

    [BZOJ4537][Hnoi2016]最小公倍数 Description 给定一张N个顶点M条边的无向图(顶点编号为1,2,…,n),每条边上带有权值.所有权值都可以分解成2^a*3^b的形式.现在 ...

  2. An Ordinary Game(简单推导)

    An Ordinary Game Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement There ...

  3. STL中vector怎么实现邻接表

    最近,同期的一位大佬给我出了一道题目,改编自 洛谷 P2783 有机化学之神偶尔会做作弊 这道题好坑啊,普通链表过不了,只能用vector来存边.可能更快一些吧? 所以,我想记录并分享一下vector ...

  4. CenterFactory

    <?php /* 实例3 */ /* 抽象工厂 */ //青铜会员的打折商品 class BronzeRebateCommodity { //描述 public $desc = '青铜会员的打折 ...

  5. [原创]java WEB学习笔记18:java EE 中的MVC 设计模式(理论)

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  6. AC自动机的一点理解

    \(fail\)指针:指向最长的在\(tire\)里出现的后缀 比\(tire\)多出来的子边:原来的\(tire\),我们失配后又得返回根结点再次匹配,而加入这些边后只需要花\(strlen(s)\ ...

  7. 导出数据到表格PHP

    导出数据到表格 public function excel(){ $filename = '导出表格'; $header = ['编号','名称']; $index = ['id','name']; ...

  8. mysql中的内连接,外连接实例详解

    内连接: 只连接匹配的行左外连接: 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行右外连接: 包含右边表的全部行(不管左边的表中是否存在与它们匹配的行),以及左边 ...

  9. hihocoder 第五十二周 高斯消元·二【高斯消元解异或方程 难点【模板】】

    题目地址:http://hihocoder.com/contest/hiho57/problem/1 输入 第1..5行:1个长度为6的字符串,表示该行的格子状态,1表示该格子是亮着的,0表示该格子是 ...

  10. 转战github了

    现在已经改在github写博客了,地址为http://connorzhangxu.github.io/ 博客园用了几年,总体感觉不错,但是对公式的支持整体不是很好,所以后来自己搭建了github博客, ...