簡體   English   中英

C++ std::map<std::string, int> 獲取鍵以特定字符串開頭的值

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

我正在以這樣的方式使用 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

我想獲取其鍵以特定字符串開頭的所有值(例如“Ab”)。 我可以使用 map::lower_bound 輕松獲取開始迭代器。 但是我怎樣才能得到一個上限呢? 我是否必須從下限開始迭代整個集合並檢查每個鍵是否仍以“Ab”開頭?

我在這個頁面找到了一個類似的答案:( 地圖復雜的查找操作

代碼執行:

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
}

看起來好像在這個例子中你從 upper_bound 向后迭代直到開始尋找特定的子字符串

這個例子略有不同,但應該作為一個很好的構建塊

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;
}

該代碼適用於任何字符串鍵。

您可以使用Boost 過濾器迭代器,當它們給出謂詞時,它會從普通迭代中為您提供“開始”和“結束”迭代器(一個布爾函數,它說明要包含哪些值)

例如:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM