vector基础操作
生活随笔
收集整理的這篇文章主要介紹了
vector基础操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
- //vector< T> vec; //構(gòu)造一個(gè)名為vec的儲(chǔ)存數(shù)據(jù)類型為T的動(dòng)態(tài)數(shù)組。其中T為需要儲(chǔ)存的數(shù)據(jù)類型
- //初始時(shí)vec為空
- //push_back 末尾添加一個(gè)元素
- //pop_back 在末尾彈出一個(gè)元素
- //size 獲取長度
- size_type size() const; Return size Returns the number of elements in the vector.
- This is the number of actual objects held in the vector, which is not
necessarily equal to its storage capacity.
- //clear 清空
- //修改vector其中的某個(gè)元素,直接賦值,比如vec[1]=3; //修改vector其中的某個(gè)元素,直接賦值,比如vec[1]=3;
-
//vector的方法size()可以直接獲取長度,通過[]可以直接獲取其中的元素,和數(shù)組相同
-
//clear()會(huì)清空vector中內(nèi)容,但是不會(huì)重新分配空間
- 如果需要清空vector的內(nèi)存,一種典型的方法是使用交換, 即使用一個(gè)空的vector和原來的vector進(jìn)行交換,完成內(nèi)存的釋放
- 下面是官方文檔關(guān)于clear()的描述:
C++官方文檔
Clear content
Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. A typical alternative that forces a reallocation is to use swap:
vector().swap(x); // clear x reallocating
轉(zhuǎn)載于:https://www.cnblogs.com/FlyerBird/p/9052561.html
總結(jié)
以上是生活随笔為你收集整理的vector基础操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 背水一战 Windows 10 (36)
- 下一篇: 新建idea加入的一句话,键值对