std::unordered_multimapのキーの一致条件を変える方法

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
けん

std::unordered_multimapのキーの一致条件を変える方法

#1

投稿記事 by けん » 12年前

はじめまして。けんと申します。

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

アバター
h2so5
副管理人
記事: 2212
登録日時: 15年前
住所: 東京
連絡を取る:

Re: std::unordered_multimapのキーの一致条件を変える方法

#2

投稿記事 by h2so5 » 12年前

std::unordered_multimapはハッシュテーブルを利用しているので、原理的に部分一致はできません。

けん

Re: std::unordered_multimapのキーの一致条件を変える方法

#3

投稿記事 by けん » 12年前

よくわかりませんが部分一致は元々出来ない仕様になっているんですね。
他のコンテナの使用を考えることにします。
h2so5さんご返信ありがとうございました。

閉鎖

“C言語何でも質問掲示板” へ戻る