C++:STL数组的STL排序STL vector

标签 c++ c++11 stl stl-algorithm

以下 C++ 代码和 Makefile 会产生一个无法理解的编译错误(对我来说)。谁能解释一下

  1. 问题到底是什么?

  2. 需要做什么来修复此代码?能举个例子吗?

我在 Cygwin 的 GCC 上成功编译了这段代码,所以我什至不确定我可以信任哪个编译器。我附加了 cpp 文件 (test.cpp)、我的 makefile(使用 clang,因为 GCC 错误消息更糟糕)以及运行 make 时的实际错误消息。

测试.cpp:

#include <array>
#include <vector>
#include <map>
#include <algorithm>

class Foo
{

    public:

        Foo( 
            const std::vector<std::array<int,3>> inputdata
        );

    private:

        std::vector< std::array<int,2> > membervariable;

};

Foo::Foo( 
    const std::vector<std::array<int,3>> inputdata
)
:
    membervariable( 0 )
{

    /* 1. create all edges, allocate memory */

    membervariable.resize( inputdata.size() * 3 );

    for( int t  = 0; t  < inputdata.size(); t++  )
    for( int ei = 0; ei <                        3; ei++ )
    {
      membervariable[ 0 * inputdata.size() + t ] = { inputdata[t][0], inputdata[t][1] };
      membervariable[ 1 * inputdata.size() + t ] = { inputdata[t][0], inputdata[t][2] };
      membervariable[ 2 * inputdata.size() + t ] = { inputdata[t][1], inputdata[t][2] };
    }

    std::sort( membervariable.begin(), membervariable.end() );

    auto it = std::unique( membervariable.begin(), membervariable.end() );

    membervariable.resize( it - membervariable.begin() );


}

生成文件:

CC = clang++ -O0 -g -std=c++11 -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -pedantic -Wall -Wextra -Wno-unused-variable -Wno-sign-compare -Wno-missing-braces -Wmissing-field-initializers -Werror=implicit
# CC =     g++ -O0 -g -std=c++11 -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -pedantic -Wall -Wextra -Wno-unused-variable -Wno-sign-compare -Wno-missing-braces -Wmissing-field-initializers -Werror=implicit

test.o: test.cpp
        $(CC) test.cpp -c -o test.o 

调用 make 和 clang 版本的输出:

$ make
clang++ -O0 -g -std=c++11 -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -pedantic -Wall -Wextra -Wno-unused-variable -Wno-sign-compare -Wno-missing-braces -Wmissing-field-initializers -Werror=implicit test.cpp -c -o test.o
In file included from test.cpp:3:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/map:61:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_map.h:63:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/tuple:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/array:338:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/debug/array:86:52: error: too many arguments to function call, expected
      single argument '__other', have 2 arguments
      noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
                        ~~~~                       ^~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/debug/array:264:23: note: in instantiation of exception specification for
      'swap' requested here
    noexcept(noexcept(__one.swap(__two)))
                      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algobase.h:148:7: note: in instantiation of exception
      specification for 'swap<int, 2>' requested here
      swap(*__a, *__b);
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:84:11: note: in instantiation of function template
      specialization 'std::iter_swap<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
      std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
      std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
      __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *, std::__cxx1998::vector<std::__debug::array<int, 2>,
      std::allocator<std::__debug::array<int, 2> > > >, std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int,
      2> > > > >' requested here
            std::iter_swap(__result, __b);
                ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:1916:12: note: in instantiation of function template
      specialization 'std::__move_median_to_first<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
      std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
      std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, __gnu_cxx::__ops::_Iter_less_iter>'
      requested here
      std::__move_median_to_first(__first, __first + 1, __mid, __last - 1,
          ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:1948:11: note: in instantiation of function template
      specialization 'std::__unguarded_partition_pivot<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
      std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
      std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, __gnu_cxx::__ops::_Iter_less_iter>'
      requested here
            std::__unguarded_partition_pivot(__first, __last, __comp);
                ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:1963:9: note: in instantiation of function template
      specialization 'std::__introsort_loop<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
      std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
      std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, long,
      __gnu_cxx::__ops::_Iter_less_iter>' requested here
          std::__introsort_loop(__first, __last,
              ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:4698:12: note: in instantiation of function template
      specialization 'std::__sort<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
      std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
      std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >, __gnu_cxx::__ops::_Iter_less_iter>'
      requested here
      std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter());
          ^
test.cpp:40:10: note: in instantiation of function template specialization
      'std::sort<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<std::__debug::array<int, 2> *,
      std::__cxx1998::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > >,
      std::__debug::vector<std::__debug::array<int, 2>, std::allocator<std::__debug::array<int, 2> > > > >' requested here
    std::sort( membervariable.begin(), membervariable.end() );
        ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/debug/array:84:7: note: 'swap' declared here
      void
      ^
1 error generated.
makefile:6: recipe for target 'test.o' failed
make: *** [test.o] Error 1
$ clang++ --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$

最佳答案

问题中显示的错误消息涉及 GCC 标准库已严格实现的标准 ( LWG issue 2456 ) 中的错误。 GCC 的编译器没有诊断出生成代码的问题(否则错误会更早被发现),但您使用的 clang 版本却可以。 IIRC clang 后来添加了一个特殊的 hack 来允许这种特定用法的编译,因此“修复”是使用更新版本的 GCC 或更新版本的 Clang 或两者。

关于C++:STL数组的STL排序STL vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50094355/

相关文章:

c++ - XCode 上的 size_t

c++ - vector 迭代器不兼容错误

c++ - c++ 11中的线程安全对象状态操作

c++ - 错误: invalid conversion from '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' {aka 'char' } to 'const char*' [-fpermissive]

c++ - 如何将 lambda 用于 std::find_if

c++ - 无法将 "member pointer to derived class"转换为 "member pointer to base class"

c++ - 未找到 Fmod 常量 (FMOD_HARDWARE)

c++ - initializer_list 和默认构造函数重载决议

c++ - 指向函数和 std::function 的指针:第一个编译而不是第二个?

c++ - C++:在子类中扩展静态STL容器/映射成员?