C++ 在 std::vector<std::pair> 中查找

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15499590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:26:18  来源:igfitidea点击:

find in std::vector<std::pair>

c++booststdc++98

提问by Baz

I have a vector of pairs. The first in the pair is of type std::string and the second is of type Container.

我有一个成对的向量。该对中的第一个是 std::string 类型,第二个是 Container 类型。

What convenient functionality exists in std or boost so that I can return a Container given the string value as key?

std 或 boost 中存在哪些方便的功能,以便我可以以字符串值作为键返回一个容器?

UPDATE

更新

It has been commented that I could use a std::map instead, but I actually need to preserve the order of my items, in the order that I push them to the vector.

有人评论说我可以使用 std::map 代替,但我实际上需要保留我的项目的顺序,按照我将它们推送到向量的顺序。

回答by Andy Prowl

A possible solution:

一个可能的解决方案:

struct comp
{
    comp(std::string const& s) : _s(s) { }

    bool operator () (std::pair<std::string, Container> const& p)
    {
        return (p.first == _s);
    }

    std::string _s;
};

// ...

typedef std::vector<std::pair<std::string, Container> > my_vector;
my_vector v;

// ...

my_vector::iterator i = std::find_if(v.begin(), v.end(), comp("World"));
if (i != v.end())
{
    Container& c = i->second;
}

// ...

Here is a complete example:

这是一个完整的例子:

#include <vector>
#include <utility>
#include <string>
#include <algorithm>

struct Container
{
    Container(int c) : _c(c) { }
    int _c;
};

struct comp
{
    comp(std::string const& s) : _s(s) { }

    bool operator () (std::pair<std::string, Container> const& p)
    {
        return (p.first == _s);
    }

    std::string _s;
};

#include <iostream>

int main()
{
    typedef std::vector<std::pair<std::string, Container> > my_vector;
    my_vector v;
    v.push_back(std::make_pair("Hello", Container(42)));
    v.push_back(std::make_pair("World", Container(1729)));
    my_vector::iterator i = std::find_if(v.begin(), v.end(), comp("World"));
    if (i != v.end())
    {
        Container& c = i->second;
        std::cout << c._c; // <== Prints 1729
    }
}

And here is a live example.

这是一个活生生的例子

回答by Paul Fultz II

Using Boost.Rangeand Boost.Bind, you can do this:

使用Boost.RangeBoost.Bind,你可以这样做:

struct predicate
{
    template<class Key, class Pair>
    bool operator()(const Key& k, const Pair& p) const
    {
        return p.first == k;
    }
};

// Your vector of pairs
std::vector<std::pair<std:string, Container> v = ...;
// The key you would like to search for
std::string key = ...;
Container& c = boost::find_if(v, boost::bind(predicate(), key, _1))->second;

回答by Charles L Wilcox

There is a simple solution: use std::copyand std::inserter:

有一个简单的解决方案:使用std::copystd::inserter

#include <algorithm>
#include <map>
#include <string>
#include <utility> // pair
#include <vector>

void function()
{
    typedef int Data;
    typedef std::pair< std::string, Data > String_Data_Pair;
    typedef std::vector< String_Data_Pair > String_Data_Pair_Sequence;
    typedef std::map< std::string, Data > String_To_Data_Map;

    String_Data_Pair_Sequence string_data_pairs;

    /* fill 'string_data_pairs' here */

    String_To_Data_Map string_to_data_map;

    std::copy( string_data_pairs.begin(),
               string_data_pairs.end(),
               std::inserter( string_to_data_map,
                              string_to_data_map.begin() /* superfluous, but required */ ) );
}

回答by Stefan Matta

class SomeClass{
   int num;
public:
   SomeClass();
   void setNumber(int n) const { num = n;}
};

vector<pair<SomeClass,string> > vectr;

for(unsigned int i = 0; i < vectr.size(); i++)
   if(vectr[i].second == "key")
       vectr[i].first.setNumber(50);

Worked for me!

为我工作!