C++ std::map<std::string, int> 获取键以特定字符串开头的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16263060/
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 20:11:15  来源:igfitidea点击:

C++ std::map<std::string, int> get values whose key starts with a particular string

c++map

提问by Dejwi

I'm using std::map in such a way:

我以这样的方式使用 std::map :

#include <map>
#include <string>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    map<string, int> my_map;

    my_map.insert(pair<string, int>("Ab", 1));
    my_map.insert(pair<string, int>("Abb", 2));
    my_map.insert(pair<string, int>("Abc", 3));
    my_map.insert(pair<string, int>("Abd", 4));
    my_map.insert(pair<string, int>("Ac", 5));
    my_map.insert(pair<string, int>("Ad", 5));

    cout<<my_map.lower_bound("Ab")->second<<endl;
    cout<<my_map.upper_bound("Ab")->second<<endl;
    return 0;
}

http://ideone.com/5YPQmj

http://ideone.com/5YPQmj

I'd like to get all values whose key starts with a particular string (for example "Ab"). I can easily get the begin iterator using map::lower_bound. But how can I get an upper bound? Do I have to iterate the whole set starting at lower bound and check every key if it still starts with "Ab"?

我想获取其键以特定字符串开头的所有值(例如“Ab”)。我可以使用 map::lower_bound 轻松获取开始迭代器。但是我怎样才能得到一个上限呢?我是否必须从下限开始迭代整个集合并检查每个键是否仍以“Ab”开头?

回答by Matt Stokes

I found a similar answer check out this page: (map complex find operation)

我在这个页面找到了一个类似的答案:(地图复杂的查找操作

Code Exert:

代码执行:

template<typename Map> typename Map::const_iterator
find_prefix(Map const& map, typename Map::key_type const& key)
{
    typename Map::const_iterator it = map.upper_bound(key);
    while (it != map.begin())
    {
        --it;
        if(key.substr(0, it->first.size()) == it->first)
            return it;
    }

    return map.end(); // map contains no prefix
}

It looks as if in this example you iterate from the upper_bound backwards till the beginning looking for the specific substring

看起来好像在这个例子中你从 upper_bound 向后迭代直到开始寻找特定的子字符串

This example is slightly different but should server as a good building block

这个例子略有不同,但应该作为一个很好的构建块

回答by lucas92

class BeginWithKey
{
public:
    BeginWithKey(const string key);
    bool operator()(const string& s,const int x);
private:
    const string& key_;
};

BeginWithKey::BeginWithKey(const string key):key_(key)
{
}

bool BeginWithKey::operator()(const string& s, const int& rh)
{
    bool begin = true;

    for(int i = 0; i < key_.size() && begin; ++i)
        begin = (s[i] == key_[i]);
    return !begin;
}

int main()
{
    //your code

    //copying the map object
    map<string, int> copy = my_map;

    //removing the strings not beginning with abc
    BeginWithKey func("abc");
    remove_if(copy.begin(), copy.end(), func);

    return 0;
}

The code will work with any string key.

该代码适用于任何字符串键。

回答by Roee Gavirel

you can use Boost filter iteratorwhich give you a "begin" and a "end" iterator from normal iterators when they given a predicate (a bool function which says which values to include)

您可以使用Boost 过滤器迭代器,当它们给出谓词时,它会为您提供来自普通迭代器的“开始”和“结束”迭代器(一个布尔函数,它说明要包含哪些值)

For example:

例如:

template <class Predicate>
boost::filter_iterator<Predicate, map<string,int>::const_iterator> begin(Predicate predicate) const
{
    return boost::make_filter_iterator(predicate, my_map.begin(), my_map.end());
}
template <class Predicate>
boost::filter_iterator<Predicate, map<string,int>::const_iterator> end(Predicate predicate) const
{
    return boost::make_filter_iterator(predicate, my_map.end(), my_map.end());
}

struct isMatch
{
    isMatch(const std::string prefix) {m_prefix = prefix;};
    bool operator()(std::string value)
    {
        return value.find_first_of(m_prefix) == 0;
    };
    std::string m_prefix;
};

//using:
isMatch startWithAb("Ab");
auto myBegin = boost::filter_iterator<startWithAb> begin();
auto myEnd = boost::filter_iterator<startWithAb> end();