自动无法被编译器识别,最好的替代品是什么?

时间:2013-10-18 03:50:28

标签: c++ variables auto

所以我编写了一个使用auto的程序,但是编译器似乎没有识别它,可能它是一个早期的编译器。

我想知道我的代码,什么是合适的变量来修复我的代码,这样我就不需要使用auto关键字了?我在想一个指向字符串的指针?或者字符串迭代器,虽然我不确定。

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

提前致谢!

我得到的错误:

main.cpp:70:26: error: 'res' does not name a type
main.cpp:73:29: error: 'res' was not declared in this scope
main.cpp:77:32: error: 'res' was not declared in this scope
main.cpp:97:26: error: 'res' does not name a type 
main.cpp:100:29: error: 'res' was not declared in this scope 
main.cpp:104:32: error: 'res' was not declared in this scope

1 个答案:

答案 0 :(得分:1)

由于您使用auto来声明保存不匹配返回值的变量,因此可以将其替换为所述函数的返回类型。根据{{​​3}},这将是

std::pair<InputIt1,InputIt2>.

在你第一次使用时,这意味着InputIt1 = InputIt2 = std :: string :: iterator(std :: string :: begin的返回类型),结果类型应该是     性病::对

在你的第二个中,它将是std :: string :: rbegin()的返回类型:

std::pair<std::string::reverse_iterator,std::string::reverse_iterator>

我希望这会有所帮助,但是输入这样长的类型名称当然是相当繁琐的,并且应该优先选择auto,imho,所以如果你可以使用更现代的编译器(或者将正确的标志传递给你当前的那个( - std = c ++ 11 for g ++))),我建议你这样做。