如何修复算法 header 文件中的这个错误?

How do I fix this error in the algorithm header file?

我目前正在编写一个接收 string 作为输入的代码,检查它是否仅由 car_acc 数组中的字符组成,如果不是情况下,使用 do-while 循环再次请求输入。

这是代码:

    char car_acc [11] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ':'};
    deque <bool> contr_car;
    
    do
    {
        cout << "Inserire il tempo di attesa (ore:minuti:secondi) -> ";
        cin >> tempo; fflush (stdin);
        lung = (int) tempo.length ()-1;
        
        for (; i != lung; i+=1)
        {
            contr_car [i] = false;
        }
        
        for (i = 0; i != 11; i+=1)
        {
            if (car_acc [i] == tempo [j])
            {
                contr_car [j] = true;
            }
        }
    }
    while (all_of (contr_car [0], contr_car [lung], true) == false);

我正在使用 contr_car 双端队列来存储 string 中的每个字符是否等于 car_acc 数组中存储的字符之一。

我收到这个错误,在 algorithm header 文件中链接:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:926:21: Indirection requires pointer operand ('int' invalid)

错误发生在定义 all_of 函数的文件部分:

template <class _InputIterator, class _Predicate>
inline _LIBCPP_INLINE_VISIBILITY
bool
all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
{
    for (; __first != __last; ++__first)
        if (!__pred(*__first)) // error's here
            return false;
    return true;
}

我也在 while:

的同一行收到这条消息
In instantiation of function template specialization 'std::__1::all_of<bool, bool>' requested here

使用 find () 函数代替 all_of () 并不能修复错误。

我正在使用双端队列,因为我必须在循环外声明它,在循环内我得到大小,所以数组不是一个选项,我真的不知道如何使用 vector <bool>,因为它作为位集工作。 我 确实 包括了 algorithmdeque header。 我尝试使用 vector <int> 但它仍然不起作用,它只会更改我在 while 所在的行上收到的消息:

In instantiation of function template specialization 'std::__1::all_of<int, int>' requested here

老实说,我不知道如何修复它,因为我刚刚开始使用 C++ 和一般编码,所以如果您能用 easy-to-understand 方式表达您的答案,我将非常感激。

算法 std::all_of 旨在对迭代序列中的每个元素调用一元谓词,从每个元素中获取 'true' 或 'false',并用一个包含 'true'(所有报告为真)或 'false'(至少有一件事报告为假)。为此,您需要提供三样东西:

  • 一个开始的迭代器
  • 结束迭代器
  • 返回布尔值的可调用一元谓词。

您的代码恰好提供了其中的 none,因此它无法编译也就不足为奇了。下面显示了如何使用 std::all_of 的示例。它不打算成为 cookie-cutter,所以 不要有其他想法。

#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <string>

int main()
{
    const std::unordered_set<char> car_acc = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ':'};
    std::string tempo;

    do
    {
        std::cout << "Inserire il tempo di attesa (ore:minuti:secondi) -> ";
        std::cout.flush();

        if (!(std::cin >> tempo))
        {
            // TODO: handle failure on stdin
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }

    } while ( !std::all_of(tempo.begin(), tempo.end(), 
        [&car_acc](char c){return car_acc.find(c) != car_acc.end();}) );

    std::cout << "You entered: " << tempo << '\n';
}

这只是创建一个无序集,其中包含您认为是“允许”的字符,然后使用该集作为此处一元谓词的基础,一个简单的 lambda:

[&car_acc](char c){return car_acc.find(c) != car_acc.end();}

然后使用字符串开始和结束迭代器将其馈入 std::all_of。如果字符串中的任何字符未能在集合中找到匹配项,则 match-check 的结果将为假,因此也应为 std::all_of.

的结果