C++中怎么使用vector標(biāo)準(zhǔn)模板庫(kù)

今天就跟大家聊聊有關(guān)C++中怎么使用vector標(biāo)準(zhǔn)模板庫(kù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)是一家專(zhuān)注于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),貴德網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:貴德等地區(qū)。貴德做網(wǎng)站價(jià)格咨詢(xún):028-86922220

一:介紹

vector是C++標(biāo)準(zhǔn)模板庫(kù),是一個(gè)容器,底層是數(shù)組,為連續(xù)內(nèi)存。

命名空間為std,所屬頭文件為<vector>   注意:不是<vector.h>

vector存儲(chǔ)數(shù)據(jù)時(shí),會(huì)分配一個(gè)存儲(chǔ)空間,如果繼續(xù)存儲(chǔ),該分配的空間已滿(mǎn),就會(huì)分配一塊更大的內(nèi)存,把原來(lái)的數(shù)據(jù)復(fù)制過(guò)來(lái),繼續(xù)存儲(chǔ),這些性能也會(huì)一定程度上會(huì)有損耗

二:常用操作

容量:

  • a.vector大?。簐ector.size()

  • b.vector所占內(nèi)存實(shí)際大小:vector.capacity()

修改:

  • a.尾部添加元素:vector.push_back()

  • b.尾部刪除元素:vector.pop_back()

  • c.交換兩個(gè)vector元素:vector.swap()

  • d.清空vector元素:vector.clear()

  • e.刪除指定元素:vector.erase(it)

迭代器:

  • a.vector開(kāi)始指針:vector.begin()

  • b.vector尾部指針:vector.end()   注:最后一個(gè)元素的下一個(gè)位置,類(lèi)似為NULL,不是容器的最后一個(gè)元素

訪(fǎng)問(wèn)元素:

  • a.下標(biāo)訪(fǎng)問(wèn):vector[1]  //不檢查是否越界

  • b.at方法訪(fǎng)問(wèn):vector.at(1) //自動(dòng)檢查是否越界,如越界會(huì)拋出異常

  • c.訪(fǎng)問(wèn)第一個(gè)元素:vector.front()

  • d.訪(fǎng)問(wèn)最后一個(gè)元素:vector.back()

三:存儲(chǔ)

簡(jiǎn)單存儲(chǔ)

  //存儲(chǔ)方式1
  vector<int> v1(10);
  for (int i=0; i<10; i++)
  {
    v1[i] = i;
  }
  //存儲(chǔ)方式2
  vector<int> v2;
  for (int i=0; i<10; i++)
  {
    v2.push_back(i);
  }

存儲(chǔ)結(jié)構(gòu)體和結(jié)構(gòu)體指針

  struct Student
  {
    char name[32];
    int age;
  };
  //存儲(chǔ)結(jié)構(gòu)體
  vector<Student> vStu1;
  for (int i=0; i<10; i++)
  {
    Student stu;
    strcpy(stu.name, "woniu201");
    stu.age = 30 + i;
    vStu1.push_back(stu);
  }
  //存儲(chǔ)結(jié)構(gòu)體指針
  vector<Student*> vStu2;
  for (int i=0; i<10; i++)
  {
    Student* pStu = (Student*)malloc(sizeof(Student));
    strcpy(pStu->name, "woniu201"); 
    pStu->age = 30 + i; 
    vStu2.push_back(pStu); 
  }

四:vector遍歷

  vector<int> v;
  for (int i=0; i<100; i++)
  {
    v.push_back(i);
  }
  //遍歷方式1
  for (int i=0; i<100; i++)
  {
    int& a = v[i];
    printf("%d ", a);
  }
  //遍歷方式2
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    int&a = *it;
    printf("%d ", a);
  }

五:排序

對(duì)vector整形進(jìn)行排序

#include "stdlib.h"
#include <vector>
#include <algorithm>
using namespace std;
//升序比較函數(shù)
int compare1(const int &a, const int &b)
{
  return a < b;
}
//降序比較函數(shù)
int compare2(const int &a, const int &b)
{
  return a > b;
}
int main()
{
  vector<int> v;
  for (int i=0; i<10; i++)
  {
    v.push_back(rand() % 10);
  }
  //遍歷輸出
  printf("排序前數(shù)據(jù):");
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%d ", *it);
  }
  //升序排序
  sort(v.begin(), v.end(), compare1);
  //遍歷輸出
  printf("\n升序后數(shù)據(jù):");
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%d ", *it);
  }
  //降序排序
  sort(v.begin(), v.end(), greater<int>());
  //遍歷輸出
  printf("\n降序后數(shù)據(jù):");
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%d ", *it);
  }
  getchar();
  return 1;
}

對(duì)存放類(lèi)成員變量排序

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:  
  Student(string n, int c) :name(n), core(c) {}
  string  name;
  int    core;
};
//升序比較函數(shù)
bool compare1(const Student& s1, const Student& s2)
{
  return s1.core < s2.core;
}
//降序比較函數(shù)
bool compare2(const Student& s1, const Student& s2)
{
  return s1.core > s2.core;
}
int main()
{
  vector<Student> v;
  Student s1("aaaa", 97);
  Student s2("bbbb", 99);
  Student s3("cccc", 95);
  v.push_back(s1);
  v.push_back(s2);
  v.push_back(s3);
  printf("排序前數(shù)據(jù):\n");
  for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
  }
  //升序排序
  sort(v.begin(), v.end(), compare1);
  printf("\n升序后的數(shù)據(jù):\n");
  for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
  }
  //降序排序
  sort(v.begin(), v.end(), compare2);
  printf("\n降序后的數(shù)據(jù):\n");
  for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
  }
  getchar();
  return 1;
}

六:查找

  vector<int>::iterator it = find(v.begin(), v.end(), 5);
  if(it != v.end())
  {
    cout << "found";
  }
  else
  {
    cout << "not found";
  }

七:刪除

  for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
  {
    if(*it == 8)
    {
      it = v.erase(it);//it會(huì)++一次
      it--;    //刪除完后需要--,否則最終循環(huán)越界
    }
  }

八:釋放內(nèi)存

存放整形vector釋放

 //存放整型
 vector<int> v;
 for (int i=0; i<100; i++)
 {
 v.push_back(i);
 }
  //釋放內(nèi)存
  {
    vector<int> vEmpty;
    v.swap(vEmpty);
  }

存放結(jié)構(gòu)體vector釋放

 //存儲(chǔ)結(jié)構(gòu)體
 vector<Student> vStu1;
 for (int i=0; i<10; i++)
 {
 Student stu;
 strcpy(stu.name, "woniu201");
 stu.age = 30 + i;
 vStu1.push_back(stu);
 }
 //釋放內(nèi)存  
    {
      vector<Student>
    }
 vector<Student> vEmpty;
     vStu1.swap(vEmpty);

存放結(jié)構(gòu)體指針vector釋放

 //存儲(chǔ)結(jié)構(gòu)體指針
 vector<Student*> vStu2;
 for (int i=0; i<10; i++)
 {
 Student* pStu = (Student*)malloc(sizeof(Student));
 strcpy(pStu->name, "wangpengfei");
 pStu->age = 30 + i;
 vStu2.push_back(pStu);
 }
 //釋放內(nèi)存
 for (vector<Student*>::iterator it = vStu2.begin(); it != vStu2.end(); it++)
 {
 if (NULL != *it)
 {
  delete *it;
  *it = NULL;
 }
 }

看完上述內(nèi)容,你們對(duì)C++中怎么使用vector標(biāo)準(zhǔn)模板庫(kù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

當(dāng)前標(biāo)題:C++中怎么使用vector標(biāo)準(zhǔn)模板庫(kù)
URL標(biāo)題:http://muchs.cn/article12/gpghgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、企業(yè)建站、建站公司、關(guān)鍵詞優(yōu)化、網(wǎng)站維護(hù)網(wǎng)站營(yíng)銷(xiāo)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化