c++ - 为什么在使用STL列表时不能使用此回文功能?

标签 c++ c++11 stl palindrome auto

我做了一个回文函数,该函数可以使用任何容器类型,并且它适用于字符串, vector 和双端队列,但是当我创建一个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 error list

在我运行的代码下面。
#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;
}

最佳答案

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

itr1 = s.end() - 1

为随机访问迭代器定义。您可以改用 header 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

如果需要,您可以将函数专门用于随机访问迭代器。

关于c++ - 为什么在使用STL列表时不能使用此回文功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60529984/

相关文章:

c++ - 如何在指针 vector 之间动态转换?

c++ - N 条价格路径的平均估计不会随着 N 次迭代而改变

c++ - 如何在 constexpr 中进行 strlen?

c++ - 运行时非多态容器

c++ - 如何使用反向迭代器从 `std::set` 删除元素?

c++ - 排序 vector<variant<...>> 不能通过 operator< 正常工作

c++ - 什么是指针引用?

c++ - Qt - QProcess 不工作

c++ - 递归 boost::variant 类型不能用 "-std=c++11 -stdlib=libc++"编译

c++ - C++ 中的多线程抛出线程构造函数失败 : Resource temporarily unavailable