如何使用 c++ 中的 NEW 运算符为完整列表分配更多内存?

How can I allocate more memory to a full list using the NEW operator in c++?

提问人:evanparial 提问时间:9/5/2022 更新时间:9/5/2022 访问量:53

问:

我正在尝试制作带有歌曲的播放列表。我在 c++ 中使用 List 数据结构。我想改进函数 insert(),如果我的列表充满了 100 首歌曲,我可以使用运算符 NEW 为列表分配更多内存。我还必须使用 delete[] 解除分配现有内存。

我不确定如何创建一个新列表并将它们连接在一起。

这是我的 List 类:

#include <string>
#include <iostream>
#include <stdexcept>
using namespace std;


template<class ItemType>
class List
{
private:
    static const int CHUNK_SIZE=100;
    ItemType *list;
    int numItems;
    int maxItems;
public:
    // default constructor and destructor
    List()
    {
      numItems = 0;
      maxItems = CHUNK_SIZE;
      list = new ItemType[CHUNK_SIZE];
    }

    ~List()
    {
      delete[] list;
    }
    
    // list member functions
    bool isEmpty()  const 
    {
      return numItems==0;
    }


    int getLength() const 
    { 
      return numItems; 
    }
    bool insert(int pos, const ItemType& item);

    bool remove(int pos);
    
    // clear the list
    // clear can simply set numItems to zero.  The array list may still contain
    // items already inserted into the list, but since numItems is zero, there
    // isn't any way to get at them using getEntry() or setEntry()
    void clear()
    {
      numItems = 0;
    }
    
    // return entry at postion pos
    // throw invalid_argument if pos<1 or pos>getLength()
    ItemType getEntry(int pos) const;
    
    // set entry at postion pos to item
    // throw invalid_argument if pos<1 or pos>getLength()
    // changes whatever is inside the position to that item. swap. 
    void setEntry(int pos, const ItemType& item);
    
};

这是我的 insert() 函数:

template<class ItemType>
bool List<ItemType>::insert(int pos, const ItemType& item)
{
    bool canAdd;
    
    canAdd = ((pos > 0) && (pos <= numItems + 1) && (numItems < maxItems));
    if (canAdd)
    {
        // first, we have to move everything after our insertion point over one
        // position to make room for our new item.  start at the back of the list.
        // don't forget arrays start at postion zero and our list ADT starts at
        // position 1.
        for(int i=numItems; i>=pos; i--)
            list[i] = list[i-1];
        
        // now put our item at position pos-1
        list[pos-1] = item;
        
        numItems++;
    }

    return canAdd;
}

这是我的 main() 函数:

int main()
{
    List<string> songs;
    char goAgain = 'y';
    int trackNumber;
    string trackName;
    
    // Insert some songs into our list

    songs.insert(1, "Bohemian Rhaspody");

    songs.insert(2, "The Highway Song");

    songs.insert(3, "In the Loop");

    songs.insert(4, "Lovesong");

    songs.insert(5, "Blow The Whistle");

    songs.remove(1);
      
    cout << "Welcome!  There are " << songs.getLength() << " tracks.\n";
    while (goAgain!='n')
    {
        trackNumber = getTrack();
        try
        {
            trackName = songs.getEntry(trackNumber);
        }
        catch (invalid_argument arg)
        {
            cout << arg.what() << endl;
            trackName = "No Track";
        }
        cout << "Your track name is " << trackName << endl;
        cout << "Go again? (y/n) ";
        cin >> goAgain;
    }
    
    cout << "you're ready to rock!\n";
    return 0;
}
C++ 数组 异常 播放列表

评论

0赞 Sam Varshavchik 9/5/2022
显示的代码已使用 和 ,并使用赋值运算符移动内容。这就是完成给定任务所需的全部内容,那么您究竟不清楚什么?newdelete
0赞 evanparial 9/5/2022
那么,如果列表达到 100 个限制,我是否必须声明一个新列表?如何将所有 inserts() 重定向到新列表中?它们会结合在一起吗?
1赞 Sam Varshavchik 9/5/2022
如果你有一个足够大的盒子,可以容纳 100 个苹果,它现在已经满了,但你建造了一个新盒子,现在足够大,可以容纳 200 个苹果:你如何实现一个盒子的最终结果,包含相同的 100 个应用,但现在还有空间容纳另外 100 个苹果?难道你把 100 个适用于新盒子,把它放在旧盒子原来的位置,然后把旧盒子切开扔进垃圾桶,这难道不是很明显吗?
0赞 doron 9/5/2022
一个简单的方法是首先在其构造函数中调整列表的大小。当您需要更多空间时,您可以创建一个更大的列表,然后将所有数据移动到其中。下一阶段是在内部进行所有这些簿记。
3赞 john 9/5/2022
@evanparial 你三个问题的答案都是否定的。你没有以正确的方式思考这个问题。您必须分配一个新数组,将项目从旧数组复制到新数组,然后删除旧数组,然后将列表设置为使用新数组。所有这些都必须在函数(或它调用的函数)中完成。没有新列表,您正在修改现有列表,以便它可以容纳更多项目。insert

答: 暂无答案