我对C 中共享指针列表进行排序的功能未完成类型
my function to sort a shared pointer list in c++ is not completing the sort
我有一个共享指针的stl std::list
,指向节点对象,需要用它们各自的 id
数字进行排序。
do
{
check = 0, i = 0;
auto it = newList.begin();
while (i < newList.size() - 1)
{
first = *it;
second = *++it;
if (comp_id(first, second))
{
temp = second;
second = first;
first = temp;
check = 1;
}
i++;
}
} while (check == 1);
在此代码中,comp_id()
返回true,并通过组织运行,但是在list
发生时,没有任何变化。我希望对为什么会发生这种情况以及如何修复它有一些观点。
P.S。我不允许使用list_name.sort()
方法:(
它不起作用的原因是因为您仅对列表中的 copies 进行修改(first
和second
(。您根本没有修改列表的实际内容。
要使代码工作,只需将您的 first
和 second
变量更改为列表迭代器,然后在要访问其值时将它们更改为listerference,例如:
auto size = newList.size();
if (size > 1)
{
--size;
do
{
check = 0, i = 0;
auto it = newList.begin();
while (i < size)
{
auto first = it;
auto second = ++it;
if (comp_id(*first, *second))
{
auto temp = *second;
*second = *first;
*first = temp;
check = 1;
}
++i;
}
}
while (check == 1);
}
为此,i
也可以用迭代器代替:
if (newList.size() > 1)
{
auto begin = newList.begin();
auto end = newList.end()-1;
do
{
check = 0;
auto it = begin;
while (it != end)
{
auto first = it;
auto second = ++it;
if (comp_id(*first, *second))
{
auto temp = *second;
*second = *first;
*first = temp;
check = 1;
}
}
}
while (check == 1);
}
相关文章:
- 更改运行时优先级队列的排序功能
- 不正确的比较和交换计数器输出用于快速排序功能
- 排序功能在C++中未按预期工作
- 基于不同字段的对象向量的排序功能
- 如何将标准::矢量插入具有自定义排序功能的 std::set 中
- 快速排序功能不一致
- 我的选择排序功能C 有什么问题
- 排序功能不起作用
- 面临CPP中排序功能的问题
- 数组排序功能
- 当我们在C 中已经可以使用STL排序功能时,为什么我们需要学习不同的排序算法
- 排序功能无法正常工作
- 排序功能中的分段错误
- C++:在模板快速排序功能中接收"couldn't deduce template parameter"错误
- 排序功能不起作用(字符串比较问题)
- 为气泡排序功能添加计数器
- C++列表排序功能
- 排序功能不排序
- C++ 使用指针进行选择排序功能
- C++调度程序的排序功能