vector基础操作
生活随笔
收集整理的這篇文章主要介紹了
vector基础操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- //vector< T> vec; //構造一個名為vec的儲存數據類型為T的動態數組。其中T為需要儲存的數據類型
- //初始時vec為空
- //push_back 末尾添加一個元素
- //pop_back 在末尾彈出一個元素
- //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其中的某個元素,直接賦值,比如vec[1]=3; //修改vector其中的某個元素,直接賦值,比如vec[1]=3;
-
//vector的方法size()可以直接獲取長度,通過[]可以直接獲取其中的元素,和數組相同
-
//clear()會清空vector中內容,但是不會重新分配空間
- 如果需要清空vector的內存,一種典型的方法是使用交換, 即使用一個空的vector和原來的vector進行交換,完成內存的釋放
- 下面是官方文檔關于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
轉載于:https://www.cnblogs.com/FlyerBird/p/9052561.html
總結
以上是生活随笔為你收集整理的vector基础操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 背水一战 Windows 10 (36)
- 下一篇: 新建idea加入的一句话,键值对