优先级队列库中的 C++ 推送函数在我的环境中不起作用

C++ push function in priority queue library won't work on my environment

我试图解决一个竞争性编程问题(在 AtCoder 上),我想使用优先级队列,但后来我遇到了一些问题。

我写了下面的代码,但是当我试图编译它时,我收到错误消息,说“没有匹配的成员函数来调用 'push'” (完整的错误信息如下所示)

我不知道为什么会这样,我在AtCoder Code Test在线评委上试了一下。

https://atcoder.jp/contests/abc176/custom_test

在在线判断上完全没问题。

除此之外,我和我的朋友谈过这个问题,他说代码在他的环境中工作得很好(他使用 Windows)。

这是代码。

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
#include <iomanip>
#include <ctime>
#include <complex>

using namespace std;

int main()
{
    priority_queue<array<int, 3>> pq;
    pq.push({0, 1, 4});

}

我用vscode(版本:1.49.1)编写代码并构建它,这是我当时收到的错误消息。

> Executing task: g++ -std=c++14 -g -O2 abc176_d_11.cpp -o abc176_d_11 <

abc176_d_11.cpp:27:8: error: no matching member function for call to 'push'
    pq.push({0, 1, 4});
    ~~~^~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:536:10: note: candidate function not viable: cannot convert initializer list argument to
      'const std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >,
      std::__1::less<std::__1::array<int, 3> > >::value_type' (aka 'const std::__1::array<int, 3>')
    void push(const value_type& __v);
         ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:539:10: note: candidate function not viable: cannot convert initializer list argument to
      'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >,
      std::__1::less<std::__1::array<int, 3> > >::value_type' (aka 'std::__1::array<int, 3>')
    void push(value_type&& __v);
         ^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:426:68: error: implicit instantiation of undefined template 'std::__1::array<int, 3>'
        __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__soon_to_be_end));
                                                                   ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:369:29: note: in instantiation of member function
      'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::__destruct_at_end' requested here
    void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
                            ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:463:9: note: in instantiation of member function
      'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::clear' requested here
        clear();
        ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of member function
      'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
    ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of member function 'std::__1::vector<std::__1::array<int,
      3>, std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
        : c(), comp() {}
          ^
abc176_d_11.cpp:26:35: note: in instantiation of member function 'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int,
      3>, std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> > >::priority_queue' requested here
    priority_queue<array<int, 3>> pq;
                                  ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
                                                               ^
In file included from abc176_d_11.cpp:3:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:14:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:504:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string_view:175:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__string:56:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:643:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1816:55: error: implicit instantiation of undefined template 'std::__1::array<int, 3>'
        {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
                                                      ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1554:14: note: in instantiation of member function
      'std::__1::allocator<std::__1::array<int, 3> >::deallocate' requested here
        {__a.deallocate(__p, __n);}
             ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:464:25: note: in instantiation of member function
      'std::__1::allocator_traits<std::__1::allocator<std::__1::array<int, 3> > >::deallocate' requested here
        __alloc_traits::deallocate(__alloc(), __begin_, capacity());
                        ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of member function
      'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
    ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of member function 'std::__1::vector<std::__1::array<int,
      3>, std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
        : c(), comp() {}
          ^
abc176_d_11.cpp:26:35: note: in instantiation of member function 'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int,
      3>, std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> > >::priority_queue' requested here
    priority_queue<array<int, 3>> pq;
                                  ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
                                                               ^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:372:52: error: implicit instantiation of undefined template 'std::__1::array<int, 3>'
        {return static_cast<size_type>(__end_cap() - __begin_);}
                                                   ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:464:57: note: in instantiation of member function
      'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::capacity' requested here
        __alloc_traits::deallocate(__alloc(), __begin_, capacity());
                                                        ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of member function
      'std::__1::__vector_base<std::__1::array<int, 3>, std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
    ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of member function 'std::__1::vector<std::__1::array<int,
      3>, std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
        : c(), comp() {}
          ^
abc176_d_11.cpp:26:35: note: in instantiation of member function 'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int,
      3>, std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> > >::priority_queue' requested here
    priority_queue<array<int, 3>> pq;
                                  ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
                                                               ^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:872:54: error: implicit instantiation of undefined template 'std::__1::array<int, 3>'
      __annotate_contiguous_container(data(), data() + capacity(),
                                                     ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:551:9: note: in instantiation of member function 'std::__1::vector<std::__1::array<int,
      3>, std::__1::allocator<std::__1::array<int, 3> > >::__annotate_delete' requested here
        __annotate_delete();
        ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:427:28: note: in instantiation of member function 'std::__1::vector<std::__1::array<int,
      3>, std::__1::allocator<std::__1::array<int, 3> > >::~vector' requested here
class _LIBCPP_TEMPLATE_VIS priority_queue
                           ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
                                                               ^
5 errors generated.
The terminal process "/usr/local/bin/bash '-c', 'g++ -std=c++14 -g -O2 abc176_d_11.cpp -o abc176_d_11'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

之后我尝试从正常(我的意思是,不在 vscode)终端(我使用 iTerm)编译它,但得到了类似的错误消息。 这是我尝试在终端上编译代码时收到的错误消息。

[my_computer_name]@MacBook-Pro:~/coder$ g++ abc176_d_11.cpp
abc176_d_11.cpp:27:8: error: no matching member function for call to 'push'
    pq.push({0, 1, 4});
    ~~~^~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:536:10: note: candidate function
      not viable: cannot convert initializer list argument to 'const
      std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> >
      >::value_type' (aka 'const std::__1::array<int, 3>')
    void push(const value_type& __v);
         ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:539:10: note: candidate function
      not viable: cannot convert initializer list argument to
      'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> >
      >::value_type' (aka 'std::__1::array<int, 3>')
    void push(value_type&& __v);
         ^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:426:68: error: implicit
      instantiation of undefined template 'std::__1::array<int, 3>'
        __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__soon_to_be_end));
                                                                   ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:369:29: note: in instantiation of
      member function 'std::__1::__vector_base<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >::__destruct_at_end' requested here
    void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
                            ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:463:9: note: in instantiation of
      member function 'std::__1::__vector_base<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >::clear' requested here
        clear();
        ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of
      member function 'std::__1::__vector_base<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
    ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of
      member function 'std::__1::vector<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
        : c(), comp() {}
          ^
abc176_d_11.cpp:26:35: note: in instantiation of member function
      'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> >
      >::priority_queue' requested here
    priority_queue<array<int, 3>> pq;
                                  ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is
      declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
                                                               ^
In file included from abc176_d_11.cpp:3:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:14:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:504:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string_view:175:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__string:56:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:643:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1816:55: error: implicit
      instantiation of undefined template 'std::__1::array<int, 3>'
        {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
                                                      ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1554:14: note: in instantiation
      of member function 'std::__1::allocator<std::__1::array<int, 3> >::deallocate' requested here
        {__a.deallocate(__p, __n);}
             ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:464:25: note: in instantiation of
      member function 'std::__1::allocator_traits<std::__1::allocator<std::__1::array<int, 3> >
      >::deallocate' requested here
        __alloc_traits::deallocate(__alloc(), __begin_, capacity());
                        ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of
      member function 'std::__1::__vector_base<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
    ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of
      member function 'std::__1::vector<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
        : c(), comp() {}
          ^
abc176_d_11.cpp:26:35: note: in instantiation of member function
      'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> >
      >::priority_queue' requested here
    priority_queue<array<int, 3>> pq;
                                  ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is
      declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
                                                               ^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:372:52: error: implicit
      instantiation of undefined template 'std::__1::array<int, 3>'
        {return static_cast<size_type>(__end_cap() - __begin_);}
                                                   ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:464:57: note: in instantiation of
      member function 'std::__1::__vector_base<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >::capacity' requested here
        __alloc_traits::deallocate(__alloc(), __begin_, capacity());
                                                        ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:495:5: note: in instantiation of
      member function 'std::__1::__vector_base<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >::~__vector_base' requested here
    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
    ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:447:11: note: in instantiation of
      member function 'std::__1::vector<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >::vector' requested here
        : c(), comp() {}
          ^
abc176_d_11.cpp:26:35: note: in instantiation of member function
      'std::__1::priority_queue<std::__1::array<int, 3>, std::__1::vector<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >, std::__1::less<std::__1::array<int, 3> >
      >::priority_queue' requested here
    priority_queue<array<int, 3>> pq;
                                  ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is
      declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
                                                               ^
In file included from abc176_d_11.cpp:8:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:872:54: error: implicit
      instantiation of undefined template 'std::__1::array<int, 3>'
      __annotate_contiguous_container(data(), data() + capacity(),
                                                     ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:551:9: note: in instantiation of
      member function 'std::__1::vector<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >::__annotate_delete' requested here
        __annotate_delete();
        ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/queue:427:28: note: in instantiation of
      member function 'std::__1::vector<std::__1::array<int, 3>,
      std::__1::allocator<std::__1::array<int, 3> > >::~vector' requested here
class _LIBCPP_TEMPLATE_VIS priority_queue
                           ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__tuple:219:64: note: template is
      declared here
template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
                                                               ^
5 errors generated.

这是我的环境。

MacBook Pro
macOS Catalina
Version 10.15.5

我仍然是编程的初学者,尤其是 c++,如果我的问题没有意义或看起来很愚蠢,我想提前道歉。 我的英语也不好

如果能得到您的帮助,我将不胜感激。 谢谢。

你有旧版的g++吗?早在 4.8.1(从 2013 年开始)我就尝试编译你的代码,它在 c++11 模式下工作。在更早的版本中,它不知道某些 headers.
(当前版本为10.2.0)

我建议您尝试在 Compiler Explorer 上运行您的代码。它使您可以访问在线版本的 g++、clang、msvc++ 和其他版本,可以追溯到许多版本之前,无需任何设置或安装任何麻烦。 这是您使用 g++ 4.8 的代码:

https://godbolt.org/z/5TTjqd

关键错误信息是这样的:

implicit instantiation of undefined template 'std::__1::array<int, 3>'

这表示 array 未定义。因此,在您的环境中,您明确需要 #include<array>

最小的工作示例是这样的:

#include <array>
#include <queue>

using namespace std;

int main()
{
    priority_queue<array<int, 3>> pq;
    pq.push({0, 1, 4});
}

请注意,我明确包含 <array><queue>(按字母顺序排列)。这是一个黄金标准:如果你使用一个库,直接包含它,不要指望间接包含。在你的包含列表中保持一些顺序。不要包括你不需要的。