![](/img/trans.png)
[英]Is a trie equivalent to std::map<std::string, int> in C++?
[英]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;
}
我想获取其键以特定字符串开头的所有值(例如“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.