Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 有序数组。调整大小不允许有序元素_C++_Arraylist_Resize_Push - Fatal编程技术网

C++ 有序数组。调整大小不允许有序元素

C++ 有序数组。调整大小不允许有序元素,c++,arraylist,resize,push,C++,Arraylist,Resize,Push,我正在制作一个有序数组。元素按顺序排列的位置。例1,2,3,4,5,6,77,89100201。我要求用户输入数组的大小。这很好,推送功能将按顺序放置元素。但是,当调整arrayList的大小时,元素不再按顺序分配 这是我的密码: //------------------------------------------------------- // Name: Array::Resize // Description: Resize the array to a

我正在制作一个有序数组。元素按顺序排列的位置。例1,2,3,4,5,6,77,89100201。我要求用户输入数组的大小。这很好,推送功能将按顺序放置元素。但是,当调整arrayList的大小时,元素不再按顺序分配

这是我的密码:

//-------------------------------------------------------
//  Name:           Array::Resize
//  Description:    Resize the array to a new size.
//  Arguments:      p_size.  The new size of the Array.
//-------------------------------------------------------
    void Resize(int p_size)//resizes the array to the size of p_size
    {
        cout << "Did i get this far ";
        if(p_size < 0)//checks if new size is less than 0
        {
            cout << "ERROR! Size of an array can not be less than 0!" << endl;
        }
        else//else its ok to continue
        {
            Datatype* newArray = new Datatype[p_size];//creates a pointer newArray that points at a new array
            if(newArray == 0)
            {
                return;
            }
            cout << "Did i get this far ";
            int min;

            if(p_size < size)//checks the if the new array is smaller than the old one
                min = p_size;
            else//else its going to be bigger
                min = size;
            cout << "Did i get this far ";
            int index;
            int temp = num_elements;//puts num_elements into a temporary variable called temp
            num_elements = 0;//num_elements is set to 0
            for(index = 0; index < min; index++)
            {
                newArray[index] = m_array[index];//places everything from the old array into the new array that will fit.
                if(num_elements < temp)//if the num_elements is less than temp(the original num_elements)
                {
                    num_elements++;//increment num_elements. This will keep incrementing to create the new num_elements based the number of elements cut off in the resize
                }
            }
            size = p_size;//sets the old size to be equal to the new size
            cout << "Did i get this far ";
            if(m_array != 0)
            cout << "\nI am just about to delete ";
            //delete[] m_array;//deletes the old array
            m_array = newArray;//makes m_array point at the new array
            newArray = 0;//makes newArray a null pointer
        }
    }

//---------------------------------------------------------------------------------------
// Name:             Push
// Description:      
//---------------------------------------------------------------------------------------
void push(Datatype p_item)
{
    if(num_elements == size)//checks if the array is full and needs to be resized
    {
        Resize(size + g_size);//calls the resize function
    }

    int pos = num_elements;
    for(int x=0;x<num_elements;x++)
    {
        if(p_item < m_array[x])
        {
        pos=x;
        }
    }

    //loops through the array from high to low moving all values to the right
    //to make space for the passed in value until it gets to the right place
    for(int index = num_elements; index >= pos; index--)
    {
        m_array[index] = m_array[index-1];//moves the values to the right
    }
        m_array[pos] = p_item;//the passed in value is positioned into its ordered position
        num_elements++;

    cout<< "Num Elements " << num_elements;
    cout<< "Size " <<size;
}

//--------------------------------------------------------------------------------------------
//  Name:           template <class Datatype>   
//  Description:        

    //--------------------------------
//-------------------------------------------------------
//名称:数组::调整大小
//描述:将阵列大小调整为新大小。
//参数:p_大小。数组的新大小。
//-------------------------------------------------------
void Resize(int p_size)//将数组大小调整为p_size的大小
{

cout我建议使用m的最后一个元素的副本作为新元素,而不是默认构造的实例。因此,最后一个元素将与数组的所有前面的元素更大或相等。

不相关,但您需要删除旧数组,或使用
std::vector
作为数据存储。编辑:啊,为什么它被注释掉了?欢迎使用Stack Overflow!要求人们发现代码中的错误并不是特别有效。您应该使用调试器(或添加打印语句)来隔离问题,方法是跟踪程序的进度,并将其与预期发生的情况进行比较。一旦这两种情况出现分歧,您就发现了问题。(然后,如果有必要,你应该构造一个。)@KarthikT是的,我在删除数组时遇到另一个问题。它会导致运行时错误。但我会在修复此错误后处理:)。谢谢。你的意思是调整大小会破坏订单吗?还是调整大小后按推操作没有按预期工作?关于你的运行时错误,你需要
{}
在检查数组是否存在的
if
周围。
template <class Datatype>
//--------------------------------------------------------------------------------------------
//  Class: OrderedArray.            
//--------------------------------------------------------------------------------------------
class OrderedArray
{
//--------------------------------------------------------------------------------------------
//  Member Variables.           
//--------------------------------------------------------------------------------------------
private:
Datatype* m_array;
int size;
int g_size;
int num_elements;   //Counter for the number of elements in the Array.
//--------------------------------------------------------------------------------------------
//  Name:           Constructor.
//  Description:    Constructs the Array.
//--------------------------------------------------------------------------------------------
OrderedArray(int p_size)
{
    //Sets the Array size.
    m_array = new Datatype[p_size]; 
    size = p_size;
    grow_size = 1;  
    //How many elements are in the Array.
    num_elements = 0;               
}