为什么STL的排序功能不起作用?

why sort function of STL is not working?

一组数字将作为输入传递。此外,重新定义的数字 0-9 的升序关系将作为输入传递。基于重新定义的关系,数字集必须按升序排列。 示例 I/O

输入:

20 50 11 121

9231476058

输出:

50 11 20 121

我写的程序有一个我不知道的错误。所以,请帮我调试它。

程序规范:

1.) 我创建了一个邻接表,用于根据数字的位数对数字进行分组(50 11 20 在索引 2 处分组,121 在索引 3 处分组)

2.)为了对它们进行排序,我使用了标准模板库排序函数。我传递了以下参数

list<int> *lst=new list<int>[10];       //adjacency list

void sortg(list<int> *lst,int *arr1)
{
    static int *arr=arr1;
    struct fnct
    {
        int digi;

        fnct(int digi)
        {
            this->digi=digi;
        }
        bool operator()(int val1,int val2)
        {
            while(digi>0)  // is the number of digits of the passed arguments
            {
                //logic for sorting. here i have used local arry "*arr" which i
                //have declared static
            }
        }
    };

    for(int con=9;con>=0;--con)      //count for the rows of adjacency list
    {
        if( (*(lst+con)).size()>0 )     // for finding out a valid list
        {
            sort((lst+con)->begin(),(lst+con)->end(),fnct(con));

        }
    }
}

我得到的错误是:

In file included from /usr/include/c++/4.9/algorithm:62:0,from prog.cpp:5:
/usr/include/c++/4.9/bits/stl_algo.h: In instantiation of 'void
std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with
_RandomAccessIterator = std::_List_iterator<int>; _Compare =                  
__gnu_cxx::__ops::_Iter_comp_iter<sortg(std::list<int>*, int*)::fnct>]':
/usr/include/c++/4.9/bits/stl_algo.h:4716:78:   required from 'void              

std::sort(_RAIter, _RAIter, _Compare) [with _RAIter =   

std::_List_iterator<int>;    _Compare = sortg(std::list<int>*, int*)::fnct]'
prog.cpp:124:63:   required from here
/usr/include/c++/4.9/bits/stl_algo.h:1968:22: error: no match for       'operator-  

'    (operand types are 'std::_List_iterator<int>' and   

'std::_List_iterator<int>')
 std::__lg(__last - __first)     

*2,/usr/include/c++/4.9/bits/stl_algo.h:1968:22: note:      

'std::_List_iterator<int>' is not derived from 'const    

std::move_iterator<_Iterator>'
 std::__lg(__last - __first) * 2,


                 "Lines are removed from here"
                  ^
In file included from /usr/include/c++/4.9/vector:65:0,
             from /usr/include/c++/4.9/bits/random.h:34,
             from /usr/include/c++/4.9/random:49,
             from /usr/include/c++/4.9/bits/stl_algo.h:66,
             from /usr/include/c++/4.9/algorithm:62,
             from prog.cpp:5:
/usr/include/c++/4.9/bits/stl_bvector.h:208:3: note: std::ptrdiff_t   std::operator-(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)
operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
^
/usr/include/c++/4.9/bits/stl_bvector.h:208:3: 

note:no known conversion for argument 1 from 'std::_List_iterator<int>' to   
'const std::_Bit_iterator_base&'

由于这个错误很长,我删除了中间的一些行

行错误
 sort((lst+con)->begin(),(lst+con)->end(),fnct(con));

 In instantiation of 'void
std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with
_RandomAccessIterator = std::_List_iterator<int>; _Compare =                  
__gnu_cxx::__ops::_Iter_comp_iter<sortg(std::list<int>*, int*)::fnct>]'

现在,List 被构建为双向链表(因此双向迭代),其主要功能是支持恒定时间插入和擦除操作。但是随机访问?没有。

所以,错误消息说排序需要 RandomAccessIterator 而 List Iterator 不是(它是双向迭代器,预期是作为双向链表实现的)。

所以,使用成员函数sort来做这个。

(*(lst+con).)sort(fnct(con));  // Use it as appropriate