交叉引用生成器

#include <vector>
#include <iostream>
#include <fstream>
#include<sstream>
#include <algorithm>
using namespace std;

using PAIR = pair<string, size_t>;

bool ReadFile(vector<PAIR>& words) {
	ifstream infile;
	infile.open("text.txt", ios::in);
	if (!infile.is_open())
	{
		cout << "读取文件失败" << endl;
		return -1;
	}

	string line, word;
	size_t word_line = 1;
	while (getline(infile, line))//逐行读
	{
		istringstream istrm(line);//创建字符串流
		while (istrm >> word)//逐单词读
		{
			transform(word.begin(), word.end(), word.begin(), ::tolower);
			words.push_back({ word, word_line });
		}
		word_line += 1;
	}

	infile.close();
	return 0;
}

bool Compare(const string& a, const string& b, unsigned int n = 0) {
	if (a.length() == n && b.length() == n) {
		return false;
	}
	else if (a.length() == n) {
		return true;
	}
	else  if (b.length() == n) {
		return false;
	}

	if (a[n] > b[n]) {
		return false;
	}
	else if (a[n] == b[n]) {
		Compare(a, b, ++n);
	}
	else {
		return true;
	}
}

bool MySort(const PAIR& first, const PAIR& end) {
	return Compare(first.first, end.first);
}

void show(const vector<PAIR>& words) {
	size_t num = 1;
	for (size_t i = 0; i < words.size(); ++i) {
		if (i != words.size() - 1 && words[i].first == words[i + 1].first) {
			++num;
			continue;
		}
		cout << words[i].first << "  " << num << " ";
		while (num--) {
			cout << words[i - num].second << " ";
		}
		cout << endl;
		num = 1;
	}
}

int main() {
	vector<PAIR> words;
	if (ReadFile(words) == -1) {
		return -1;
	}
	sort(words.begin(), words.end(), MySort);
	show(words);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值