乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      STL vector用法

       啟蒙彩魂 2011-02-18

      #include <iostream>
      #include <vector>
      #include <algorithm>
      #include <cstdlib>
      using namespace std;
      typedef vector<int> INTVECTOR;

      int main(void)
      {
      vector<int> num;   // STL中的vector容器
      int element;

      // 從標準輸入設(shè)備讀入整數(shù),
      // 直到輸入的是非整型數(shù)據(jù)為止
      while (cin >> element)     //ctrl+Z 結(jié)束輸入
         num.push_back(element);

      // STL中的排序算法
      sort(num.begin(), num.end());

      // 將排序結(jié)果輸出到標準輸出設(shè)備
      for (int i = 0; i < num.size(); i ++)
         cout << num[i] << " ";
      cout<<endl;
          //vec1對象初始為空
          INTVECTOR vec1;  
          //vec2對象最初有10個值為6的元素
          INTVECTOR vec2(10,6);
          //vec3對象最初有3個值為6的元素
          INTVECTOR vec3(vec2.begin(),vec2.begin()+3);

          //聲明一個名為i的雙向迭代器
          INTVECTOR::iterator i;

          //從前向后顯示vec1中的數(shù)據(jù)
          cout<<"vec1.begin()--vec1.end():"<<endl;
          for (i =vec1.begin(); i !=vec1.end(); ++i)
              cout << *i << " ";
          cout << endl;

          //從前向后顯示vec2中的數(shù)據(jù)
          cout<<"vec2.begin()--vec2.end():"<<endl;
          for (i =vec2.begin(); i !=vec2.end(); ++i)
              cout << *i << " ";
          cout << endl;

          //從前向后顯示vec3中的數(shù)據(jù)
          cout<<"vec3.begin()--vec3.end():"<<endl;
          for (i =vec3.begin(); i !=vec3.end(); ++i)
              cout << *i << " ";
          cout << endl;

          //測試添加和插入成員函數(shù)
          vec1.push_back(2);
          vec1.push_back(4);
          vec1.insert(vec1.begin()+1,5); //在第一個位置后插入5
          vec1.insert(vec1.begin()+1,vec3.begin(),vec3.end());//在第一個位置后插入ve3開始到結(jié)束的數(shù)字
          cout<<"push() and insert():" <<endl;
          for (i =vec1.begin(); i !=vec1.end(); ++i)
              cout << *i << " ";
          cout << endl;

          //測試賦值成員函數(shù)
          vec2.assign(8,1);//重新給vec2賦了8個值1
          cout<<"vec2.assign(8,1):" <<endl;
          for (i =vec2.begin(); i !=vec2.end(); ++i)
              cout << *i << " ";
          cout << endl;

          //測試引用類函數(shù)
          cout<<"vec1.front()="<<vec1.front()<<endl;
          cout<<"vec1.back()="<<vec1.back()<<endl;
          cout<<"vec1.at(4)="<<vec1.at(4)<<endl;
          cout<<"vec1[4]="<<vec1[4]<<endl;

          //測試移出和刪除
          vec1.pop_back();//最高位前移一位(去掉最高位)
          cout<<"vec1.pop_back():" <<endl;
          for (i =vec1.begin(); i !=vec1.end(); ++i)
              cout << *i << " ";
          cout << endl;
          vec1.erase(vec1.begin()+1,vec1.end()-2);//清除 vec1.begin()+1到vec1.end()-2
          cout<<"vec1.erase():" <<endl;
          for (i =vec1.begin(); i !=vec1.end(); ++i)
              cout << *i << " ";
          cout << endl;

          //顯示序列的狀態(tài)信息
          cout<<"vec1.capacity(): "<<vec1.capacity()<<endl; //capacity() 告訴你最多添加多少個元素才會導(dǎo)致容器重分配內(nèi)存
          cout<<"vec1.max_size(): "<<vec1.max_size()<<endl;
          cout<<"vec1.size(): "<<vec1.size()<<endl;   //size() 是告訴你容器當中目前實際有多少個元素
          cout<<"vec1.empty(): "<<vec1.empty()<<endl;

          //vector序列容器的運算
          cout<<"vec1==vec3: "<<(vec1==vec3)<<endl; //相等返回1,不相等返回0
          cout<<"vec1<=vec3: "<<(vec1<=vec3)<<endl; //小于等于返回1,否則返回0

         system("pause");
         return 0;
      }
      *
          Vector: 它就是數(shù)組的一個泛化推廣, 等同于數(shù)組,它擁有一段連續(xù)的內(nèi)存空間,并且起始地址不變,
      優(yōu)點:1)很好的支持隨機存取,即[]操作符,查詢操作高效
      缺點:1)插入/刪除會造成內(nèi)存塊的拷貝,效率較低,(尾部元素除外)
          2)內(nèi)存空間不夠時,需要重新申請一塊足夠大的內(nèi)存并進行內(nèi)存的拷貝,大大影響了vector的效率
      */
      #include 
      <iostream>
      #include 
      <vector>
      #include 
      <algorithm>

      using namespace std;

      void printVector(vector<int> v1)
      {
      //使用下標方式
      //for (unsigned int i = 0; i < v1.size(); i++)
      //{
      // cout<<"v1["<<i<<"] = "<<v1[i]<<endl;
      //}

      //使用迭代器
      //vector<int>::iterator iter;
      //for (iter = v1.begin(); iter != v1.end(); iter++)
      //{
      // //cout<<"v1["<<iter-v1.begin()<<"] = "<<*iter<<endl;
      // cout<<"v1["<<iter-v1.begin()<<"]"<<"="<<*iter<<", ";
      //}
      //cout<<endl;

      //使用迭代器指針
      //vector<int>::iterator *pIter; //Error,迭代器指針需要分配內(nèi)存才能使用,
                   
      //或者通俗的說pIter未實現(xiàn)時,*pIter是不確定的,運行時對其賦值必須會發(fā)生異常
      vector<int>::iterator *pIter = new vector<int>::iterator; 
      if(NULL == pIter)
      {
         
      return;
      }

      for(*pIter = v1.begin(); *pIter != v1.end(); (*pIter)++//此處若寫成*pIter++是不對的,因為++優(yōu)化級別較高
      {
         cout
      <<**pIter<<"";
      }

      if (NULL != pIter)
      {
         delete pIter;
         pIter 
      = NULL;
      }

      cout
      <<endl;
      }


      void reversePrintVector(vector<int> vec)
      {
      vector
      <int>::reverse_iterator revIter;
      for (revIter = vec.rbegin(); revIter != vec.rend(); revIter++)
      {
         cout
      <<"v["<<revIter-vec.rbegin()<<"]="<<*revIter<<"";
      }

      cout
      <<endl;
      }

      int main()
      {
      //創(chuàng)建
      vector<int> v1; //創(chuàng)建空的vector對象
      vector<int> v2(10); //創(chuàng)建具有10個元素的vector對象
      vector<double> v3(109.1); //創(chuàng)建具有10個元素的vector對象,每個元素的值為9.1
      vector<double> v4(v3); //通過拷貝一個V3對象的各個元素值,創(chuàng)建一個新的vector對象
      int iArray[] = {1113 , 192327};
      vector
      <int> v(iArray, iArray + 5);//通過拷貝迭代器區(qū)間[first, end)的元素值,創(chuàng)建新的vector對象

      //初始化----用push_back,在容器的尾端插入新元素
      v1.push_back(3);
      v1.push_back(
      10);
      v1.push_back(
      19);

      //遍歷訪問:1-下標方式(略)
      //遍歷訪問之2:迭代器方式, 建議使用此方式并熟練應(yīng)用之
      cout<<"printVector(v1): "<<endl; 
      printVector(v1);

      //反向遍歷之迭代器方式
      cout<<"reversePrintVector(v1):"<<endl;
      reversePrintVector(v1);

      //插入元素:用insert(&pos, elem)
      cout<<"v1.insert(v1.begin() + 1, 100): "<<endl;
      v1.insert(v1.begin() 
      + 1100);
      printVector(v1);

      //刪除元素: 尾部元素刪除用pop_back();單個元素刪除用erase(&pos);
      //[first, last)區(qū)間元素刪除用erase(&first, &last); 整體刪除用clear();
      cout<<"v1.erase(v1.begin() + 1): "<<endl;
      v1.erase(v1.begin() 
      + 1);
      printVector(v1);
      cout
      <<"v1.pop_back():"<<endl;
      v1.pop_back();
      printVector(v1);

      //查找函數(shù):由外部算法提供
      vector<int>::iterator iter;
      cout
      <<"find(v1.begin(), v1.end(), 2) = "<<endl;
      iter 
      = find(v1.begin(), v1.end(), 10);
      if (iter != v1.end())
         cout
      <<"find "<<*iter<<" at index: "<<iter-v1.begin()<<endl;
      else
         cout
      <<"not find"<<endl; 

      cout
      <<"v1.empty() = "<<v1.empty()<<endl;
      cout
      <<"其它:\nv1.empty() = "<<v1.empty()<<", v1.size() = "<<v1.size()<<", v1.max_size() =(hex)"<<hex<<v1.max_size()<<endl;
      cout
      <<"head-elem="<<dec<<v1.front()<<endl;
      cout
      <<"tail-elem="<<v1.back()<<endl;

      //清空
      cout<<"v1.clear() "<<endl;
      v1.clear();
      cout
      <<"v1.size() = "<<v1.size()<<", v1.empty()="<<v1.empty()<<endl;
      }



      ////////////////////////////////////運行結(jié)果////////////////////////////////////////////////////////
      printVector(v1):
      31019,
      reversePrintVector(v1):
      v[
      0]=19, v[1]=10, v[2]=3,
      v1.insert(v1.begin() 
      + 1100):
      31001019,
      v1.erase(v1.begin() 
      + 1):
      31019,
      v1.pop_back():
      310,
      find(v1.begin(), v1.end(), 
      2=
      find 
      10 at index: 1
      v1.empty() 
      = 0
      其它:
      v1.empty() 
      = 0, v1.size() = 2, v1.max_size() =(hex)3fffffff
      head
      -elem=3
      tail
      -elem=10
      v1.clear()
      v1.size() 
      = 0, v1.empty()=1

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多