久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C ++在地圖中插入unique_ptr

C++ inserting unique_ptr in map(C ++在地圖中插入unique_ptr)
本文介紹了C ++在地圖中插入unique_ptr的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我有一個(gè) ObjectArray

typedef map<int64_t, std::unique_ptr<Class1>> ObjectArray;

Class1 類型的新對(duì)象創(chuàng)建 unique_ptr 并將其插入到 ObjectArray 類型的對(duì)象中的語法是什么?

What is the syntax to create a unique_ptr to a new object of type Class1 and insert it into an object of type ObjectArray?

推薦答案

首先,如果它是映射而不是數(shù)組,我不會(huì)稱它為 ObjectArray.

As a first remark, I wouldn't call it ObjectArray if it is a map and not an array.

無論如何,你可以這樣插入對(duì)象:

Anyway, you can insert objects this way:

ObjectArray myMap;
myMap.insert(std::make_pair(0, std::unique_ptr<Class1>(new Class1())));

或者這樣:

ObjectArray myMap;
myMap[0] = std::unique_ptr<Class1>(new Class1());

這兩種形式的區(qū)別在于,如果鍵 0 已經(jīng)存在于映射中,前一種將失敗,而第二種形式會(huì)用新的值覆蓋其值.

The difference between the two forms is that the former will fail if the key 0 is already present in the map, while the second one will overwrite its value with the new one.

在 C++14 中,您可能希望使用 std::make_unique() 而不是從 new 表達(dá)式構(gòu)造 unique_ptr .例如:

In C++14, you may want to use std::make_unique() instead of constructing the unique_ptr from a new expression. For instance:

myMap[0] = std::make_unique<Class1>();

這篇關(guān)于C ++在地圖中插入unique_ptr的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對(duì)普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會(huì)導(dǎo)致訪問沖突?)
主站蜘蛛池模板: 欧美日韩国产高清 | 欧美精品久久 | 精品日韩一区二区三区 | 欧美精品在欧美一区二区少妇 | 亚洲男人的天堂网站 | 视频精品一区二区三区 | 国产www在线 | 精品亚洲一区二区 | 日韩成人免费 | 成人av在线大片 | 日本成人在线播放 | 99久久国产综合精品麻豆 | 网址黄 | 国产一级久久久久 | 欧美寡妇偷汉性猛交 | 午夜a级理论片915影院 | 一区二区三区四区日韩 | 欧美在线小视频 | av看片网站 | 日韩一区二区三区在线视频 | 欧美黑人激情 | 国产精品123区| av在线免费观看网址 | 亚洲一区二区在线电影 | 国产电影一区二区三区爱妃记 | 亚洲精品1区 | 影音先锋中文字幕在线观看 | 中国一级特黄真人毛片免费观看 | 国产美女精品 | 亚洲视频一区在线观看 | 一区二区三区亚洲 | 日韩在线精品视频 | 青草青草久热精品视频在线观看 | 精品一区电影 | 91久久爽久久爽爽久久片 | 国产精品伦一区二区三级视频 | 亚洲精品久久久一区二区三区 | 国产精品精品久久久 | 亚洲视频在线免费 | 国产99久久| 欧美一区二区视频 |