146 lines
3.8 KiB
C++
146 lines
3.8 KiB
C++
|
#include <map>
|
|||
|
#include <iostream>
|
|||
|
#include <string>
|
|||
|
#include <vector>
|
|||
|
|
|||
|
#include "split.h"
|
|||
|
|
|||
|
using std::cin; using std::cout;
|
|||
|
using std::endl; using std::getline;
|
|||
|
using std::istream; using std::string;
|
|||
|
using std::vector; using std::map;
|
|||
|
|
|||
|
// 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)) {
|
|||
|
++line_number;
|
|||
|
|
|||
|
// break the input line into words
|
|||
|
vector<string> words = find_words(line);
|
|||
|
|
|||
|
// remember that each word occurs on the current line
|
|||
|
#ifdef _MSC_VER
|
|||
|
for (std::vector<string>::const_iterator it = words.begin();
|
|||
|
#else
|
|||
|
for (vector<string>::const_iterator it = words.begin();
|
|||
|
#endif
|
|||
|
it != words.end(); ++it)
|
|||
|
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
|
|||
|
#ifdef _MSC_VER
|
|||
|
for (std::map<string, vector<int> >::const_iterator it = ret.begin();
|
|||
|
#else
|
|||
|
for (map<string, vector<int> >::const_iterator it = ret.begin();
|
|||
|
#endif
|
|||
|
it != ret.end(); ++it) {
|
|||
|
// write the word
|
|||
|
cout << it->first << " occurs on line(s): ";
|
|||
|
|
|||
|
// followed by one or more line numbers
|
|||
|
#ifdef _MSC_VER
|
|||
|
std::vector<int>::const_iterator line_it = it->second.begin();
|
|||
|
#else
|
|||
|
vector<int>::const_iterator line_it = it->second.begin();
|
|||
|
#endif
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
#include <map> // <20><><EFBFBD><EFBFBD> map <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ洢ÿ<E6B4A2><C3BF><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>к<EFBFBD>
|
|||
|
#include <iostream> // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
#include <string> // <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
#include <vector> // <20><><EFBFBD>붯̬<EBB6AF><CCAC><EFBFBD><EFBFBD><EFBFBD>⣬<EFBFBD><E2A3AC><EFBFBD>ڴ洢ÿһ<C3BF>еĵ<D0B5><C4B5><EFBFBD>
|
|||
|
|
|||
|
#include "split.h" // <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD> split.h <20>⣬<EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷָ<DAB7><D6B8>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD>
|
|||
|
|
|||
|
using std::cin; // ʹ<>ñ<EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
using std::cout; // ʹ<>ñ<EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
using std::endl; // ʹ<>û<EFBFBD><C3BB>з<EFBFBD>
|
|||
|
using std::getline; // ʹ<><CAB9>getline<6E><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
using std::istream; // ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
using std::string; // ʹ<><CAB9><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
using std::vector; // ʹ<><CAB9>vector<6F><72><EFBFBD><EFBFBD>
|
|||
|
using std::map; // ʹ<><CAB9>map<61><70><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
// xref<65><66><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>г<EFBFBD><D0B3>ֵ<EFBFBD><D6B5>к<EFBFBD>
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> in <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8> find_words<64><73><EFBFBD><EFBFBD><EFBFBD>ڷָ<DAB7>ÿ<EFBFBD>еĵ<D0B5><C4B5>ʣ<EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ split <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
map<string, vector<int> >
|
|||
|
xref(istream& in,
|
|||
|
vector<string> find_words(const string&) = split)
|
|||
|
{
|
|||
|
string line; // <20><><EFBFBD><EFBFBD><EFBFBD>洢ÿһ<C3BF>е<EFBFBD><D0B5>ı<EFBFBD>
|
|||
|
int line_number = 0; // <20>к<EFBFBD>
|
|||
|
map<string, vector<int> > ret; // <20><>map<61><70><EFBFBD>洢ÿ<E6B4A2><C3BF><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>к<EFBFBD>
|
|||
|
|
|||
|
// <20><><EFBFBD>ж<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
|||
|
while (getline(in, line)) {
|
|||
|
++line_number; // <20><><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD>
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>find_words<64><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ı<EFBFBD><C4B1>ָ<EFBFBD><D6B8>ɵ<EFBFBD><C9B5><EFBFBD>
|
|||
|
vector<string> words = find_words(line);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>ǰ<EFBFBD>к<EFBFBD><D0BA><EFBFBD><EFBFBD>ӵ<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ʵĶ<CAB5>Ӧλ<D3A6><CEBB>
|
|||
|
for (vector<string>::const_iterator it = words.begin(); it != words.end(); ++it)
|
|||
|
ret[*it].push_back(line_number); // <20><><EFBFBD><EFBFBD><EFBFBD>ʺ<EFBFBD><CABA>кŶ<D0BA>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
return ret; // <20><><EFBFBD>ذ<EFBFBD><D8B0><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>кŵ<D0BA>map
|
|||
|
}
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD> xref <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>ʹ<EFBFBD><CAB9> split <20><><EFBFBD>ָ<D6B8><EEB5A5>
|
|||
|
map<string, vector<int> > ret = xref(cin);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
for (map<string, vector<int> >::const_iterator it = ret.begin(); it != ret.end(); ++it) {
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
cout << it->first << " occurs on line(s): ";
|
|||
|
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʳ<EFBFBD><CAB3>ֵ<EFBFBD><D6B5>к<EFBFBD>
|
|||
|
vector<int>::const_iterator line_it = it->second.begin();
|
|||
|
cout << *line_it; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>к<EFBFBD>
|
|||
|
|
|||
|
++line_it;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD>
|
|||
|
while (line_it != it->second.end()) {
|
|||
|
cout << ", " << *line_it;
|
|||
|
++line_it;
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD>
|
|||
|
cout << endl;
|
|||
|
}
|
|||
|
|
|||
|
return 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
|