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

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

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

問題描述

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

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, "
"));

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

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

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

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.)

推薦答案

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

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.

我不確定您的目標(biāo)是將密鑰放入向量中還是將它們打印出來進(jìn)行 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.

這篇關(guān)于如何從 std::map 檢索所有鍵(或值)并將它們放入向量中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
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 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 亚洲xx在线 | 91.com在线观看 | 丁香一区二区 | 91爱爱·com| 成人精品在线观看 | 国产精品免费在线 | 天天影视色综合 | 在线免费观看视频黄 | 国产精品一区二区三区久久 | 免费黄色大片 | 黄色大片观看 | 国产欧美日韩精品一区 | 久操福利| 欧美日韩国产精品一区 | 国产91丝袜在线播放 | 亚洲精品短视频 | 一区二区三区四区不卡 | 欧美日韩大陆 | 日韩成人在线播放 | 亚洲精品乱码久久久久久蜜桃91 | 亚州影院 | 欧美激情久久久 | 精品欧美一区二区三区久久久 | 国产在线视频在线观看 | 亚洲最新在线 | 99视频精品 | 91在线| 在线观看国产视频 | 亚洲综合小视频 | 亚洲区中文字幕 | 三级在线免费观看 | 国产精品久久久久久久久久 | 欧美一区视频 | 国产二区三区 | av午夜电影 | 中文字幕日韩一区二区 | 亚洲www啪成人一区二区麻豆 | 中文亚洲视频 | 亚洲成人自拍网 | 免费视频一区二区三区在线观看 | 中文字幕日韩在线 |