C ++“排序-> CMP函数”错误

时间:2018-08-09 11:48:00

标签: c++ arrays sorting raspberry-pi

我对C ++“排序-> CMP函数”有疑问。看来我的编译器不想支持该标准(可以在Internet上找到它)。

我将Raspberry Pi(Raspbian)与CodeLite和C ++一起使用。我想使用自定义比较功能对字符串数组(ASC,DESC,Random)进行排序。

但是我总是收到错误消息,对我什么也没说(字符串/字符可能有什么问题)。

这是什么?

这是我的测试代码:

#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
//#include "Test.h"

bool cmp1(const auto& a, const auto& b) { return a<b; }//DOES NOT WORK, ERROR: /home/pi/test_cpp/main.cpp:22:40: error: no matching function for call to 'sort(std::__cxx11::string [bufferSize], std::__cxx11::string*, <unresolved overloaded function type>)'
bool cmp2(const char * a, const char * b) { return a<b; }//DOES NOT WORK, ERROR: /usr/include/c++/6/bits/predefined_ops.h:125:18: error: cannot convert 'std::__cxx11::basic_string<char>' to 'const char*' in argument passing
bool cmp3(const char * a, const char * b) { return std::strcmp(a, b) < 0; }//DOES NOT WORK, ERROR: like cmp2
bool cmp4(char a, char b) { return a<b; }//DOES NOT WORK, ERROR: /usr/include/c++/6/bits/predefined_ops.h:125:18: error: cannot convert 'std::__cxx11::basic_string<char>' to 'char' in argument passing

int main(int argc, char **argv){
    printf("Testarea...\n");
    unsigned int bufferSize = 4;
    std::string buffer[bufferSize];
    buffer[0] = "def";
    buffer[1] = "abc";
    buffer[2] = "XYZ";
    buffer[3] = "GHI";
    unsigned int bufferLength = 4;
    printf("Array: "); for(unsigned int i=0; i<bufferLength; i++){ printf("%s ", buffer[i].c_str()); } printf("\n");
    printf("sort...\n");
    sort(buffer, buffer+bufferLength, cmp1); 
    //sort(buffer, buffer+bufferLength, [](const auto& a, const auto& b){ return a<b; });//DOES WORK, but i cant use strcmp!!!
    //sort(buffer, buffer+bufferLength, [](const auto& a, const auto& b){ return strcmp(a, b)==0; });//DOES NOT WORK, ERROR: cannot convert 'const std::__cxx11::basic_string<char>' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'
    printf("Array: "); for(unsigned int i=0; i<bufferLength; i++){ printf("%s ", buffer[i].c_str()); } printf("\n");
    return 0;
}

上面的cmp1-cmp4中给出了错误消息。这是完整的错误消息:

/bin/sh -c '/usr/bin/make -j4 -e -f  Makefile'
----------Building project:[ test_cpp - Debug ]----------
make[1]: Entering directory '/home/pi/test_cpp'
/usr/bin/g++  -c  "/home/pi/test_cpp/main.cpp" -g -O0 -Wall  -o ./Debug/main.cpp.o -I. -I.
In file included from /usr/include/c++/6/bits/stl_algobase.h:71:0,
                 from /usr/include/c++/6/bits/char_traits.h:39,
                 from /usr/include/c++/6/string:40,
                 from /home/pi/test_cpp/main.cpp:2:
/usr/include/c++/6/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = std::__cxx11::basic_string<char>*; _Iterator2 = std::__cxx11::basic_string<char>*; _Compare = bool (*)(char, char)]':
/usr/include/c++/6/bits/stl_algo.h:1844:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::__cxx11::basic_string<char>*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(char, char)>]'
/usr/include/c++/6/bits/stl_algo.h:1882:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::__cxx11::basic_string<char>*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(char, char)>]'
/usr/include/c++/6/bits/stl_algo.h:1968:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::__cxx11::basic_string<char>*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(char, char)>]'
/usr/include/c++/6/bits/stl_algo.h:4739:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = std::__cxx11::basic_string<char>*; _Compare = bool (*)(char, char)]'
/home/pi/test_cpp/main.cpp:23:40:   required from here
/usr/include/c++/6/bits/predefined_ops.h:125:18: error: cannot convert 'std::__cxx11::basic_string<char>' to 'char' in argument passing
         { return bool(_M_comp(*__it1, *__it2)); }
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/6/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = std::__cxx11::basic_string<char>*; _Value = std::__cxx11::basic_string<char>; _Compare = bool (*)(char, char)]':
/usr/include/c++/6/bits/stl_heap.h:129:48:   required from 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = std::__cxx11::basic_string<char>*; _Distance = int; _Tp = std::__cxx11::basic_string<char>; _Compare = __gnu_cxx::__ops::_Iter_comp_val<bool (*)(char, char)>]'
/usr/include/c++/6/bits/stl_heap.h:230:23:   required from 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = std::__cxx11::basic_string<char>*; _Distance = int; _Tp = std::__cxx11::basic_string<char>; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(char, char)>]'
/usr/include/c++/6/bits/stl_heap.h:335:22:   required from 'void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::__cxx11::basic_string<char>*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(char, char)>]'
/usr/include/c++/6/bits/stl_algo.h:1669:23:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::__cxx11::basic_string<char>*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(char, char)>]'
/usr/include/c++/6/bits/stl_algo.h:1930:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::__cxx11::basic_string<char>*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(char, char)>]'
/usr/include/c++/6/bits/stl_algo.h:1945:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = std::__cxx11::basic_string<char>*; _Size = int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(char, char)>]'
/usr/include/c++/6/bits/stl_algo.h:1965:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::__cxx11::basic_string<char>*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(char, char)>]'
/usr/include/c++/6/bits/stl_algo.h:4739:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = std::__cxx11::basic_string<char>*; _Compare = bool (*)(char, char)]'
/home/pi/test_cpp/main.cpp:23:40:   required from here
/usr/include/c++/6/bits/predefined_ops.h:147:11: error: cannot convert 'std::__cxx11::basic_string<char>' to 'char' in argument passing
  { return bool(_M_comp(*__it, __val)); }
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~
test_cpp.mk:95: recipe for target 'Debug/main.cpp.o' failed
make[1]: *** [Debug/main.cpp.o] Error 1
make[1]: Leaving directory '/home/pi/test_cpp'
Makefile:4: recipe for target 'All' failed
make: *** [All] Error 2
====17 errors, 1 warnings====

是由于将“字符串”用作数组而导致错误,还是我做错了什么? 如果原因是字符串,该如何对字符串数组进行排序?

1 个答案:

答案 0 :(得分:0)

非常感谢大家的快速支持。

我再次关注类型。我现在可以按如下方式实现比较功能(不区分大小写的比较):

功能:

bool cmp5(const std::string& a, const std::string& b){ return strcasecmp(a.c_str(),b.c_str()) < 0; }

作为lambda:

sort(buffer, buffer+bufferLength, [](const std::string& a, const std::string& b){ return strcasecmp(a.c_str(),b.c_str()) < 0; });

结果:

Testarea...
Array: def abc XYZ GHI 
sort...
Array: abc def GHI XYZ 

这不是很好(由于类型转换),但是可以按需工作。我没有找到任何匹配的std :: string函数进行比较(区分大小写/不区分大小写)。