b*********n 发帖数: 1258 | 1 C++ Prototype
==============================
class Ball
{
public:
enum BallColor { RED, BLUE };
BallColor Color() const { return _color; }
private:
BallColor _color;
// Other data in class (unrelated to assignment)
};
我想在主函数里面定义一个Ball array,每一个元素都是一个Ball type
如果我不修改Ball class,可以初始化那个Ball array吗?
我的想法是修改Ball class的ctor
或者定义一个method来初始化Ball array里面的每一个元素
但是不知道,如果不修改这个class,可不可以实现同样的目的
谢谢 |
r****5 发帖数: 11 | 2 把原题贴上来吧
【在 b*********n 的大作中提到】 : C++ Prototype : ============================== : class Ball : { : public: : enum BallColor { RED, BLUE }; : BallColor Color() const { return _color; } : private: : BallColor _color; : // Other data in class (unrelated to assignment)
|
b*********n 发帖数: 1258 | 3 What's your opinion to my previous question? Thanks.
=============================
Given an array of balls, which can be one of two colors (RED or BLUE), w
rite a function that partitions the array in-place such that on exit fro
m the function all the balls of the same color are contiguous. It does n
ot matter whether the red or blue balls come first. The return value fro
m the function is the index of the first ball of the second color. If t
here is only one color of balls in the array then th
【在 r****5 的大作中提到】 : 把原题贴上来吧
|
r****5 发帖数: 11 | 4 in-place sorting,这个好像有经典解法
【在 b*********n 的大作中提到】 : What's your opinion to my previous question? Thanks. : ============================= : Given an array of balls, which can be one of two colors (RED or BLUE), w : rite a function that partitions the array in-place such that on exit fro : m the function all the balls of the same color are contiguous. It does n : ot matter whether the red or blue balls come first. The return value fro : m the function is the index of the first ball of the second color. If t : here is only one color of balls in the array then th
|
b*********n 发帖数: 1258 | 5 这个inplace sorting我知道
我想问的是那个class Ball如何初始化
谢谢
【在 r****5 的大作中提到】 : in-place sorting,这个好像有经典解法
|
r****5 发帖数: 11 | 6 不用初始化吧,Partition函数的输入,应该可以直接用
【在 b*********n 的大作中提到】 : 这个inplace sorting我知道 : 我想问的是那个class Ball如何初始化 : 谢谢
|
S*********g 发帖数: 5298 | 7 The question starts with "given an array"
It is given, you don't need to worry about that.
What you need to do is to partite the array
【在 b*********n 的大作中提到】 : 这个inplace sorting我知道 : 我想问的是那个class Ball如何初始化 : 谢谢
|
i*****f 发帖数: 578 | 8 heap sorting could work?
【在 r****5 的大作中提到】 : in-place sorting,这个好像有经典解法
|
|
p***o 发帖数: 1252 | 9 哪有那么复杂,参见std::partition。
【在 i*****f 的大作中提到】 : heap sorting could work?
|
e***s 发帖数: 2 | 10 参考快速排序的partition部分就是了。。。 |