首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么STL的排序功能不起作用?

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

Stack Overflow用户
提问于 2015-06-21 14:04:40
回答 1查看 2.2K关注 0票数 2

一组数字将作为输入传递。此外,以升序排列的数字0-9的重新定义的关系将作为输入传递。根据重新定义的关系,必须按升序列出一组数字。样品I/O

输入:

代码语言:javascript
复制
20 50 11 121

9231476058

输出:

代码语言:javascript
复制
50 11 20 121

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

程序规范:

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

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

代码语言:javascript
复制
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));

        }
    }
}

我得到的错误是:

代码语言:javascript
复制
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&'

由于这个错误很长,所以我从中间删除了一些行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-21 15:07:35

直线误差

代码语言:javascript
复制
 sort((lst+con)->begin(),(lst+con)->end(),fnct(con));

他说

代码语言:javascript
复制
 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被实现为双链接列表)。

因此,使用成员函数排序来完成此操作。

代码语言:javascript
复制
(*(lst+con).)sort(fnct(con));  // Use it as appropriate
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30965367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档