分类: C/C++
2007-11-11 09:02:25
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
typedef pair
typedef vector
typedef vector
typedef pair
typedef map
text_loc* separate_words(const vector
{
vector
vector
short line_pos=0; //行(句子条数)
for(; line_pos < text_file->size(); ++line_pos)
{
short word_pos=0; //列(一行单词个数)
string textline=(*text_file)[line_pos];
string::size_type pos =0 ,prev_pos=0;
while(( pos=textline.find_first_of(' ',pos)) !=string::npos)
{
words->push_back( textline.substr(prev_pos, pos-prev_pos));
locations->push_back( make_pair(line_pos,word_pos));
++word_pos; prev_pos=++pos;
}
words->push_back( textline.substr(prev_pos,pos-prev_pos));
locations->push_back( make_pair(line_pos,word_pos));
}
return new text_loc(words,locations);
}
map
{
map
vector
vector
register int elem_cnt=text_words->size();
for(int ix=0;ix
string textword=(*text_words)[ix];
if(textword.size()<3)
continue;
if(!word_map->count(textword)) //是否有此关键字,无则新加键/值,有则只加新值,形成一键一(多)值关系
{
loc *ploc=new vector
ploc->push_back((*text_locs)[ix]);
word_map->insert(valType(textword,ploc));
}
else
(*word_map)[textword]->push_back((*text_locs)[ix]);
}
return word_map;
}
void main()
{
string ia[4]={"Alice Emma has long",
"wind blows",
"bird has flight",
"Daddy, shush, has"};
vector
const vector
const text_loc *text_locations=separate_words(text_file);
map
vector
for(int i=0;i<(*locat).size();i++)
{
cout << (*locat)[i].first << " " << (*locat)[i].second << endl;
}
}