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

如何從 std::map 檢索所有鍵(或值)并將它們放入向

How to retrieve all keys (or values) from a std::map and put them into a vector?(如何從 std::map 檢索所有鍵(或值)并將它們放入向量中?)
本文介紹了如何從 std::map 檢索所有鍵(或值)并將它們放入向量中?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這是我出來的可能方式之一:

This is one of the possible ways I come out:

struct RetrieveKey
{
    template <typename T>
    typename T::first_type operator()(T keyValuePair) const
    {
        return keyValuePair.first;
    }
};

map<int, int> m;
vector<int> keys;

// Retrieve all keys
transform(m.begin(), m.end(), back_inserter(keys), RetrieveKey());

// Dump all keys
copy(keys.begin(), keys.end(), ostream_iterator<int>(cout, "
"));

當然,我們也可以通過定義另一個函子RetrieveValues來從地圖中檢索所有值.

Of course, we can also retrieve all values from the map by defining another functor RetrieveValues.

有沒有其他方法可以輕松實現這一目標?(我一直想知道為什么 std::map 不包含一個成員函數讓我們這樣做.)

Is there any other way to achieve this easily? (I'm always wondering why std::map does not include a member function for us to do so.)

推薦答案

雖然您的解決方案應該有效,但可能難以閱讀,這取決于您的程序員同事的技能水平.此外,它將功能從呼叫站點移開.這會使維護變得更加困難.

While your solution should work, it can be difficult to read depending on the skill level of your fellow programmers. Additionally, it moves functionality away from the call site. Which can make maintenance a little more difficult.

我不確定您的目標是將密鑰放入向量中還是將它們打印出來進行 cout,所以我兩者都在做.你可以試試這樣的:

I'm not sure if your goal is to get the keys into a vector or print them to cout so I'm doing both. You may try something like this:

std::map<int, int> m;
std::vector<int> key, value;
for(std::map<int,int>::iterator it = m.begin(); it != m.end(); ++it) {
  key.push_back(it->first);
  value.push_back(it->second);
  std::cout << "Key: " << it->first << std::endl();
  std::cout << "Value: " << it->second << std::endl();
}

或者更簡單,如果您使用的是 Boost:

Or even simpler, if you are using Boost:

map<int,int> m;
pair<int,int> me; // what a map<int, int> is made of
vector<int> v;
BOOST_FOREACH(me, m) {
  v.push_back(me.first);
  cout << me.first << "
";
}

就我個人而言,我喜歡 BOOST_FOREACH 版本,因為輸入較少,而且它的作用非常明確.

Personally, I like the BOOST_FOREACH version because there is less typing and it is very explicit about what it is doing.

這篇關于如何從 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 轉換單一顏色)
主站蜘蛛池模板: 一区不卡在线观看 | 日韩在线观看一区 | 亚洲第一区国产精品 | 91在线影院| 在线亚洲人成电影网站色www | 欧美成人一区二免费视频软件 | 精品一区二区三区四区在线 | av免费网站在线观看 | 99精品在线观看 | av毛片在线免费观看 | 高清视频一区二区三区 | 亚洲男人网 | 99精品热视频 | 中文字幕精品一区 | aa级毛片毛片免费观看久 | 日本不卡免费新一二三区 | 人人看人人射 | 欧美日韩国产三级 | 久久成人免费观看 | 久草热在线 | 亚洲精品一区二区在线观看 | 免费av播放 | 黄免费观看视频 | 拍真实国产伦偷精品 | 久久免费精品 | 精品九九 | 成人在线观看中文字幕 | h网站在线观看 | 国产精品久久久久一区二区 | 国产高清免费 | 亚洲另类春色偷拍在线观看 | 国产成人综合网 | 91精品国产91久久久久久吃药 | 久久天堂网 | 欧美视频中文字幕 | 日韩一区二区在线视频 | 特黄特黄a级毛片免费专区 av网站免费在线观看 | 玖玖视频网 | 天堂中文资源在线 | 国产日韩一区二区三免费高清 | 中文字幕久久精品 |