简体   繁体   English

追加并清除列表C ++

[英]Append and clear for a list c++

I'm trying to realize two methds append() and clear() . 我正在尝试实现两种方法append()clear()

In appened() , I need to newPoint to the end of a list. appened() ,我需要newPoint到列表的末尾。 If the list is empty, then adds newPoint as the first(and only) point in the list. 如果列表为空,则将newPoint添加为列表中的第一个(也是唯一的)点。

In clear() ,I need to remove all the points in a list. clear() ,我需要删除列表中的所有点。

Can you give me some advice to realize appened and clear . 你能给我一些建议以实现appened and clear Here is a code: 这是一个代码:

//
#pragma once
const int maxListSize = 10;

class Point
{
private:
    float x;
    float y;
public:
    Point(float x0 = 0, float y0 = 0)
    {
        x = x0;
        y = y0;
    }
};


class PointList
{
private:
    //Data members
    int size;
    int cursor;
    Point points[maxListSize];
public:
    PointList();
    //List Manipalution operations 
    void append(Point newPoint);
    void clear();

    ~PointList();
};

*I don't need you to write everything for me, just give me some advice. *我不需要您为我编写所有内容,只需给我一些建议即可。 I would like to realize it by myself. 我想一个人实现。 Thank you for helping. 感谢您的帮助。

Since you store your list elements by value ( Point points[maxListSize] ), it is quite easy to do: 由于您按值存储列表元素( Point points[maxListSize] ),因此很容易做到:

PointList() :size(0) {}
void append(Point newPoint) { points[size++] = newPoint; }
void clear() { size = 0; }

This assumes that your Point object doesn't manage any resource, which is true for this particular example. 假定您的Point对象不管理任何资源,这对于此特定示例而言是正确的。 Otherwise, inserted Point objects should be destroyed in clear. 否则,应彻底销毁插入的Point对象。

To get the semantics that you're probably expecting for appending new items, and clearing out existing items, I suggest you look at the placement new operator, and manually calling the destructor of an item in the list. 为了获得可能期望添加新项目并清除现有项目的语义,建议您查看place new操作符,并手动调用列表中项目的析构函数。

Currently your class will construct all of the items in the list when you create the list. 当前,当您创建列表时,您的班级将构造列表中的所有项目。 This can be quite time consuming for complex structures. 对于复杂的结构,这可能会非常耗时。 Then, instead of the constructor for your elements being called, you'll instead be calling the copy-assignment operator, as the items are already constructed. 然后,您将不再调用元素的构造函数,而是调用copy-assignment运算符,因为这些项已经被构造。

If you store your array as 如果将数组存储为

char * points[sizeof(Point)*maxListSize];

Any only initialize the items when they're actually added, you avoid the construction cost when you create the list. 任何人仅在实际添加项目时才对其进行初始化,因此可以避免在创建列表时产生的建设成本。

Your append function takes it's argument by value. 您的append函数按值接受参数。 Instead, I recommend you have two append functions. 相反,我建议您有两个附加函数。 One that takes const&, and the other that takes an rvalue-reference. 一个使用const&,另一个使用rvalue-reference。 Then, inside the append function, call the placement new operator on the address of the next location in your array. 然后,在append函数内部,在数组中下一个位置的地址上调用placement new运算符。

To clear the array, simple call the destructor for each element in the array one at a time. 要清除数组,只需一次为数组中的每个元素调用析构函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM