CHAPTER6-24.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:3k
源码类别:

STL

开发平台:

C/C++

  1. //文件名:CHAPTER6-24.cpp
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iostream>
  5. using namespace std;
  6. void pause()  //程序暂停
  7. {   char c;
  8.     cout << "nnPress return to continue: ";
  9.     cin.get(c);
  10.     cout << "nn";
  11. }
  12. int main() 
  13. {
  14.     vector<int> v(10,0);   //定义一个vector变量,大小为10,值都为0
  15.     ostream_iterator<int> out(cout, " ");  //定义一个输出迭代器
  16.     copy(v.begin(), v.end(), out);// 通过算法函数copy输出v中全部的数据
  17.     pause(); //程序输出为:0 0 0 0 0 0 0 0 0 0 
  18.     vector<int>::iterator i = v.begin(); //定义头迭代器
  19.     i += 4;  //指向第5个元素
  20.     *i++ = 7;  // or v[4] = 7; //使第5个元素值为7,同时迭代器指向下一个元素
  21.     *i = 9;    // or v[5] = 9; //赋值第6个元素大小为9
  22.     copy(v.begin(), v.end(), out); // 把通过迭代器赋值后的所有元素打印出来
  23.     pause();//程序输出为: 0 0 0 0 7 9 0 0 0 0
  24.     vector<int>::iterator where = find(v.begin(), v.end(), 9);//在v中查找值为9的元素,并返回相应的迭代器
  25.     copy(where, v.end(), out);// 把查找到的元素及其该元素后的数据全部显示出来。
  26.     pause();//程序输出为:9 0 0 0 0
  27.     where = v.insert(where, 8); //在迭代器指示的元素前插入一个元素,其值为8
  28.     copy(v.begin(), v.end(), out); //检验insert函数的效果
  29.     pause();//程序输出为:0 0 0 0 7 8 9 0 0 0 0 
  30.     where += 3;  //迭代器指示当前元素后的第三个元素为当前元素
  31.     where = v.insert(where, 4); //在当前元素前插入一个元素,值为4
  32.     copy(v.begin(), v.end(), out);
  33.     pause();//程序输出为:0 0 0 0 7 8 9 0 4 0 0 0
  34.     where -= 6;//迭代器前移6个元素
  35.     where = v.insert(where, 11); //插入元素11到vector中
  36.     copy(v.begin(), v.end(), out);
  37.     pause();//程序输出为:0 0 11 0 0 7 8 9 0 4 0 0 0 
  38.     v.erase(where+2);  // 删除迭代器后的第2个元素
  39.     copy(v.begin(), v.end(), out);
  40.     pause();//程序输出为:0 0 11 0 7 8 9 0 4 0 0 0
  41.     sort(v.begin(), v.end()); //对vector进行由大到小排序
  42.     copy(v.begin(), v.end(), out);
  43.     pause();//程序输出为:0 0 0 0 0 0 0 4 7 8 9 11 
  44.     if (binary_search(v.begin(), v.end(), 8)) // vector的查找
  45.          cout << "Yes, 8 occurs in vector v.";
  46.     else
  47.          cout << "No, didn't find 8 in vector v.";
  48.     pause();//程序输出为:Yes, 8 occurs in vector v.
  49.     if (binary_search(v.begin(), v.end(), 12)) //  vector的查找
  50.          cout << "Yes, 12 occurs in vector v.";
  51.     else
  52.          cout << "No, didn't find 12 in vector v.";
  53.     pause();//程序输出为:No, didn't find 12 in vector v.
  54.     where = lower_bound(v.begin(), v.end(), 8); //查找第一次出现8的位置
  55.     copy(where, v.end(), out);
  56.     pause();//程序输出为:8 9 11
  57.     where = lower_bound(v.begin(), v.end(), 0); //查找第一次出现0的位置
  58.     copy(where, v.end(), out);
  59.     pause();//程序输出为:0 0 0 0 0 0 0 4 7 8 9 11
  60.     where = upper_bound(v.begin(), v.end(), 0); //查找第一次不出现0时的位置
  61.     copy(where, v.end(), out);
  62.     pause();//程序输出为:4 7 8 9 11
  63.     vector<int> w(v);
  64.     if (v == w) //两个vector直接比较
  65.        cout << "v and w have the same contents";
  66.     else
  67.        cout << "v and w have different contents";
  68.     pause();//程序输出为:v and w have the same contents
  69.     w[5] = 17;
  70.     if (v == w)
  71.        cout << "v and w have the same contents";
  72.     else
  73.        cout << "v and w have different contents";
  74.     pause();//程序输出为:v and w have different contents
  75.     v[5] = 17;
  76.     if (v == w)
  77.        cout << "v and w have the same contents";
  78.     else
  79.        cout << "v and w have different contents";
  80.     pause();//程序输出为:v and w have the same contents
  81.     return 0;
  82. }