标准模板库(STL)学习指南之vector向量

简介:
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

目录
相关文章
|
12天前
|
存储 C++ 容器
C++STL(标准模板库)处理学习应用案例
【4月更文挑战第8天】使用C++ STL,通过`std:vector`存储整数数组 `{5, 3, 1, 4, 2}`,然后利用`std::sort`进行排序,输出排序后序列:`std:vector<int> numbers; numbers = {5, 3, 1, 4, 2}; std:sort(numbers.begin(), numbers.end()); for (int number : numbers) { std::cout << number << " "; }`
17 2
|
6月前
|
存储 算法 C++
【C++STL基础入门】vector向量基础使用
【C++STL基础入门】vector向量基础使用
|
2月前
|
存储 算法 搜索推荐
C++模板与STL【常用算法】
C++模板与STL【常用算法】
|
3月前
|
存储 算法 C++
【C++入门到精通】C++入门 —— vector (STL)
`std::vector`是C++标准库中的一个容器类模板,是一种动态数组,可以存储相同类型的元素。它提供了动态调整大小、快速随机访问、插入和删除元素的操作。
33 1
|
4月前
|
存储 算法 编译器
C++:STL第一篇vector
C++:STL第一篇vector
|
7月前
|
算法 C++
[Eigen中文文档] STL迭代器和算法
从 3.4 版本开始,Eigen 的稠密矩阵和数组提供了 STL 兼容的迭代器。这使 Eigen 自然地与 range-for 循环和 STL 算法兼容。
108 0
|
9月前
|
存储 搜索推荐 C++
竞赛:STL之vector用法详解(关于vector这一篇就够了!)
竞赛:STL之vector用法详解(关于vector这一篇就够了!)
|
10月前
|
算法 搜索推荐 C++
STL-常用算法总结
STL-常用算法总结
29 0
|
11月前
|
存储 算法 测试技术
【C++ STL】vector基础知识
本篇将学习vector的基础知识
57 0
|
11月前
|
算法 编译器 Linux
【C++】STL——vector 深度剖析 及 模拟实现(二)
【C++】STL——vector 深度剖析 及 模拟实现(二)
61 0