C++ 编译器无法识别自动,最好的替换方法是什么?
所以我写了一个使用auto的程序,但是编译器似乎无法识别它,可能是早期的编译器 我想知道对于我的代码,什么是合适的变量来修复我的代码,这样我就不需要使用auto关键字了?我在想一个指向字符串的指针?或者字符串迭代器,尽管我不确定C++ 编译器无法识别自动,最好的替换方法是什么?,c++,variables,auto,C++,Variables,Auto,所以我写了一个使用auto的程序,但是编译器似乎无法识别它,可能是早期的编译器 我想知道对于我的代码,什么是合适的变量来修复我的代码,这样我就不需要使用auto关键字了?我在想一个指向字符串的指针?或者字符串迭代器,尽管我不确定 #include <cstdlib> #include <string> #include <iostream> #include <unistd.h> #include <alg
#include <cstdlib>
#include <string>
#include <iostream>
#include <unistd.h>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[]) {
enum MODE {
WHOLE, PREFIX, SUFFIX, ANYWHERE, EMBEDDED
} mode = WHOLE;
bool reverse_match = false;
int c;
while ((c = getopt(argc, argv, ":wpsaev")) != -1) {
switch (c) {
case 'w': // pattern matches whole word
mode = WHOLE;
break;
case 'p': // pattern matches prefix
mode = PREFIX;
break;
case 'a': // pattern matches anywhere
mode = ANYWHERE;
break;
case 's': // pattern matches suffix
mode = SUFFIX;
break;
case 'e': // pattern matches anywhere
mode = EMBEDDED;
break;
case 'v': // reverse sense of match
reverse_match = true;
break;
}
}
argc -= optind;
argv += optind;
string pattern = argv[0];
string word;
int matches = 0;
while (cin >> word) {
switch (mode) {
case WHOLE:
if (reverse_match) {
if (pattern != word) {
matches += 1;
cout << word << endl;
}
} else if (pattern == word) {
matches += 1;
cout << word << endl;
}
break;
case PREFIX:
if (pattern.size() <= word.size()) {
auto res = mismatch(pattern.begin(), pattern.end(), word.begin());
if (reverse_match) {
if (res.first != word.end()) {
matches += 1;
cout << word << endl;
}
} else if (res.first == word.end()) {
matches += 1;
cout << word << endl;
}
}
break;
case ANYWHERE:
if (reverse_match) {
if (!word.find(pattern) != string::npos) {
matches += 1;
cout << word << endl;
}
} else if (word.find(pattern) != string::npos) {
matches += 1;
cout << word << endl;
}
break;
case SUFFIX:
if (pattern.size() <= word.size()) {
auto res = mismatch(pattern.rbegin(), pattern.rend(), word.rbegin());
if (reverse_match) {
if (res.first != word.rend()) {
matches = +1;
cout << word << endl;
}
} else if (res.first == word.rend()) {
matches = +1;
cout << word << endl;
}
}
break;
case EMBEDDED:
if (reverse_match) {
if (!pattern.find(word) != string::npos) {
matches += 1;
cout << word << endl;}
} else if (pattern.find(word) != string::npos) {
matches += 1;
cout
<< word << endl;
}
break;
}
}
return (matches == 0) ? 1 : 0;
}
由于您使用auto来声明保存不匹配返回值的变量,因此可以将其替换为所述函数的返回类型。据报道,这将是
std::pair<InputIt1,InputIt2>.
std::pair。
在第一次使用时,这意味着InputIt1=InputIt2=std::string::iterator(std::string::begin的返回类型),结果类型应为
std::pair
在第二个示例中,它将是std::string::rbegin()的返回类型:
std::pair
我希望这能有所帮助,但输入这么长的类型名当然是相当繁琐的,而且应该优先选择auto,因此,如果您可以使用更现代的编译器(或者将正确的标志传递给当前的编译器(-std=c++11,例如g++),我建议您这样做。定义“编译器似乎无法识别它”。怎么用?你期望它做什么?哪一行?没有单一尺寸的替代品,您的一个用途是
std::pair
,而另一个用途是std::pair
,但我更好奇的是,为什么auto
没有发挥作用。所以我支持克里斯的问题。您使用的编译器是什么?在主帖子中添加了错误。@user1719605,但仍然没有回答问题。请问:你用的是什么编译器?(还有,这是错误的:if(!pattern.find(word)!=string::npos)
,您正在将bool
与pos\u类型进行比较。您需要在内部表达式周围添加一些参数。这种情况至少会发生两次)。对不起,g++,if(!(pattern.find(word)!=string::npos)
会起作用吗?
std::pair<InputIt1,InputIt2>.
std::pair<std::string::reverse_iterator,std::string::reverse_iterator>