網站首頁 編程語言 正文
一、vector
(1)區分size()和capacity()
-
size()
:返回容納的元素個數 -
capacity()
:返回當前分配存儲的容量
(2)迭代器失效
(3)區分const_iterator和const iterator
-
const_iterator
:常性迭代器,指向的對象的屬性為常性; -
const iterator
:常性的普通迭代器,迭代器自身屬性為常性;
(4)區分reserve()和resize()
reserve():預留存儲空間,只改變capacity
增加 vector 的容量到大于或等于 new_cap 的值。若 new_cap 大于當前的 capacity() ,則分配新存儲,否則該方法不做任何事。reserve() 不更改 vector 的 size 。
若 new_cap 大于 capacity() ,則所有迭代器,包含尾后迭代器和所有到元素的引用都被非法化。否則,沒有迭代器或引用被非法化。
#include#include using namespace std; class Value { public: Value() { cout << "Value()"<< endl; } ~Value() { cout << "~Value()" << endl; } }; void Vector_user() { vector vec; //vec.resize(10); vec.reserve(10); cout << vec.capacity()<< endl; cout << vec.size()<< endl; } int main() { Vector_user(); return 0; }
resize():改變容器中可存儲元素的個數size和capacity,并調用默認的構造函數
#include#include using namespace std; class Value { public: Value() { cout << "Value()"<< endl; } ~Value() { cout << "~Value()" << endl; } }; void Vector_user() { vector vec; vec.resize(10); //vec.reserve(10); cout << vec.capacity()<< endl; cout << vec.size()<< endl; } int main() { Vector_user(); return 0; }
(5)push_back和emplace
1.push_back()
#include#include using namespace std; class Object { private: int val; public: Object(int x = 0):val(x) { cout << "Object(int x)"<< endl; } Object(const Object& src): val(src.val) { cout << "Object(const Object& src)" << endl; } Object(Object&& src) : val(src.val) { cout << "Object(Object&& src)" << endl; } Object& operator=(const Object& src) { val = src.val; cout << "=" << endl; return *this; } Object& operator=(Object&& src) { val = src.val; cout << "=&" << endl; return *this; } ~Object() { cout << "~Object()" << endl; } }; void fun() { std::vector
push_back(10);
push_back(Object(10));
兩種方式構造對象的順序個數都相同!
Object obj(10);
vcobj.push_back(obj);
2. emplace()原位構造
void fun() { std::vector
void fun() { std::vector
void fun() { std::vector
(6)關于原位構造(定位new + 完美轉發)
定位new:直接在指定的地址空間內調用構造函數
完美轉發:保留傳參的右值屬性
可變參數:根據傳參個數類型,調用不同的構造函數
templatevoid Make(T* p, Arg... arg) { new(p) T(std::forward(arg)...); } int main() { //1.開辟空間 Object* p = (Object*)malloc(sizeof(Object)); //2.在p指向的地址空間調用構造Object(10) Make(p, 10); //3.釋放空間并調用析構函數 delete p; return 0; }
總結
原文鏈接:https://blog.csdn.net/xiaoxiaoguailou/article/details/123377623
相關推薦
- 2023-03-22 gin正確多次讀取http?request?body內容實現詳解_Golang
- 2022-05-05 .NetCore手動封裝日志組件的實現代碼_實用技巧
- 2022-04-12 Qt實現實時鼠標繪制圖形_C 語言
- 2023-02-10 數據卷(Data?Volumes)及dockefile詳解_docker
- 2023-12-08 uniapp 頁面添加背景圖片不顯示
- 2023-12-18 Jedis和springboot集成redis
- 2022-10-07 C語言函數之memcpy函數用法實例_C 語言
- 2022-06-02 python文件與路徑操作神器?pathlib_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支