gpt4 book ai didi

c++ - vector 和结构错误

转载 作者:行者123 更新时间:2023-11-28 03:06:56 24 4
gpt4 key购买 nike

所以我决定尝试只使用 vector 类制作我在上一个问题中编写的程序,但我仍然遇到错误。

错误:

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;
}

非常感谢任何帮助,谢谢。

最佳答案

您确定您使用的是 C++11 吗?

auto 与早期编译器的含义完全不同。

如果您使用的是 g++,请尝试使用 -std=c++11 标志进行编译:

g++ -std=c++11 foobar.cpp

关于c++ - vector 和结构错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19440351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com