首页 > 解决方案 > 为什么我在使用 STL 列表时不能使用这个回文函数?

问题描述

我做了一个回文函数,它允许我使用任何容器类型,它适用于字符串、向量和双端队列,但是当我创建一个 STL 列表并尝试运行代码时,我得到了下面的错误,我无法弄清楚这意味着什么,该怎么做。

Severity    Code    Description Project File    Line    Suppression State
Error   C2676   binary '-': 'std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' does not define this operator or a conversion to a type acceptable to the predefined operator homework4   C:\...\Source.cpp   9   

Severity    Code    Description Project File    Line    Suppression State
Error   C3536   'itr1': cannot be used before it is initialized homework4   C:\...\Source.cpp   9   

Severity    Code    Description Project File    Line    Suppression State
Error   C2676   binary '<': 'std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' does not define this operator or a conversion to a type acceptable to the predefined operator homework4   C:\...\Source.cpp   9   
Severity    Code    Description Project File    Line    Suppression State
Error   C2100   illegal indirection homework4   C:\...\Source.cpp   10  

Visual Studio 错误列表

在我运行的代码下方。

#include <iostream>
#include<list>
#include<vector>
#include<deque>
using namespace std;

template <typename Container>
bool palindrome(const Container& s) {
    for (auto itr = s.begin(), itr1 = s.end() - 1; itr < itr1; itr++, itr1--) {
        if (*itr != *itr1)
            return false;
    }
    return true;
}

void main() {
    const string word1{ "racecar" };
    const vector<char> word2{ 'a', 'b', 'b', 'a' };
    const deque<int> word3{ 83, 84, 65, 84, 83 };
    const list<int> word4{ 83, 84, 65, 84, 83 };
    word4.end();

    cout << palindrome< string >(word1) << endl;
    cout << palindrome< vector<char> >(word2) << endl;
    cout << palindrome< deque<int> >(word3) << endl;
    cout << palindrome< list<int> >(word4) << endl;
}

标签: c++c++11stlpalindromeauto

解决方案


标准容器 std::list 具有双向迭代器。运算符 - 在此表达式中使用

itr1 = s.end() - 1

为随机访问迭代器定义。您可以改用std::prev标头中声明的标准函数,<iterator>例如

itr1 = std::prev( s.end() )

然而在任何情况下,该函数都是无效的,因为通常该函数的用户可以传递一个空容器。在这种情况下,表达式std::prev( s.end() )具有未定义的行为。

并且操作符 < 没有为双向迭代器定义。所以这个表达式

itr < itr1

也可能不会在函数中使用。

此外,该函数不适用于数组,因为数组没有成员函数,例如begin()

该函数可以如下所示,如下面的演示程序所示。

#include <iostream>
#include <iomanip>
#include <string>
#include <deque>
#include <vector>
#include <list>
#include <iterator>

template <typename Container>
bool palindrome( const Container &c ) 
{
    auto first = std::begin( c );
    auto last  = std::end( c );

    while ( first != last && first != --last && *first == *last ) ++first;      

    return first == last;
}

int main() 
{
    const std::string word1{ "racecar" };
    const std::vector<char> word2{ 'a', 'b', 'b', 'a' };
    const std::deque<int> word3{ 83, 84, 65, 84, 83 };
    const std::list<int> word4{ 83, 84, 65, 84, 83 };
    const char word5[] =  { 83, 84, 65, 84, 83 };

    std::cout << std::boolalpha << palindrome( word1 ) << '\n';
    std::cout << std::boolalpha << palindrome( word2 ) << '\n';
    std::cout << std::boolalpha << palindrome( word3 ) << '\n';
    std::cout << std::boolalpha << palindrome( word4 ) << '\n';
    std::cout << std::boolalpha << palindrome( word5 ) << '\n';

    return 0;
}

程序输出为

true
true
true
true
true

如果您愿意,您可以专门针对随机访问迭代器的功能。


推荐阅读