boost::regexの使い方 (Ubuntu 10.04)
インストール
> sudo apt-get install libboost-dev libboost-regex-dev
インクルード
#include <boost/regex.hpp>
コンパイル
> g++ hoge.cpp -lboost_regex
形態素解析エンジン mecabのインストール (Ubuntu 10.04)
文字コードがECUの場合
> sudo apt-get install mecab mecab-ipadic libmecab-dev
文字コードがUTF-8の場合
> sudo apt-get install mecab mecab-ipadic-utf8 libmecab-dev
std::mapの最大値を返す関数 (C++ STL)
#include <map>
int max_value(std::map<int, int> m) {
int max = 0;
for (std::map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
if (max < it->second) max = it->second;
}
return max;
}
落とし穴
max_element()でいけると思いきや...
#include <map>
#include <algorithm>
/*
* This function has a bug returning the value associated to the maximum key
* instead of maximum value in the map.
*/
int max(std::map<int, int> m) {
std::map<int, int>::iterator max_it = max_element(m.begin(), m.end());
return max_it->second;
}
試してみると...
#include <iostream>
int main(void) {
std::map<int, int> map;
map[100] = 2;
map[200] = 9;
map[300] = 3;
map[250] = 4;
std::cout << "max = " << max(map) << std::endl;
return 0;
}
valueの最大値ではなく、最大のkeyに対応するvalueが返されてしまう。
$ ./max_element_test max = 3
Copyright (C) 2009 - 2011 Kazuhiro Tobe <
>. All Rights Reserved.