第6章 vector向量容器

/*


第三篇 C++STL容器技术
  第6章 vector向量容器
   6.1 vector技术原理
   6.2 vector应用基础
   6.3 本章小结

*/


//  第6章 vector向量容器
//   6.1 vector技术原理 -------------------------------------------------------------------------------------------

//   6.2 vector应用基础 -------------------------------------------------------------------------------------------

// 95
#include <vector>
#include <iostream>
int main(void)
{
  using namespace std;
  vector < int > v;
  v.push_back(20);
  v.push_back(26);
  v.push_back(39);
  for(size_t i = 0; i < v.size(); i++)
    cout << "v[" << i << "] = " << v[i] << endl;
  return 0;
}


//96
#include <vector>
#include <iostream>
int main(void)
{
  using namespace std;
  vector < int > v;
  v.push_back(20);
  v.push_back(26);
  v.push_back(39);
  vector < int > ::iterator i, iend;
  iend = v.end();
  int j;
  for(i = v.begin(), j = 0; i != iend; i++, j++)
    cout << "v[" << j << "] = " <<  *i << endl;
  return 0;
}


// 96 , insert
#include <vector>
#include <iostream>
int main(void)
{
  using namespace std;
  vector < int > v;
  v.push_back(6);
  v.push_back(7);
  v.push_back(8);
  v.push_back(10);
  v.insert(v.begin() + 3, 9); //在元素10的前面插入9
  v.insert(v.begin(), 5); //插入5为首元素
  v.insert(v.end(), 11); //插入11为末元素
  for(size_t i = 0; i < v.size(); i++)
    cout << "v[" << i << "] = " << v[i] << endl;
  return 0;
}


// 97, erase
#include <iostream>
#include <vector>
class MyAnimal
{
  public:
    char *name;
    int age;
  public:
    MyAnimal(char *name, int age)
    {
        this->name = name;
        this->age = age;
    } 
    ~MyAnimal(){}
};

int main(void)
{
  using namespace std;
  MyAnimal *pDog = new MyAnimal("dog", 1);
  MyAnimal *pMonkey = new MyAnimal("monkey", 2);
  MyAnimal *pChicken = new MyAnimal("chicken", 3);
  MyAnimal *pSnake = new MyAnimal("snake", 4);
  vector < MyAnimal * > v; //v将存放各对象的地址
  v.push_back(pDog);
  v.push_back(pMonkey);
  v.push_back(pChicken);
  v.push_back(pSnake);
  delete pMonkey; //物理删除pMonkey所指的对象
  v.erase(v.begin() + 1); //删除第2个元素,即抹去了vector的pMonkey地址
  vector < MyAnimal * > ::iterator i, iend;
  iend = v.end();
  for(i = v.begin(); i != iend; i++)
    cout << (*i)->name << ' ' << (*i)->age << endl;
  v.clear(); //清除所有vector元素
  cout << "执行clear()" << endl << "vector元素已全部清除" << endl;
  return 0;
}


// 98, 反向遍历
#include <vector>
#include <iostream>
int main(void)
{
  using namespace std;
  vector < int > v;
  v.push_back(1);
  v.push_back(3);
  v.push_back(5);
  v.push_back(7);
  v.push_back(9);
  vector < int > ::reverse_iterator ri, riend; // 
  riend = v.rend();
  for(ri = v.rbegin(); ri != riend; ri++)
    cout <<  *ri << endl;
  return 0;
}


// 99, swap
#include <vector>
#include <iostream>
using namespace std;
void print(vector < int >  &v);
int main(void)
{
  //v1
  vector < int > v1;
  v1.push_back(11);
  v1.push_back(12);
  v1.push_back(13);
  cout << "v1 = ";
  print(v1);
  //v2
  vector < int > v2;
  v2.push_back(90);
  v2.push_back(92);
  cout << "v2 = ";
  print(v2);
  //v1与v2交换
  v1.swap(v2); // v1和v2元素个数不必相等
  cout << "v1与v2交换后" << endl;
  cout << "v1 = ";
  print(v1);
  cout << "v2 = ";
  print(v2);
  return 0;
}
void print(vector < int >  &v)
{
  for(size_t i = 0; i < v.size(); i++)
    cout << v[i] << " ";
  cout << endl;
}


// 100
#include <vector>
#include <iostream>
using namespace std;
void print(vector < int >  &v);
int main(void)
{
  using namespace std;
  vector < int > v;
  print(v);
  //添加5个元素
  v.push_back(1);
  v.push_back(2);
  v.push_back(3);
  v.push_back(4);
  v.push_back(5);
  print(v);
  //再添加4个元素
  v.push_back(6);
  v.push_back(7);
  v.push_back(8);
  v.push_back(9);
  print(v);
  //调整vector数据空间大小
  v.reserve(30);
  print(v);
  return 0;
}
void print(vector < int >  &v)
{
  cout << "---------------------" << endl;
  cout << "empty = " << v.empty() << endl;
  cout << "size = " << v.size() << endl;
  cout << "max_size = " << v.max_size() << endl;
  cout << "capacity = " << v.capacity() << endl;
}


//   6.3 本章小结 -------------------------------------------------------------------------------------------

TOP

原文地址:https://www.cnblogs.com/xin-le/p/4110327.html