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

std::map,指向映射鍵值的指針,這可能嗎?

std::map, pointer to map key value, is this possible?(std::map,指向映射鍵值的指針,這可能嗎?)
本文介紹了std::map,指向映射鍵值的指針,這可能嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

std::map<std::string, std::string> myMap;

std::map<std::string, std::string>::iterator i = m_myMap.find(some_key_string);
if(i == m_imagesMap.end())
    return NULL;

string *p = &i->first;

最后一行有效嗎?我想將此指針 p 存儲在其他地方,它對整個程序生命周期都有效嗎?但是如果我向這個映射添加更多元素(使用其他唯一鍵)或刪除一些其他鍵會發生什么,它會不會重新分配這個字符串(鍵值對),所以 p 將變得無效?

Is the last line valid? I want to store this pointer p somewhere else, will it be valid for the whole program life? But what will happen if I add some more elements to this map (with other unique keys) or remove some other keys, won’t it reallocate this string (key-value pair), so the p will become invalid?

推薦答案

首先保證地圖穩定;即迭代器不會因元素插入或刪除而失效(當然被刪除的元素除外).

First, maps are guaranteed to be stable; i.e. the iterators are not invalidated by element insertion or deletion (except the element being deleted of course).

然而,迭代器的穩定性并不能保證指針的穩定性!盡管大多數實現通常會使用指針 - 至少在某種程度上 - 來實現迭代器(這意味著假設您的解決方案可以工作是非常安全的),您真正應該存儲的是迭代器本身.

However, stability of iterator does not guarantee stability of pointers! Although it usually happens that most implementations use pointers - at least at some level - to implement iterators (which means it is quite safe to assume your solution will work), what you should really store is the iterator itself.

您可以做的是創建一個小對象,例如:

What you could do is create a small object like:

struct StringPtrInMap
{
  typedef std::map<string,string>::iterator iterator;
  StringPtrInMap(iterator i) : it(i) {}
  const string& operator*() const { return it->first; }
  const string* operator->() const { return &it->first; }
  iterator it;
}

然后存儲它而不是字符串指針.

And then store that instead of a string pointer.

這篇關于std::map,指向映射鍵值的指針,這可能嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 亚洲三级在线观看 | 免费一级网站 | 中文字幕一区二区三区在线观看 | 国产激情一区二区三区 | 亚洲aⅴ| 久久久高清 | 日韩伦理一区二区 | 国产98色在线 | 日韩 | 美女一区二区在线观看 | 久久福利网站 | 精品福利一区二区三区 | 日韩视频在线播放 | 手机av网| 久久99精品久久久久久秒播九色 | 亚洲一区二区三区免费视频 | 亚洲一区二区中文字幕 | 国产精品国产三级国产aⅴ原创 | 中文字幕一区二区在线观看 | 欧美日韩一区二区电影 | av黄色在线播放 | 欧美videosex性极品hd | 视频一区二区在线观看 | 中文字幕第二十页 | 一级毛片播放 | 91在线精品视频 | 亚洲一区二区 | 欧美在线日韩 | 日本精品视频 | 精品一区二区三区免费毛片 | 激情91| 一区二区三区国产精品 | 日韩一级免费观看 | 欧美在线一区二区三区 | 精精国产xxxx视频在线播放 | 黄色一级片在线播放 | 青草青草久热精品视频在线观看 | 国产黄色在线观看 | 日本欧美在线视频 | 久久久做| 中文av网站 | 仙人掌旅馆在线观看 |