社区
C++ 语言
帖子详情
std::deque
怎样用 stable_sort()
xili
2005-08-12 12:26:26
std::deque<my_class * ptr> 怎样用 stable_sort()?
直接用的话, 它是按照 ptr 的地址 排序的.
俺要用 my_class 的 operator<() 来排序.
...全文
117
6
打赏
收藏
std::deque<my_class * ptr> 怎样用 stable_sort()
std::deque 怎样用 stable_sort()? 直接用的话, 它是按照 ptr 的地址 排序的. 俺要用 my_class 的 operator<() 来排序.
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
foochow
2005-08-12
打赏
举报
回复
.....................
class myclass
{
private:
int i;
public:
myclass(int i=0):i(i){};
friend bool compares(const myclass*s1,const myclass*s2)
{
return s1->i<s2->i;
}
};
deque<myclass*>temp;
temp.push_back(new myclass(3));
temp.push_back(new myclass(2));
temp.push_back(new myclass(6));
temp.push_back(new myclass(2));
temp.push_back(new myclass(15));
temp.push_back(new myclass(10));
stable_sort(temp.begin(),temp.end(),compares);
.................
lw1a2
2005-08-12
打赏
举报
回复
类的指针用类的规则来排序?
foochow
2005-08-12
打赏
举报
回复
必须写 friend 吗?
...
可以像 boxban(冻酸梨) ( ) 那样,也可以
class myclass
{
private:
int i;
public:
myclass(int i=0):i(i){};
int couts()const
{
return i;
}
};
bool compares(const myclass*s1,const myclass*s2)
{
return s1->couts()<s2->couts();
}
boxban
2005-08-12
打赏
举报
回复
bool comp_my_class_ptr(const my_class* a, const my_class* b)
{
return (*a) < (*b);
//需在my_class 类中重载 bool operator < (const my_class& r)
}
std::stable_sort(q.begin(), q.end(), comp_my_class_ptr);
darkstar21cn
2005-08-12
打赏
举报
回复
重载operator<()也可以的
stable_sort(temp.begin(),temp.end());
xili
2005-08-12
打赏
举报
回复
必须写 friend 吗?
STL笔记(3)- Effective STL
必须将i转换成
deque
<int>::const_itertaor, 否则distance模板无法编译. */ distance 两个迭代器之间的距离 , advance 将迭代器移动指定距离 . 28: 正确理解有 reverse_iterator 的 base() ...
STL学习小结
<itertator>中,我们完全可以不管迭代器类是怎么实现的,大多数的时候,把它理解为指针是没有问题的(指针是迭代器的一个特例,它也属于迭代器),但是,决不能完全这么做。 迭代器功能 输入迭代器 Input ...
【侯捷面向对象高级开发】第三部分:C++新特性
class
Fraction { public: Fraction(int num, int den=1) : m_numerator(num),m_denominator(den) { } operator double() const //通常都会加const 不改变数据 { return (double)(m_numerator / m_denominator);...
std
::string and stl 算法
cout << boolalpha << startwith << endl; bool endwith = s.compare(s.size() - tail.size(), tail.size(), tail) == 0; cout << boolalpha << endwith << endl; 当然了,...
c++ 常用总结(二)
//
std
::shared_
ptr
b1 =
std
::make_shared<>(new int[10]); // 错误
std
::shared_
ptr
b1(new int[10],
std
::default_delete()); // 创建指向数组的智能指针
std
::cout << "b1: " << *b1 << "\n"; //
std
::cout << ...
C++ 语言
64,648
社区成员
250,503
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章