久久久久久久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 轉換單一顏色)
主站蜘蛛池模板: 国产www在线观看 | 欧美激情综合 | 亚洲国产福利 | 四虎在线免费观看 | 在线播放亚洲 | 欧美成人精品欧美一级私黄 | 日本成人免费网站 | 国产精品成人一区二区 | 亚洲天堂国产 | 中国久久久 | www久久久久 | 少妇一级淫片aaaaaa | 日韩欧美自拍 | 成人在线国产 | 一区二区久久久 | 九色91popny蝌蚪| 色77777| 2025国产精品 | 免费在线观看av | 激情av网 | 国产一级片视频 | 麻豆av网站 | 免费av网站在线观看 | 免费成人在线看 | 三级在线免费观看 | 精品自拍视频 | 天天爽天天操 | 青青草成人在线 | 久久久久女教师免费一区 | 欧洲精品| 伊人干综合 | 久久精品久久久久 | 日韩精品在线免费观看 | 亚洲一级二级三级 | 91色国产| 自拍视频一区 | 日韩精品一区二区视频 | 欧美在线小视频 | 久久久少妇 | 手机在线免费看av | www.婷婷.com |