C++标准模板库STL之 vector容器的应用详解

vector

成员函数讲解:

1: begin()  end() :返回一个当前vector容器中起始元素的迭代器

2:insert()

iterator insert( iterator loc, const TYPE &val ); 
void insert( iterator loc, size_type num, const TYPE &val ); 
void insert( iterator loc, input_iterator start, input_iterator end ); 
insert() 函数有以下三种用法: 

在指定位置loc前插入值为val的元素,返回指向这个元素的迭代器, 
在指定位置loc前插入num个值为val的元素 

在指定位置loc前插入区间[start, end)的所有元素

3:erase参数为iter,表示删除掉相应的元素。也可以用两个iter表示删除一个区间。

4:at():参数:0,1,2等表示元素在vector中的位置。返回相应位置元素的引用

5:front() , back():返回当前vector中第一个元素的引用

6:push_back():尾部插入元素

7:size()返回vector大小,即元素个数

empty()空返回真,非空返回false

clear()清空vector

代码实例如下:

#include

#include

#include

using namespace std;

 

int main_vector(int argc, char **argv) {

       std::vector vec ,vect;                   //定义一个字符型的向量容器

       std::vector ::iterator vi;                         //定义一个字符型向量的迭代器

 

       //输入字符

       std::string str;

       char p[100];

       //存入向量中

//     std::cin.getline(p ,std::cin);

//     cin.getline(cin,str);

       std::string cmd;

       int i = 0;

       for(i = 0; i < 2; i++){

              getline(cin,cmd);

              vec.push_back(cmd);                                 //push_back 尾部插入

//            cout << cmd << endl;

              cout << vec.size() << endl;

       }

 

       for(i = 0 ,vi = vec.begin(); vi != vec.end(); vi++, i++){     //用迭代器遍历输出

              cout << "迭代器访问" << endl;

              cout << *vi << endl;

              cout << "下标访问" << endl;

              cout << vec[i] << endl;

       }

       vec.insert(vec.begin()+1, "lixiaoliang");                                  //insert 插入元素

       for(i = 0, vi = vec.begin(); vi != vec.end(); vi++, i++){     //用迭代器遍历输出

              cout << "迭代器访问" << endl;

              cout << *vi << endl;

              cout << "下标访问" << endl;

              cout << vec[i] << endl;

       }

 

       vec.erase(vec.begin());                                                                  //erase删除元素

       for(i = 0, vi = vec.begin(); vi != vec.end(); vi++, i++){     //用迭代器遍历输出

              cout << "迭代器访问" << endl;

              cout << *vi << endl;

              cout << "下标访问" << endl;

              cout << vec[i] << endl;

       }

 

       vect.assign(vec.begin(), vec.end());

       cout << vec.size() << endl;

       cout << vec.empty() << endl;

 

       cout << "**************assign*****************" << endl;

       for(i = 0, vi = vect.begin(); vi != vect.end(); vi++, i++){   //用迭代器遍历输出

              cout << "迭代器访问" << endl;

              cout << *vi << endl;

              cout << "下标访问" << endl;

              cout << vec[i] << endl;

       }

 

       cout << "at 0 = " << vect.at(0) << endl;

       cout << "at 1 = " << vect.at(1) << endl;

 

       cout << "front member = " + vect.front() << endl;

       cout << "back member = " + vect.back() << endl;

       //清空向量.

       vec.clear();

       cout << vec.size() << endl;

       cout << vec.empty() << endl;

}

转载于:https://www.cnblogs.com/lxlhappylife/p/6944628.html

你可能感兴趣的:(C++标准模板库STL之 vector容器的应用详解)