std::unordered_multimapのキーの一致条件を変える方法
Posted: 2013年10月02日(水) 12:24
はじめまして。けんと申します。
std::unordered_multimapのキーにstd::stringを指定してequal_range、findで文字列の部分一致による検索がしたいです。
http://program.station.ez-net.jp/specia ... ap-key.asp
こちらのstd::unordered_map<> のキーの一致条件を自分で決めるというサンプルコード
を元に以下のテストコードを作成してみたのですが、何故か結果が全体一致となってしまいます。
環境はVS2012Proです。解決策をご存じの方ご教授よろしくお願いいたします。
std::unordered_multimapのキーにstd::stringを指定してequal_range、findで文字列の部分一致による検索がしたいです。
http://program.station.ez-net.jp/specia ... ap-key.asp
こちらのstd::unordered_map<> のキーの一致条件を自分で決めるというサンプルコード
を元に以下のテストコードを作成してみたのですが、何故か結果が全体一致となってしまいます。
環境はVS2012Proです。解決策をご存じの方ご教授よろしくお願いいたします。
#include <iostream>
#include <string>
#include <unordered_map>
class Hash
{
public:
size_t operator()(const std::string& source) const
{
return std::hash<std::string>()(source);
}
};
class EqualTo : public std::binary_function<std::string, std::string, bool>
{
public:
typedef std::string first_argument_type;
typedef std::string second_argument_type;
typedef bool result_type;
bool operator()(const std::string& lhs, const std::string& rhs) const
{
return lhs.find(rhs) != std::string::npos;
}
};
int main()
{
std::unordered_multimap<std::string,int,Hash,EqualTo> a;
a.insert(std::make_pair("1",1));
a.insert(std::make_pair("12",2));
auto its = a.equal_range("1");
for(auto it=its.first;it!=its.second;++it)
{
std::cout<<it->second<<"\n";
}
}