LoginSignup
1
1

More than 5 years have passed since last update.

Platform::String^ を unordered_set の要素にする

Last updated at Posted at 2013-12-31

unordered_set, unordered_multiset,unordered_map, unordered_multimap の
要素Tには、std::hash が定義されていることが求められるため、そのままでは
Platform::String^ は unordered_XXX の要素になれない。

#include <unordered_set>

/*
 * std::hash<Platoform::String^> を定義する。
 * ハッシュ値は Platoform::String::GetHashCode() をそのまま利用できる。
 */
template<> struct std::hash<Platform::String^> {
    size_t operator()(Platform::String^ pstr) const {
        return static_cast<size_t>(pstr->GetHashCode());
    }
};

/* おためし */

#include <iostream>
#include <locale>
#include <string>

using namespace std;
using namespace Platform;

wostream& operator<<(wostream& stream, String^ str) {
    return stream << wstring(begin(str), end(str));
}

int main(Array<String^>^ ) {

    vector<String^> vps = 
      { L"れい", L"いち", L"に", L"さん", L"し", 
        L"ご", L"ろく", L"しち", L"はち", L"きゅう" };
    unordered_set<String^> uss(begin(vps), end(vps));

    wcout.imbue(locale("japanese"));
    for ( auto item : uss ) {
        wcout << item << endl;
    }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1