由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问一个sort的问题
相关主题
I like this one.C里面有没有default argument这么一说,是不是只有在C++里有这个feature?
Q in an C++ examDefault function template arguments
C++ key/value 排序C#的Dictionary赋值操作必须先ContainsKey检查吗?
这个类的default constructor怎么写一个关于methodology的问题
想知道家在linux下都有什么c++ socket libraryMac or IBM laptop for java programming? Thank you!!!
问个剧简单的问题...请教一个vignette vcm问题 (转载)
来几个C++测试题MongoDB力压Cassandra
[合集] 关于C++ default copy constructorsome tips about hard disks (for your references)
相关话题的讨论汇总
话题: string话题: cmp话题: s1话题: s2话题: comp
进入Programming版参与讨论
1 (共1页)
j*****k
发帖数: 1198
1
想用stl sort来按string size排序。在relational function cmp里面
如果这样用就没问题
bool cmp(string &s1, string &s2)
{
return s1<=s2;
}
可是如果以下面的方式用,就会出现segment fault, 什么原因呢?
bool cmp(string &s1, string &s2)
{
return (s1.length()) <= (s2.length());
}
谢谢
s***e
发帖数: 793
2
i do not know where the problem is
but it is more robust to write the cmp this way
bool cmp(const string & s1, const string s2){
return (s1.length())<(s2.length()); // prefer < over <=
}


【在 j*****k 的大作中提到】
: 想用stl sort来按string size排序。在relational function cmp里面
: 如果这样用就没问题
: bool cmp(string &s1, string &s2)
: {
: return s1<=s2;
: }
: 可是如果以下面的方式用,就会出现segment fault, 什么原因呢?
: bool cmp(string &s1, string &s2)
: {
: return (s1.length()) <= (s2.length());

j*****k
发帖数: 1198
3
能请教一下,为什么不能用<=么

【在 s***e 的大作中提到】
: i do not know where the problem is
: but it is more robust to write the cmp this way
: bool cmp(const string & s1, const string s2){
: return (s1.length())<(s2.length()); // prefer < over <=
: }
:

p***o
发帖数: 1252
4
It is required by the algorithm. Read 25.3 of the C++ standard.
3 For all algorithms that take Compare, there is a version that uses
operator< instead. That is, comp(*i, *j) != false defaults to
*i < *j != false. For the algorithms to work correctly, comp has
to induce a strict weak ordering on the values.
4 The term strict refers to the requirement of an irreflexive relation
(!comp(x, x) for all x), and the term weak to requirements that are
not as strong as those for a total ordering

【在 j*****k 的大作中提到】
: 能请教一下,为什么不能用<=么
1 (共1页)
进入Programming版参与讨论
相关主题
some tips about hard disks (for your references)想知道家在linux下都有什么c++ socket library
有人熟悉apache solr吗问个剧简单的问题...
假如想实现 entity recognition, relation extraction这些功能的话,除了GATE, 还有 哪些其它的open source library。来几个C++测试题
app请教[合集] 关于C++ default copy constructor
I like this one.C里面有没有default argument这么一说,是不是只有在C++里有这个feature?
Q in an C++ examDefault function template arguments
C++ key/value 排序C#的Dictionary赋值操作必须先ContainsKey检查吗?
这个类的default constructor怎么写一个关于methodology的问题
相关话题的讨论汇总
话题: string话题: cmp话题: s1话题: s2话题: comp