偶数奇怪的排序 [英] Even-Odd sorting

查看:97
本文介绍了偶数奇怪的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


似乎一年只需要一个人熟练掌握C ++就会变得太生锈了。从专业角度来说,我已经远离了语言(一般来说还是来自

编程),但我仍然对此表示赞赏。


所以我决定玩具有一些想法,只是为了好玩,并评估如何生锈我将成为什么。 让我写一个简单的仿函数来分类集合中的

元素!我将从一个简单的

整数集合开始。我想。然后我为

排序选择了一个不寻常的标准:我想要订购元素,以便偶数成员

应该出现在奇数之前。 "以下就是我提出的

with。该程序没有产生预期的行为,我怀疑它与我的谓词有关。
。它可能不完全符合

这种功能的要求。


#include< iostream>

#include < set>

#include< functional>

#include< algorithm>

#include< iterator>


class EvenOddSorting {

public:

bool operator()(const int i,const int j)const {

返回i%2 == 0&& j%2!= 0;

}

};


int main(){

std :: set< int,EvenOddSortingintSet;

intSet.insert(10);

intSet.insert(7);

intSet。 insert(5);

intSet.insert(18);

intSet.insert(3);

std :: cout<< ; Even-Odd有序集:" ;;

std :: copy(intSet.begin(),

intSet.end(),

std :: ostream_iterator< int>(std :: cout,""));

std :: cout<< " \ n";

}


鉴于上述程序,预期输出应为:


偶数奇数有序集:10 18 3 5 7

请注意我提出的输出意味着隐藏的要求在偶数和奇数之间订购

。这将是理想的,但我会满意

(起初)的输出像''18 10 5 7 3'。有人愿意给我一块手吗?


谢谢!


-

NeyAndrédeMello Zunino

解决方案

NeyAndrédeMello Zunino写道:


[..]

所以我决定[..]让我写一个简单的仿函数来整理集合中的

元素!我将从一个简单的

整数集合开始。我想。然后我为

排序选择了一个不寻常的标准:我想要订购元素,以便偶数成员

应该出现在奇数之前。 "以下就是我提出的

with。该程序没有产生预期的行为,我怀疑它与我的谓词有关。
。它可能不完全符合

这种功能的要求。


#include< iostream>

#include < set>

#include< functional>

#include< algorithm>

#include< iterator>


class EvenOddSorting {

public:

bool operator()(const int i,const int j)const {

返回i%2 == 0&& j%2!= 0;



如果它们都是奇数怎么办?如果他们都是平均怎么办?

谓词是否满足严格弱序的要求?


如果第一个是偶数而第二个是奇数,则谓词返回''true''。

对于其余的它会返回''false'',对吗?这意味着当比较5和7时,它将确定5不小于7.而当比较7和5时,它将确定7不小于7。 5.

在我看来,5与7是相同的。也就是说,5和7不能

共存于你的''EvenOddSorting''的集合中谓词。


用两个evens做同样的事。


}

};


int main(){

std :: set< int,EvenOddSortingintSet;

intSet.insert(10);

intSet.insert(7);



此后收集应为{10,7}。是吗?


intSet.insert(5);



此后收集_could_为10,5,7,如果谓词允许,它可以是10,7,5,

将5和7放在一起。它是b / b
,AFAICT。所以,这里的集合仍然是{10,7}。


等等。


intSet.insert (18);

intSet.insert(3);

std :: cout<< Even-Odd有序集:" ;;

std :: copy(intSet.begin(),

intSet.end(),

std :: ostream_iterator< int>(std :: cout,""));

std :: cout<< " \ n";

}


鉴于上述程序,预期输出应为:


偶数奇数有序集:10 18 3 5 7



好​​的,这就是*应该*读取(你相信)。 *它读取了什么*


>

注意我提出的输出意味着隐藏的订购要求
$ b偶数和奇数之间的$ b。这将是理想的,但我会满意

(起初)的输出像''18 10 5 7 3'。有人愿意给我一个手吗?



不确定您正在寻求什么样的帮助。您尚未指定所有

正确实现谓词的要求。我有足够的提示继续吗?


V

-

请在通过电子邮件回复时删除资本''A'

我没有回复最热门的回复,请不要问




5月23日下午3:41,NeyAndrédeMello Zunino< zun ... @ inf.ufsc.br>

写道:

[snip]


然后我为

排序选择了一个不寻常的标准:我想要要订购的元素,以便偶数成员

应出现在奇数成员之前。



[snip]


该程序没有产生预期的行为,我怀疑

它与我的谓词有关。



[snip]


class EvenOddSorting {

public:

bool operator()(const int i,const int j)const {

return i%2 == 0&& j%2!= 0;

}


};



[snip]


std :: set< int,EvenOddSortingintSet;



[snip]

你的主要问题是你的谓词以及你使用

std ::组。请注意,集合只能包含唯一项目。也就是说,没有两个元素a和b存在于一个集合中,使得a == b。


你的谓词测试a<湾如果< b OR b< a然后a!= b。如果(NOT

a< b)AND NOT(b< a)则a == b。该集使用您的谓词

来确定唯一性。因为您的谓词仅基于

奇偶校验而不是值进行比较,实际上您已将所有偶数定义为

等效,并且所有奇数都相等。因此你的

设置,只能包含唯一值,将包含一个偶数
数和一个奇数。


如果您希望整数的值成为唯一性的标准

,则谓词必须考虑到这一点。然后,你也可以在内排序b $ b。相反,偶数和奇数,如果你做了类似

这样的话:


class EvenOddValueSorting {

public:

bool operator()(const int i,const int j)const {

bool i_is_even =!(i%2);

bool j_is_even =! (j%2);

if(i_is_even == j_is_even)//如果奇偶校验相同...

返回i<焦耳; // ...仅按价值比较

其他

返回i_is_even; // ...否则总是<奇怪。

}

};


或者,您可以使用现在的谓词进行排序,但

不使用需要值唯一性的容器。例如,

使用带有当前谓词的向量:


std :: vector< intv;

v.push_back( 10);

v.push_back(7);

v.push_back(5);

v.push_back(18);

v.push_back(3);

std :: sort(v.begin(),v.end(),EvenOddSorting());

使用EvenOddSorting执行此操作会将所有偶数放在

之前。使用EvenOddValueSorting执行此操作也将按

数值排序。注意std :: sort不稳定;但如果需要,你可以使用

std :: stable_sort。请参阅:

http:// www。 sgi.com/tech/stl/table_of_contents.html

Jason


Victor Bazarov写道:


如果它们都是奇数怎么办?如果他们都是平均怎么办?

谓词是否满足严格弱序的要求?


如果第一个是偶数而第二个是奇数,则谓词返回''true''。

对于其余的它会返回''false'',对吗?这意味着当比较5和7时,它将确定5不小于7.而当比较7和5时,它将确定7不小于7。 5.

在我看来,5与7是相同的。也就是说,5和7不能

共存于你的''EvenOddSorting''的集合中谓词。


用两个evens做同样的事。



是的,我明白你的意思了,现在很有道理。


[...]


好​​的,那就是*应该*读取(你相信)。它读了什么*?



很抱歉没有发布结果。你是对的,收藏中只有10和7个



>通知我提出的输出如何隐含了在偶数和奇数之间进行排序的隐藏要求。这将是理想的,但我会满意(首先)输出像''18 10 5 7 3'。有人会关心
帮我一把吗?



不确定您正在寻求什么样的帮助。您尚未指定所有

正确实现谓词的要求。我有足够的提示继续吗?



是的,先生,您有。这里是修改后的仿函数类(可以进一步简化为
):


类EvenOddSorting {

public:

bool operator()(const int i,const int j)const {

if(i%2 == 0){

if (j%2!= 0){//我是偶数,j是奇数

返回true;

} else {// i和j都是偶数/>
返回i< j;

}

}否则{

如果(j%2 == 0){//我是奇数而j是偶数

返回false;

} else {//我和j都是奇数

返回i< j;

}

}

}

};


该程序现在输出我想要的结果:


偶数奇数有序集:10 18 3 5 7


您是否看到任何剩余的问题?


谢谢,


-

NeyAndrédeMello Zunino


Hello.

It seems a year is all it takes for one''s proficiency in C++ to become
too rusty. Professionally, I''ve been away from the language (and from
programming in general), but I still preserve an appreciation for it.

So I decided to toy with some idea, just for fun and for evaluating how
rusty I''d become. "Let me write a simple functor for sorting the
elements of a collection! I will start with a simple collection of
integers.", I thought. I then chose an unusual criterion for the
sorting: "I''d like the elements to be ordered so that the even members
should appear before the odd ones." The following is what I came up
with. The program is not producing the expected behavior and I suspect
it has to do with my predicate. It is probably not fully complying to
the requirements for such a function.

#include <iostream>
#include <set>
#include <functional>
#include <algorithm>
#include <iterator>

class EvenOddSorting {
public:
bool operator()(const int i, const int j) const {
return i % 2 == 0 && j % 2 != 0;
}
};

int main() {
std::set<int, EvenOddSortingintSet;
intSet.insert(10);
intSet.insert(7);
intSet.insert(5);
intSet.insert(18);
intSet.insert(3);
std::cout << "Even-Odd ordered set: ";
std::copy(intSet.begin(),
intSet.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}

Given the above program, the expected output should read:

Even-Odd ordered set: 10 18 3 5 7

Notice how my proposed output implies a hidden requirement of ordering
among even and odd numbers. That would be ideal, but I''d be satisfied
(at first) with an output like ''18 10 5 7 3''. Would anybody care to give
me a hand?

Thank you!

--
Ney André de Mello Zunino

解决方案

Ney André de Mello Zunino wrote:

[..]
So I decided [..] "Let me write a simple functor for sorting the
elements of a collection! I will start with a simple collection of
integers.", I thought. I then chose an unusual criterion for the
sorting: "I''d like the elements to be ordered so that the even members
should appear before the odd ones." The following is what I came up
with. The program is not producing the expected behavior and I suspect
it has to do with my predicate. It is probably not fully complying to
the requirements for such a function.

#include <iostream>
#include <set>
#include <functional>
#include <algorithm>
#include <iterator>

class EvenOddSorting {
public:
bool operator()(const int i, const int j) const {
return i % 2 == 0 && j % 2 != 0;

What if they are both odd? What if they are both even? Does the
predicate satisfy the requirement of strict weak ordering?

The predicate returns ''true'' if the first is even and the second is odd.
For the rest it would return ''false'', right? That means that when
comparing 5 and 7 it would determine that 5 is not less than 7. And
when comparing 7 and 5 it would determine that 7 is not less than 5.
That seems to me that 5 is the same as 7. That is, 5 and 7 cannot
coexist in the set that has your ''EvenOddSorting'' as the predicate.

Do the same with two evens.

}
};

int main() {
std::set<int, EvenOddSortingintSet;
intSet.insert(10);
intSet.insert(7);

After this the collection should be {10, 7}. Is it?

intSet.insert(5);

After this the collection _could_ be 10, 5, 7, or it could be 10, 7, 5,
if the predicate allowed placing both 5 and 7 next to each other. It
does not, AFAICT. So, here the collection will be {10, 7}, still.

And so on.

intSet.insert(18);
intSet.insert(3);
std::cout << "Even-Odd ordered set: ";
std::copy(intSet.begin(),
intSet.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}

Given the above program, the expected output should read:

Even-Odd ordered set: 10 18 3 5 7

OK, that''s what it *should* read (you believe). What *does* it read?

>
Notice how my proposed output implies a hidden requirement of ordering
among even and odd numbers. That would be ideal, but I''d be satisfied
(at first) with an output like ''18 10 5 7 3''. Would anybody care to give
me a hand?

Not sure what kind of help you are seeking. You''ve not specified all
the requirements for your predicate to implement it correctly. Have I
given you enough hints to go on?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



On May 23, 3:41 pm, Ney André de Mello Zunino <zun...@inf.ufsc.br>
wrote:
[snip]

I then chose an unusual criterion for the
sorting: "I''d like the elements to be ordered so that the even members
should appear before the odd ones."

[snip]

The program is not producing the expected behavior and I suspect
it has to do with my predicate.

[snip]

class EvenOddSorting {
public:
bool operator()(const int i, const int j) const {
return i % 2 == 0 && j % 2 != 0;
}

};

[snip]

std::set<int, EvenOddSortingintSet;

[snip]
Your main problem is your predicate along with your use of an
std::set. Note that a set can only hold unique items. That is, no two
elements a and b exist in a set such that a == b.

Your predicate tests if a < b. If a < b OR b < a then a != b. If (NOT
a < b) AND NOT (b < a) then a == b. The set uses your predicate to
determine uniqueness. Because your predicate only compares based on
parity and not values, in effect you have defined all even numbers to
be equivalent, and all odd numbers to be equivalent. Therefore your
set, which must only contain unique values, will contain one even
number, and one odd number.

If you want the value of the integers to be a criteria for uniqueness
your predicate must take that into account. You can also, then, have
sorting "within" the even and odd numbers, if you do something like
this instead:

class EvenOddValueSorting {
public:
bool operator()(const int i, const int j) const {
bool i_is_even = !(i % 2);
bool j_is_even = !(j % 2);
if (i_is_even == j_is_even) // if parity is same...
return i < j; // ... compare by value only
else
return i_is_even; // ... otherwise even always < odd.
}
};

Alternatively, you can use the predicate you have now for sorting, but
do not use a container that requires value uniqueness. For example,
use a vector with your current predicate:

std::vector<intv;
v.push_back(10);
v.push_back(7);
v.push_back(5);
v.push_back(18);
v.push_back(3);
std::sort(v.begin(), v.end(), EvenOddSorting());

Doing that with EvenOddSorting will place all the even numbers before
all the odd ones. Doing it with EvenOddValueSorting will sort by
numeric value also. Note that std::sort is not stable; but you can use
std::stable_sort if that is a requirement. See:

http://www.sgi.com/tech/stl/table_of_contents.html

Jason


Victor Bazarov wrote:

What if they are both odd? What if they are both even? Does the
predicate satisfy the requirement of strict weak ordering?

The predicate returns ''true'' if the first is even and the second is odd.
For the rest it would return ''false'', right? That means that when
comparing 5 and 7 it would determine that 5 is not less than 7. And
when comparing 7 and 5 it would determine that 7 is not less than 5.
That seems to me that 5 is the same as 7. That is, 5 and 7 cannot
coexist in the set that has your ''EvenOddSorting'' as the predicate.

Do the same with two evens.

Yes, I see what you mean and it makes perfect sense now.

[...]

OK, that''s what it *should* read (you believe). What *does* it read?

Sorry for not posting the results. You were right, only 10 and 7 were
being accepted in the collection.

>Notice how my proposed output implies a hidden requirement of ordering
among even and odd numbers. That would be ideal, but I''d be satisfied
(at first) with an output like ''18 10 5 7 3''. Would anybody care to
give me a hand?


Not sure what kind of help you are seeking. You''ve not specified all
the requirements for your predicate to implement it correctly. Have I
given you enough hints to go on?

Yes, sir, you have. Here''s the modified functor class (which can
probably be further simplified):

class EvenOddSorting {
public:
bool operator()(const int i, const int j) const {
if (i % 2 == 0) {
if (j % 2 != 0) { // i is even and j is odd
return true;
} else { // both i and j are even
return i < j;
}
} else {
if (j % 2 == 0) { // i is odd and j is even
return false;
} else { // both i and j are odd
return i < j;
}
}
}
};

The program now outputs my desired results:

Even-Odd ordered set: 10 18 3 5 7

Do you see any remaining issue?

Thank you,

--
Ney André de Mello Zunino


这篇关于偶数奇怪的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆