strtokとmap
#include <iostream> #include <string> #include <map> using namespace std; int main(int arg, char *argv[]){ char *token; // char ss = argv[1]; char seps[] = " !$\"%^&*()_+=#{}[]:;'`/?,.\t\n\u00a3"; int i = 0; char test[] = "this is a pen. that is a cat."; // char * test = "this is a pen. that is a cat."; // Segmentation fault (core dumped) string m_token; map<string, int> m1; map<string, int>::iterator pa; token = strtok(test, seps); while( token != NULL ) { // char *m_token = token; m_token = token; cout << m_token << endl; if( (pa = m1.find(m_token)) != m1.end()){ //すでにマップにあるのなら pa->second = pa->second + 1; //二つ目の数値を加算 } else{// そうでないなら,データを挿入 m1.insert(make_pair(m_token, 1)); } // 次のトークンを取得します。 i++; token = strtok( NULL, seps ); } pa = m1.begin(); int n = (int)m1.size(); for (int i = 0; i < n; i++) { cout << pa->first << " " << pa->second << endl; pa++; } }
Link: Programming(5250d)
Programming_C(5701d)
Last-modified: 2008-03-25 (火) 11:36:10 (6141d)