C++ STL 练习1

#include "stdafx.h"

#include <ctype.h>
#include <stddef.h>

typedef pair<short, short> location;
typedef vector<location> loc;
typedef vector<string> text;
typedef pair<text*, loc*> text_loc;


/* 定义全局变量*/
vector<string> *lines_of_text; /* 将文本存储到该向量中 */
text_loc *text_locations;   /* 存储每一个单词的位置向量 */
map<string, loc*, less<string>> * word_map; /* 每一个单词的位置集合 */

string filt_elems("\",.;:!<<)(\\/");


void retrieve_text();
void separate_words();
void filter_text();
void strip_caps();
void suffix_text();
void suffix_s( string& );
void build_word_map();

int main()
{
	retrieve_text();

	vector<string>::iterator it = (*lines_of_text).begin();

	while ( it != (*lines_of_text).end() )
	{
		cout<< *it <<endl;
		++it;
	}
	separate_words();

	vector<string> *words = text_locations->first;
	vector<string>::iterator it1 = (*words).begin();
	vector<location> * locals = text_locations->second;
	vector<location>::iterator it2 = (*locals).begin();

	for ( it1, it2; it1 != (*words).end() && it2 != (*locals).end(); ++it1, ++it2 )
	{
		cout<< *it1 <<" : " << (*it2).first << (*it2).second <<endl;
	}

	

	return 0;
}

void retrieve_text()
{
	string file_name;
	cout << "please input file name: ";
	cin >> file_name;

	ifstream infile( file_name.c_str(), ios::in );
	if ( !infile )
	{
		cerr << "oops! unable to open file "
			 << file_name << " -- bailing out \n ";
		exit( -1 );
	}
	else
	{
		cout<< "\n";
	}

	lines_of_text = new vector<string>;
	string textline;
	while( getline(infile,textline, '\n' ) )
	{
		lines_of_text->push_back(textline);
	}

}

void separate_words()
{
	short line_pos;
	vector<string>* words = new vector<string>;
	vector<location>* locations = new vector<location>;

	for ( line_pos = 0; line_pos < lines_of_text->size(); ++line_pos )
	{
		string textline = (*lines_of_text)[line_pos];
		string::size_type pos = 0, pre_pos = 0;
		short words_pos = 0;

		while ( (pos = textline.find_first_of(' ', pos )) != string::npos )
		{
			words->push_back( textline.substr(pre_pos, pos - pre_pos ));
			locations->push_back( make_pair(line_pos, words_pos ));
			words_pos++;
		    pos++;
			pre_pos = pos;
		}

		words->push_back( textline.substr(pre_pos, pos - pre_pos ));
		locations->push_back( make_pair(line_pos, words_pos ));

	}
	
	text_locations = new text_loc(words, locations );
	
	

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值