欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ 中Vector常用基本操作

 更新時間:2017年10月16日 11:17:24   作者:Ginger_2  
標(biāo)準(zhǔn)庫vector類型是C++中使用較多的一種類模板,本文給大家分享C++ 中Vector常用基本操作,感興趣的朋友一起看看吧

標(biāo)準(zhǔn)庫vector類型是C++中使用較多的一種類模板,vector類型相當(dāng)于一種動態(tài)的容器,在vector中主要有一些基本的操作,下面通過本文給大家介紹,具體內(nèi)容如下所示:

(1)頭文件#include<vector>.

(2)創(chuàng)建vector對象,vector<int> vec;

(3)尾部插入數(shù)字:vec.push_back(a);

(4)使用下標(biāo)訪問元素,cout<<vec[0]<<endl;記住下標(biāo)是從0開始的。

(5)使用迭代器訪問元素.

vector<int>::iterator it;
for(it=vec.begin();it!=vec.end();it++)
cout<<*it<<endl;

(6)插入元素: vec.insert(vec.begin()+i,a);在第i+1個元素前面插入a;

(7)刪除元素:vec.erase(vec.begin()+2);刪除第3個元素

vec.erase(vec.begin()+i,vec.end()+j);刪除區(qū)間[i,j-1];區(qū)間從0開始

(8)向量大小:vec.size();

(9)清空:vec.clear();

 下面有一個簡單示例:

#include<iostream>
#include<stdio.h>
#include<vector>//不定長數(shù)組,向量
#include<string>
using namespace std;
int main()
{
  vector<string> v;
  string temp;
  cout<<"請輸入一個字符串,并且回車后按Ctrl+Z表示循環(huán)結(jié)束:"<<endl;
  while(getline(cin,temp))//Ctrl+Z 結(jié)束循環(huán)
  {
    v.push_back(temp);
  }
  vector<string>::iterator t; //定義一個迭代器t
  t=v.begin();
  for(t;t!=v.end();t++)
  {
    (*t)[0]=toupper((*t)[0]);//把開頭第一個字母變?yōu)榇髮?
    cout<<*t<<endl;
  }
  return 0;
}
/*主要功能:輸入一個字符串,再輸出一個字符串,并把首字母大寫
輸入示例:
ginger,you are the best!
^Z
輸出:
Ginger,you are the best!
*/

總結(jié)

以上所述是小編給大家介紹的C++ 中Vector常用基本操作,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論