对字符串中的字符进行反向排序

C++ Reversely sort characters in a string

本文关键字:排序 字符 字符串      更新时间:2023-10-16

有很多代码按字典顺序排序字符串数组,但我找不到一个在一个字符串中按反向字典顺序排序字符的代码。到目前为止,我发现在<algorithm>中使用std::sort()可能是最接近的候选。

这是我尝试过的:

template <typename T>
class ReverseComparator{
     bool operator()(T l, T r){return !(l < r);}
};
//.....later
std::sort(str.begin(), str.end(), comparator);
这里有一个问题:我如何初始化比较器,使它可以比较字符串中的字符?我尝试了ReverseComparator<char> comparator,但编译器抛出了大量的错误消息。Gcc-4.5.1显示了以下错误:
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/algorithm:63:0,
                 from prog.cpp:1:
prog.cpp: In function 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2192:4:   instantiated from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5252:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49:   instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2125:4: error: within this context
prog.cpp: In function 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5100:7:   instantiated from 'void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2297:8:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49:   instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1914:2: error: within this context
prog.cpp: In function 'void std::__move_median_first(_Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2260:7:   instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2302:62:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49:   instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:108:7: error: within this context
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:110:4: error: within this context
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2260:7:   instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2302:62:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49:   instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:112:9: error: within this context
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:115:12: error: within this context
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:117:12: error: within this context
prog.cpp: In function '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Tp = char, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2261:78:   instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2302:62:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49:   instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2229:4: error: within this context
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2232:4: error: within this context
prog.cpp: In function 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2133:6:   instantiated from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2192:4:   instantiated from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5252:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49:   instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2083:7: error: within this context
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:62:0,
                 from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/algorithm:63,
                 from prog.cpp:1:
prog.cpp: In function 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Distance = int, _Tp = char, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:434:4:   instantiated from 'void std::make_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1912:7:   instantiated from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5100:7:   instantiated from 'void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2297:8:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49:   instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:303:4: error: within this context
prog.cpp: In function 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Distance = int, _Tp = char, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:316:7:   instantiated from 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Distance = int, _Tp = char, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:434:4:   instantiated from 'void std::make_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1912:7:   instantiated from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5100:7:   instantiated from 'void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2297:8:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Size = int, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49:   instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:180:7: error: within this context

虽然解决编译器错误只需要operator()是公共的,根据这个问题的大多数其他答案,您应该注意您的比较器不是一个有效的严格的比较器。对于被视为相等的元素,严格比较器必须返回false。对于比较器的参数类型,还应该使用const引用而不是值。

我想为您提供多个选项来解决您的问题(不是编译器错误,而是原始问题,以反向词法顺序对字符串排序)。对于两个相等的元素,它们都返回false

第一个选项是您的解决方案,但我固定了严格性:

template <typename T>
class ReverseComparator{
public:
     bool operator()(const T& l, const T& r){return (r < l);}
};
std::sort(str.begin(), str.end(), ReverseComparator());
第二个选项使用c++ 11的lambda函数,但本质上是相同的:
std::sort(str.begin(), str.end(), [](const T& l, const T& r){return (r < l);});

作为第三个选项,正如Mooning Duck首先提出的,您可以简单地在类型char上使用operator>的std函子:

std::sort(str.begin(), str.end(), std::greater<char>());
第四个选项(最简单的一个)是在反转的字符串上使用默认比较器,这是Peter Wood首先提出的(注意r !):
std::sort(str.rbegin(), str.rend());

通常,只有第一个错误是相关的,所以我们有:

In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/algorithm:63:0,
                 from prog.cpp:1:
prog.cpp: In function 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2192:4:   instantiated from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5252:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, _Compare = ReverseComparator<char>]'
prog.cpp:12:49:   instantiated from here
prog.cpp:6:11: error: 'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private

大部分都是简单的上下文,所以这里是关键部分:

'bool ReverseComparator<T>::operator()(T, T) [with T = char]' is private

这很容易修复:

#include <algorithm>
#include <string>
template <typename T>
class ReverseComparator{
    public: //forgot this
     bool operator()(T l, T r){return !(l < r);}
};
int main() {
    std::string str = "HELLO";
    ReverseComparator<char> comparator;
    std::sort(str.begin(), str.end(), comparator);
}

编译得很好,但是,排序是基于所谓的"严格弱排序"的,并且!(l < r)而不是严格弱排序。也就是说,comparitor(a,a)应该返回false,但您的返回true。幸运的是,以这种方式排序的想法已经在标准库中以std::greater的名称存在。(没有比较器的std::sort默认使用std::less)

使用std::sort()显然是正确的方法。默认比较器为std::less<value_type>,其中value_type为传递的迭代器类型的decltype(*it)。要更改默认值,只需传递一个不同的比较器,例如std::greater<char>

要使用ReverseComparator,因为它是定义的,你需要正确实例化它:

std::sort(s.begin(), s.end(), ReverseComparator<char>());

当然,要使其工作,您需要使operator()()可公开访问。您可能还想让它成为const成员函数:

template <typename T>
class ReverseComparator {
public:
    bool operator()(T lhs, T rhs) const { return rhs < lhs; }
};

请注意,运算符中的表达式也发生了变化:使用!(lhs < rhs)并不定义严格的弱顺序:comparator(x, x)会产生true,但需要产生false