Skip to content

Instantly share code, notes, and snippets.

@hymkor
Created December 12, 2014 21:37
Show Gist options
  • Save hymkor/152e0e58e2b7621be517 to your computer and use it in GitHub Desktop.
Save hymkor/152e0e58e2b7621be517 to your computer and use it in GitHub Desktop.
C++ の find を TryGetValue 的な関数に置き換えてみたが、あまり意味がなかった。
#include <string>
#include <map>
#include <iostream>
template <class C,class K,class V>
bool inline lookup(C &c,const K &k,V &v)
{
v = c.find(k); return v != c.end();
}
int main()
{
std::map<std::string,std::string> map1;
map1["ahaha"] = "ihihi";
std::map<std::string,std::string>::iterator p;
// auto p=map1::end(); // $ g++ -std=c++11 foo.cpp
if( lookup(map1,"ahaha",p) ){
std::cout << p->second << std::endl;
}else{
std::cout << "not found\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment