简体   繁体   English

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

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

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").我想获取其键以特定字符串开头的所有值(例如“Ab”)。 I can easily get the begin iterator using map::lower_bound.我可以使用 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”开头?

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这个例子略有不同,但应该作为一个很好的构建块

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)
{
    if(s.length() < key_.length())
        return false;

    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.该代码适用于任何字符串键。

you can use Boost filter iterator which 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是等价于 std::map 的 trie<std::string, int> 在 C++ 中?</std::string,> - Is a trie equivalent to std::map<std::string, int> in C++? C ++ std :: map缓存,使用std :: string作为键值 - C++ std::map cache using std::string as the key value C ++ std ::地图 <std::string, std::set<std::string> &gt;。 如何循环设置值? - c++ std::map<std::string, std::set<std::string>> . How to loop set values? c++ 标准::map<std::string, int> 与 std::string_view 一起使用</std::string,> - c++ std::map<std::string, int> use at with std::string_view 通过C ++套接字(linux)发送std :: map <int,std :: map <std :: string,double >> - Sending an std::map<int, std::map<std::string, double> > over C++ socket (linux) C ++ std :: map和std :: pair <int, int> 作为关键 - C++ std::map and std::pair<int, int> as Key C ++ - std :: wstring to std :: string - 快速和脏转换,用作std :: map中的键 - C++ - std::wstring to std::string - quick and dirty conversion for use as key in std::map 如何检查 C++ std::string 是否以某个字符串开头,并将子字符串转换为 int? - How do I check if a C++ std::string starts with a certain string, and convert a substring to an int? c++:转换std::map<std::string, double> 到 std::map<std::string_view, double></std::string_view,></std::string,> - c++ : convert std::map<std::string, double> to std::map<std::string_view, double> 使用参数 Map 调用 Java 函数<String, String>来自带有 std::map 的 C++<std::string, std::string> - Call Java function with parameter Map<String, String> from c++ with std::map<std::string, std::string>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM