最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

C++ vector使用示例

2023-02-03 09:56 作者:神兮兮的喵社長(zhǎng)  | 我要投稿

(1)a.assign(b.begin(), b.begin()+3); //b為向量,將b的0~2個(gè)元素構(gòu)成的向量賦給a

(2)a.assign(4,2); //是a只含4個(gè)元素,且每個(gè)元素為2

(3)a.back(); //返回a的最后一個(gè)元素

(4)a.front(); //返回a的第一個(gè)元素

(5)a[i]; //返回a的第i個(gè)元素,當(dāng)且僅當(dāng)a[i]存在

(6)a.clear(); //清空a中的元素

(7)a.empty(); //判斷a是否為空,空則返回ture,不空則返回false

(8)a.pop_back(); //刪除a向量的最后一個(gè)元素

(9)a.erase(a.begin()+1,a.begin()+3); //刪除a中第1個(gè)(從第0個(gè)算起)到第2個(gè)元素,也就是說(shuō)刪除的元素從a.begin()+1算起(包括它)一直到a.begin()+? ? ? ? ?3(不包括它)

(10)a.push_back(5); //在a的最后一個(gè)向量后插入一個(gè)元素,其值為5

(11)a.insert(a.begin()+1,5); //在a的第1個(gè)元素(從第0個(gè)算起)的位置插入數(shù)值5,如a為1,2,3,4,插入元素后為1,5,2,3,4

(12)a.insert(a.begin()+1,3,5); //在a的第1個(gè)元素(從第0個(gè)算起)的位置插入3個(gè)數(shù),其值都為5

(13)a.insert(a.begin()+1,b+3,b+6); //b為數(shù)組,在a的第1個(gè)元素(從第0個(gè)算起)的位置插入b的第3個(gè)元素到第5個(gè)元素(不包括b+6),如b為1,2,3,4,5,9,8? ? ? ? ?,插入元素后為1,4,5,9,2,3,4,5,9,8

(14)a.size(); //返回a中元素的個(gè)數(shù);

(15)a.capacity(); //返回a在內(nèi)存中總共可以容納的元素個(gè)數(shù)

(16)a.resize(10); //將a的現(xiàn)有元素個(gè)數(shù)調(diào)至10個(gè),多則刪,少則補(bǔ),其值隨機(jī)

(17)a.resize(10,2); //將a的現(xiàn)有元素個(gè)數(shù)調(diào)至10個(gè),多則刪,少則補(bǔ),其值為2

(18)a.reserve(100); //將a的容量(capacity)擴(kuò)充至100,也就是說(shuō)現(xiàn)在測(cè)試a.capacity();的時(shí)候返回值是100.這種操作只有在需要給a添加大量數(shù)據(jù)的時(shí)候才? ? ? ? ?顯得有意義,因?yàn)檫@將避免內(nèi)存多次容量擴(kuò)充操作(當(dāng)a的容量不足時(shí)電腦會(huì)自動(dòng)擴(kuò)容,當(dāng)然這必然降低性能)?

(19)a.swap(b); //b為向量,將a中的元素和b中的元素進(jìn)行整體性交換

(20)a==b; //b為向量,向量的比較操作還有!=,>=,<=,>,<

#include <vector>

# include <iostream>


int main()

{

std::vector<int> a(10);

std::vector<int> b(6);


std::cout << "The original vector a:" << std::endl;

for (int i = 0; i < a.size(); i++)

{

std::cout << a.at(i) << " ";

}

std::cout << std::endl;


std::cout << "The original vector b:" << std::endl;

for (int i = 0; i < b.size(); i++)

{

std::cout << b.at(i) << " ";

}

std::cout << std::endl;


std::cout << "The vector a insert 2 in the head" << std::endl;

a.insert(a.begin(),2);

for (int i = 0; i < a.size(); i++)

{

std::cout << a.at(i) << " ";

}

std::cout << std::endl;


std::cout << "The front element of vector a is " << a.front() << std::endl;

std::cout << "The back element of vector a is " << a.back() << std::endl;


std::cout << "Clear the vector a" << std::endl;

a.clear();

b.clear();

std::cout << "The size of vector a is " << a.size() << std::endl;


std::cout << "insert the vector a from 1 to 10" << std::endl;

for (int i = 0; i < 10; i++)

{

a.insert(a.begin(), i + 1);

b.insert(b.end(), i + 1);

}


std::cout << "print the vector a" << std::endl;

for (int i = 0; i < 10; i++)

{

std::cout << a.at(i) << " ";

}

std::cout << std::endl;


std::cout << "print the vector b" << std::endl;

for (int i = 0; i < 10; i++)

{

std::cout << b.at(i) << " ";

}

std::cout << std::endl;


std::cout << "judge the vector a whether is empty?" << std::endl;

std::cout << a.empty() << std::endl;


system("pause");

return 0;

}


C++ vector使用示例的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
获嘉县| 北安市| 寿阳县| 纳雍县| 长沙市| 福州市| 喀什市| 阿城市| 屏山县| 宣恩县| 临猗县| 台南县| 崇礼县| 阜康市| 徐州市| 大余县| 柳河县| 黄平县| 烟台市| 吉林市| 谷城县| 商河县| 平原县| 荥经县| 广汉市| 八宿县| 当涂县| 江油市| 昆山市| 福海县| 夹江县| 西充县| 扶余县| 石台县| 陆川县| 永兴县| 华池县| 楚雄市| 崇阳县| 大足县| 邢台县|