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

我應該存儲整個對象,還是在容器中存儲指向對

Should I store entire objects, or pointers to objects in containers?(我應該存儲整個對象,還是在容器中存儲指向對象的指針?)
本文介紹了我應該存儲整個對象,還是在容器中存儲指向對象的指針?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

從頭開始設計新系統.我將使用 STL 來存儲某些長期存在的對象的列表和映射.

Designing a new system from scratch. I'll be using the STL to store lists and maps of certain long-live objects.

問題:我應該確保我的對象具有復制構造函數并將對象的副本存儲在我的 STL 容器中,還是通常更好地管理生命周期?確定自己的范圍并將指向這些對象的指針存儲在我的 STL 容器中?

Question: Should I ensure my objects have copy constructors and store copies of objects within my STL containers, or is it generally better to manage the life & scope myself and just store the pointers to those objects in my STL containers?

我意識到這有點缺乏細節,但我正在尋找理論上"更好的答案(如果存在),因為我知道這兩種解決方案都是可能的.

I realize this is somewhat short on details, but I'm looking for the "theoretical" better answer if it exists, since I know both of these solutions are possible.

玩指針的兩個非常明顯的缺點:1) 我必須自己在 STL 之外的范圍內管理這些對象的分配/解除分配.2) 我無法在堆棧上創建臨時對象并將其添加到我的容器中.

Two very obvious disadvantage to playing with pointers: 1) I must manage allocation/deallocation of these objects myself in a scope beyond the STL. 2) I cannot create a temp object on the stack and add it to my containers.

還有什么我遺漏的嗎?

推薦答案

因為人們對使用指針的效率贊不絕口.

Since people are chiming in on the efficency of using pointers.

如果您正在考慮使用 std::vector 并且如果更新很少并且您經常迭代您的集合并且它是一種非多態類型存儲對象副本"將更有效,因為您將獲得更好的引用位置.

If you're considering using a std::vector and if updates are few and you often iterate over your collection and it's a non polymorphic type storing object "copies" will be more efficent since you'll get better locality of reference.

Otoh,如果更新是常見的,存儲指針將節省復制/重定位成本.

Otoh, if updates are common storing pointers will save the copy/relocation costs.

這篇關于我應該存儲整個對象,還是在容器中存儲指向對象的指針?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: www.狠狠操 | 99精品九九 | 久久综合一区 | 久久久久一区二区三区四区 | 精品一区av | 亚洲看片| 国产色 | 秋霞在线一区 | 国内精品久久久久久 | 视频在线观看一区二区 | a网站在线观看 | 久久久久9999 | 国产成人久久av免费高清密臂 | 视频一区二区三区中文字幕 | 免费小视频在线观看 | 欧美一区不卡 | 美国一级黄色片 | 亚洲一区不卡在线 | 99re超碰| 日韩中文字幕在线播放 | 91在线精品视频 | 亚洲国产成人在线 | 涩涩视频在线看 | 国产精品视频中文字幕 | 精品一区二区三区免费视频 | 欧美精品一区在线发布 | 亚洲国产成人久久综合一区,久久久国产99 | 在线播放中文字幕 | 久草.com | 亚洲网视频 | 国产精品永久免费 | 看片地址| 天天躁日日躁狠狠的躁天龙影院 | 黄色男女网站 | 亚洲欧美成人影院 | 国产精品久久久久久久久免费软件 | 亚洲欧美视频一区 | 久久精品亚洲一区 | 一区二区福利视频 | 亚洲免费视频一区 | 国产精品一区二区三区四区 |