所以我决定尝试只使用向量类来制作我在上一个问题中编写的程序,但我仍然遇到错误。
错误:
error: 'match' does not name a type
error: 'match' was not declared in the scope
error: 'conversion from 'int' to non-scalar type 'WordInfo' requested
我的代码:
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct WordInfo {
string text;
int count;
};
int main(int argc, char** argv) {
enum {
total, unique, individual
} mode = total;
for (int c; (c = getopt(argc, argv, "tui")) != -1;) {
switch (c) {
case 't': mode = total;
break;
case 'u': mode = unique;
break;
case 'i': mode = individual;
break;
}
}
argc -= optind;
argv += optind;
string word;
vector<string> list;
vector<WordInfo> words;
int count = 0;
while (cin >> word) {
switch(mode){
case total :
count += 1;
break;
case unique :
if (find(list.begin(), list.end(), word) != list.end()){
} else {
count += 1;
}
break;
case individual :
if (find(list.begin(), list.end(), word) != list.end()) {
auto match = find(list.begin(), list.end(), word);
words.at(match - list.begin()).count++;
} else {
int count = 1;
WordInfo tmp = (word, i);
words.push_back(tmp);
}
}
}
switch (mode) {
case total: cout << "Total " << count << endl;
break;
case unique: cout << "Unique " << count << endl;
break;
case individual:
for (int i = 0; i < words.size(); i++){
cout << words.at(i); << endl;
}
break;
}
return 0;
}
任何和所有的帮助将不胜感激,谢谢。