C++ 为什么STL的排序功能不起作用?
一组数字将作为输入传递。此外,按升序重新定义的数字0-9关系将作为输入传递。根据重新定义的关系,数字集必须按升序列出。 样本I/O 输入:C++ 为什么STL的排序功能不起作用?,c++,c++11,stl,C++,C++11,Stl,一组数字将作为输入传递。此外,按升序重新定义的数字0-9关系将作为输入传递。根据重新定义的关系,数字集必须按升序列出。 样本I/O 输入: 20 50 11 121 9231476058 输出: 50 11 20 121 我写的程序有一个我不知道的错误。所以,请帮我调试一下 程序规范: 1.)我创建了一个邻接列表,用于根据数字的位数对数字进行分组(50 11 20在索引2处分组,121在索引3处分组) 2.)为了对它们进行排序,我使用了标准模板库排序功能。我已经传递了以下参数 list&l
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));
}
}
}
list*lst=新列表[10]//邻接表
无效排序(列表*lst,int*arr1)
{
静态int*arr=arr1;
结构fnct
{
int-digi;
fnct(国际数字)
{
这个->digi=digi;
}
布尔运算符()(int-val1,int-val2)
{
while(digi>0)//是传递参数的位数
{
//用于排序的逻辑。这里我使用了本地arry“*arr”,我
//已声明为静态
}
}
};
for(int con=9;con>=0;--con)//计算邻接列表的行数
{
if(*(lst+con)).size()>0)//用于查找有效列表
{
排序((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&'
在/usr/include/c++/4.9/algorithm:62:0、prog.cpp:5中包含的文件中:
/usr/include/c++/4.9/bits/stl_algo.h:在“void”的实例化中
std::uu排序(RandomAccessIterator,RandomAccessIterator,Compare)[with
_RandomAccessIterator=std::_列表_迭代器;_比较=
__gnu_cxx::_运营::_Iter_comp_Iter]':
/usr/include/c++/4.9/bits/stl_algo.h:4716:78:来自“void”的必填项
排序(_RAIter,_RAIter,_Compare)[with _RAIter=
std::_List_iterator;_Compare=sortg(std::List*,int*)::fnct]'
程序cpp:124:63:此处需要
/usr/include/c++/4.9/bits/stl_algo.h:1968:22:错误:与“运算符-
(操作数类型为“std::_List_iterator”和
'std::_List_iterator')
标准::uuu lg(uuu last-uuu first)
*2、/usr/include/c++/4.9/bits/stl_algo.h:1968:22:注:
“std::_List_iterator”不是从“const”派生的
std::move_迭代器'
标准::uuu lg(uuu last-uuu first)*2,
“从此处删除行”
^
在/usr/include/c++/4.9/vector:65:0中包含的文件中,
从/usr/include/c++/4.9/bits/random.h:34,
从/usr/include/c++/4.9/random:49,
从/usr/include/c++/4.9/bits/stl_algo.h:66,
从/usr/include/c++/4.9/算法:62,
来自项目cpp:5:
/usr/include/c++/4.9/bits/stl_bvector.h:208:3:注:std::ptrdiff_t std::operator-(const std::_Bit_iterator_base&,const std:_Bit_iterator_base&)
运算符-(常量位迭代器基和常量位迭代器基和常量位迭代器y)
^
/usr/include/c++/4.9/bits/stl_bvector.h:208:3:
注意:参数1从“std::_List_iterator”到
'const std::_Bit_iterator_base&'
由于这个错误很长,我已经删除了它中间的一些行
sort((lst+con)->begin(),(lst+con)->end(),fnct(con));
说
std::sort
对列表(双向迭代器类型)不起作用。使用<代码>列表.SoTo()/Cord>。你可以将C++模板归咎于可怕的错误消息。好的,你已经清楚地知道了std::vector
?@JohnnyCage:谢谢,您的解决方案是有效的。旁白:您通常应该使用lst[con]
而不是*(lst+con)
。另外,使用x->y
而不是(*x).y
。但是这里如何使用RandomAccessIterator以及如何实现它们呢?
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>]'
(*(lst+con).)sort(fnct(con)); // Use it as appropriate