c++ - 如何插入数组? C++

标签 c++ arrays insert

我有一个模板类,它使用数组和当前大小存储对象集合。我想出了插入和删除。我在使用 isEmpty 函数时遇到困难。它返回 false(好像它不是空的),即使我添加 3 和 5 然后删除 3 和 5。它应该说 true

#include <cstdio>
#include <iostream>
#include <cstdlib>

/************************************************************/
// Local includes

/************************************************************/
// Using declarations

using namespace std;

/************************************************************/
template <class T>
class Collection 
{
public:  

    Collection (int mArraySize)
    {
    m_size = mArraySize;
    array = new T[mArraySize];
    reset();
}
/************************************************************/ 

int 
size()
{
    return nextEmpty;
}

/************************************************************/ 

void
reset()
{
    nextEmpty = 0;
    nextToRead = 0;
}

/************************************************************/ 

void
insert(const T& a)
{
    if (nextEmpty < m_size)
    {
        array[nextEmpty++] = a;
    }
}

/************************************************************/ 

bool
isEmpty() 
{
   if(m_size == 0)
   {
       return true;
   }
   else
   {
       return false;
   }
}

/************************************************************/ 

void
makeEmpty() 
{  
    m_size = 0;
    array = NULL;
}

/************************************************************/

void
remove(const T& r) 
{
    int i = 0;
    for (i = 0; i < m_size; i++)
    {
        if (array[i] == r)
        {
            break;
        }
    }
    while (i++ < m_size)
    {
        array[i - 1] = array[i];
    }
    m_size--;
}

/************************************************************/


void
contains(T array, T target) 
{

    for(int i = 0; i < m_size; i++)
    {

    }
}

/************************************************************/

T& 
get()
{
    return array[nextToRead++];
}

/************************************************************/

private:

T* array;
int nextEmpty;
int nextToRead;
int m_size;
};

/************************************************************/

#include <iostream>
#include <iterator>

/****************************************************************************/
// Local includes
#include "header.h"

/****************************************************************************/
// Using declarations

using namespace std;

/****************************************************************************/
// Prototypes, typedefs, etc.

bool
isEmpty();

void
makeEmpty();

void
insert();

void
remove();

void
contains();

void
testerFcn();

/****************************************************************************/

int main(int argc, char* pArgs[]) 
{
    testerFcn();

    return EXIT_SUCCESS;
}

/************************************************************/

void
testerFcn()
{
    Collection <int> testArray(15);

    // insert test
    cout << "Enter numbers to add to array (enter negative number to finish): " << endl;

    for(;;)
    {
        int n;
        cin >> n;

        if (n < 0) {
            break;
        }
        testArray.insert(n);
    }

    // remove test
    cout << "Enter value to remove, (enter negative number to finish): " << endl;

    for(;;)
    {
        int n;
        cin >> n;

        if (n < 0) {
            break;
        }
        testArray.remove(n);
    }

    cout << "Is the array empty: 0 = false, 1 = true ==> " << testArray.isEmpty() << endl;

    // print what we have
    cout << "New Array: " << endl;
    for (int i = 0; i < testArray.size(); i++)
    {
        cout << i << ":" << testArray.get() << endl;
    }
}

最佳答案

由于您是 C++ 新手,因此我建议您在这里采取一些小步骤。您的大部分语法都不正确,我觉得您最好在担心复制项目和调整数组大小的问题之前拥有一些实际编译的最小类。但既然你已经走到这一步,这里有一些一般性建议(我显然不会为你编写正确的代码,因为这是家庭作业):

  • 您正在使用 T可互换地作为您包含的类型和数组的类型。事实上调用new T[x]给你一个指向t的指针,即。一个T* .您需要了解两者之间的区别才能学习 C 或 C++。

  • 你的 ensureCapacity函数有两个参数。您不应该使用成员变量 m_array作为一个论点——它实际上根本不会做你在这里所期望的(为了加分,想想它做什么)。还要考虑对 minimumCapacity 是否有意义成为T

  • 您在 ensureCapacity 中分配了两个新数组永远不会delete任何事物。请记住,这是 C++,您必须以某种方式释放内存。此外,您实际上应该只需要一个新的、更大的数组——我认为关于 T 之间的区别可能会有一些混淆。和一个 T*再次关于如何交换它们。

  • 你确定比较吗m_array[m_size] < minimumCapacity ?你在这里比较什么?为什么?

希望对您有所帮助。同样,我建议您在过分担心调整数组大小的逻辑之前尝试掌握基本的语言概念(公认的比许多其他概念更棘手)——也许从一个非常简单的类开始,该类具有固定大小的数组和从那里继续前进。

关于c++ - 如何插入数组? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9234867/

相关文章:

c++ - SendInput() 不等于在 C++ 键盘上手动按键?

c++ - 使用可变参数模板进行统一初始化

python - numpy stride_tricks.as_strided 与滚动窗口的列表理解

php - MySQL - 忽略插入错误 : duplicate entry

database - KDB 中插入和更新插入的行为

c++ - 渲染 SketchUp 模型 API

java - 使用模数获取给定输入元素数组中最大子数组的总和

arrays - 按子数组总和大小拆分子数组 Ruby

c++ - 在 STL 映射中添加类对象作为值

c++ - 如何在头文件中声明类模板(由于循环依赖)