⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 7-3.cpp

📁 Accelerated C++ 课后练习题 本人自己完成、可供参考
💻 CPP
字号:
#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> split(const string& s)
{
	vector<string> ret;
	typedef string::size_type string_size;
	string_size i=0;

	//ivarinant: we have processed characters [original value of i, i)
	while(i!=s.size())
	{
		//ignore leading blanks
		//invariant: characters in range [original i, current i) are all spaces
		while(i!=s.size()&&isspace(s[i]))
			++i;
		//find end of next word
		string_size j=i;
		//invariant: none of the characters in range [original j,current j)is a space
		while(j!=s.size()&&!isspace(s[j]))
			j++;
		//if we found some nonwhitespace characters
		if(i!=j)
		{
			//copy from s starting at i and taking j-i chars
			ret.push_back(s.substr(i,j-i));
			i=j;
		}
	}
	return ret;
}

//find all the lines that refer to each word in the input
map<string, vector<int> >
xref(istream& in,
	 vector<string> find_words(const string&)=split)
{
	string line;
	int line_number=0;
	map<string, vector<int> > ret;

	//read the next line
	while(getline(in, line))
	{
		if(line=="exit")
			break;
		++line_number;

		//break the input line into words
		vector<string> words=find_words(line);

		//remember that each word occurs on the current line
		for(vector<string>::const_iterator it=words.begin();
			it!=words.end();++it)
				if(find(ret[*it].begin(),ret[*it].end(),line_number)==ret[*it].end())
				ret[*it].push_back(line_number);
			
	}
	return ret;
}

int main()
{
	//call xref using split by default
	map<string, vector<int> > ret=xref(cin);

	//write the results

	for(map<string, vector<int> >::const_iterator it=ret.begin();it!=ret.end();++it)
	{
		//write the word 
		cout<<it->first<<"\toccurs on line(s): ";

		//followed by one or more line numbers
		vector<int>::const_iterator line_it=it->second.begin();
		cout<<*line_it;			//write the  first line number

		++line_it;
		//write the rest of the line numbers, if any
		while(line_it!=it->second.end())
		{
			cout<<", "<<*line_it;
			++line_it;
		}
		//write a new line to separate each word from the next
		cout<<endl;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -