【问题标题】:How would you insert a number in a specific place in an array?如何在数组的特定位置插入数字?
【发布时间】:2020-07-07 14:22:23
【问题描述】:
#include <iostream>

class Vector
{
public:

    int size;
    int* contents;
    int capacity;

    Vector();

    ~Vector();


    void PushFront(int value);

    //void Vector(int initialCapacity);

    void PushBack(int value);


    int& At(int index);


    int& operator[](int index);


    void Clear();


    int Size();


    bool IsEmpty();


    void Resize(int newSize);


    void Reserve(int newCapacity);


    int GetCapacity();


    void EraseAt(int index);


    void Erase(int value);


    int Find(int value);

    bool Contains(int value);

    void Insert(int value, int index);
};

所以这些是我在另一个文件中使用的函数声明,这些是供参考的。

#pragma once
#include <iostream>
#include <string>
#include "Header.h"


    //double* vPointer;
    double* vPointer;
    int* myArray[];



    Vector::Vector()
    {
        std::cout << "Vector Class Created." << std::endl;
        capacity = 10,
        contents = new int[capacity],
        size = 0;
    }


    Vector::~Vector()  // Deallocates any memory the container needed to allocate
    {
        delete[] vPointer;
        std::cout << "Vector Class Deallocated." << std::endl;

    }


    void Vector::PushFront(int value) // Adds a single value to beginning of the container
    {
        for (int i = 0; i <= size; ++i)
        {

            if (value < vPointer[capacity])
            {
                Reserve(capacity + 1);
            }
            if (i == value)
            {
                std::cout << "testing." << std::endl;
            }
            if (i > value)
            {
                std::cout << "testing (code 2)." << std::endl;
            }
        }
    }

    void Vector::PushBack(int value) // Adds a single value to end of the container
    {
        if (size == capacity)
        {
            Reserve(capacity + 1);
        }
        contents[size] = value;
        size++;
    };

    void Vector::Reserve(int newCapacity) // Allocates room for at least this many values.  Does not shrink the storage
    {
        if (capacity < newCapacity)
        {
            int* newArray = new int[newCapacity];
            for (int i = 0; i < size; i++)
            {
                newArray[i] = contents[i];
            }
            delete[] contents;
            contents = newArray;
            capacity = newCapacity;
        }
    }


    int& Vector::At(int index) // Return a reference to the element at the given index.  If unable, throws an exception.
    {
        return contents[index];
    }

    int& Vector::operator[](int index) // Return a reference to the element at the given index.  If unable, throws an exception.
    {
        std::cout << contents[index] << std::endl;
        return contents[index];
    }

    void Vector::Clear() // Remove all elements from the container
    {
        size = 0;
    }

    int Vector::Size() // Returns the number of elements in this container
    {
        return size;
        std::cout << size;
    }

    bool Vector::IsEmpty() // Returns whether or not the container has any elements
    {
        std::cout << "Container has the following number of elements: " << vPointer << std::endl;
        return vPointer;
    }

    void Vector::Resize(int newSize)  // Adds or removes elements from the end of the container to achieve the given new size
    {
        if (newSize < size)
        {
            size == newSize;

            std::cout << newSize << std::endl;

        }



        else if (newSize > capacity)
        {
            Reserve(newSize);
            size == newSize;
            return;
        }

        else 
        {
            std::cout << "something went wrong! Attempting to ReSize Anyways..." << std::endl;
            size = newSize;

            std::cout << "Vector Resized. newSize = " << newSize << std::endl;

        }


    }

    int Vector::GetCapacity()  // Returns the amount of allocated space
    {
        return capacity;

    }


    void Vector::EraseAt(int index)  // Removes the value at the given index, decreasing the contained size
    {
        for (int i = 0; i < size; i++)
        {
            if (vPointer[i] == index)
            {
                std::cout << "semi-functional." << std::endl;

            }
        }
    }

    void Vector::Erase(int value)  // Removes one value from the container: the first that matches
    {
        for (int i = 0; i < size; i++)
        {
            if (value == vPointer[i])
            {
                vPointer[i] = vPointer[i + 1];
            }
            else
            {
                //cout << "can't erase a value that does not exist." << endl;
                return;
            }
        }
    }


    int Vector::Find(int value)  // Returns the index of the given value, -1 if not found
    {
        for (int i = 0; i < size; i++)
        {
            if (value == vPointer[i])
            {
                std::cout << "Value Found : " << i << std::endl;
                return i;
            }
            else
            {
                return -1;
                std::cout << "Value Not Found" << std::endl;
            }
            if (value != vPointer[i])
            {
                std::cout << "Value Found : " << i << std::endl;
                return i;
            }
        }
    }

    bool Vector::Contains(int value)  // Returns true if this value is in the container
    {
        for (int i = 0; i < size; i++)
        {
            if (value < size)
            {


                std::cout << "The Value " << value << " Is In The Container." << std::endl;
                return value;
                return true;
            }
            else
            {
                std::cout << "The Value Is Not Within The Container." << std::endl;
                return false;
            }
        }

    }
    void Vector::Insert(int value, int index) // Insert the given element at the given position. Position 0 should insert the element at the beginning of the container
    {

    }

它在这里 ^^^ 我遇到问题的地方。我尝试编程的一些东西根本不想工作,我不能使用,因为该项目是使用数组制作一个“向量”类。在这种情况下,我不知道如何将整数插入数组,因为我找不到我们分配的数组。老师说这里,我好像根本叫不出来。

#include <iostream>
#include "Header.h"
#include <vector>

//using namespace std;

int main(int argc, char* argv[])
{
    Vector myVector;
    std::cout << "Initializing 'Vector' Array..." << std::endl;

    std::cout << " " << std::endl;
    std::cout << "myVector.PushBack():" << std::endl;
    //myVector.PushBack      
    myVector.PushBack(3);
    std::cout << "Should be 3: " << myVector.At(0) << std::endl;

    std::cout << " " << std::endl;
    std::cout << "myVector.Reserve():" << std::endl;
    //myVector.Reserve
    std::cout << "Capacity: " << myVector.capacity << std::endl;
    myVector.Reserve(15);
    std::cout << "Should allocate 5 slots in the array: " << std::endl; 
    std::cout << "New Capacity: " << myVector.capacity << std::endl;

    //myVector.At
    std::cout << " " << std::endl;
    std::cout << "myVector.At():" << std::endl;
    std::cout << "Testing myVector.At(#) with value of 0... : " << myVector.At(0) << std::endl;

    //myVector GetCapacity
    std::cout << " " << std::endl;
    std::cout << "myVector.GetCapacity():" << std::endl;
    myVector.GetCapacity();
    std::cout << "Capacity of Array: " << myVector.capacity << std::endl;

    //myVector.Resize()    -not working- 
    std::cout << " " << std::endl;
    std::cout << "myVector.Resize():" << std::endl;
    std::cout << "Orignal Vector Size: " << myVector.size << std::endl;
    myVector.Resize(12);


    //myVector Clear() + myVector Size()
    std::cout << " " << std::endl;
    std::cout << "myVector Clear() + myVector Size():" << std::endl;
    myVector.Clear();
    std::cout << "clearing myVector... Current Size:" << myVector.Size() << std::endl;

    //myVector IsEmpty()
    std::cout << " " << std::endl;
    std::cout << "myVector.IsEmpty();:" << std::endl;
    myVector.IsEmpty();



    std::cout << " " << std::endl;
    std::cout << "myVector.Operator[]:" << std::endl;
    //myVector.operator[](int index)
    myVector.operator[](3);









    std::cout <<  myVector.Size() << std::endl;
    std::cout << "NEEDS DEBUGGING:" << std::endl;

    //-not working - :

    //myVector Erase        
    std::cout << " " << std::endl;
    std::cout << "myVector.Erase:" << std::endl;   
    myVector.Erase(3);
    std::cout << myVector.Size() << std::endl;
    std::cout << myVector.GetCapacity() << std::endl;


    std::cout << " " << std::endl;
    std::cout << "myVector.EraseAt:" << std::endl;
    //myVector EraseAt         
    myVector.EraseAt(3);


    std::cout << " " << std::endl;
    std::cout << "myVector.Contains:" << std::endl;
    //myVector.Contains()
    myVector.Contains(0);


    std::cout << " " << std::endl;
    std::cout << "myVector.Find:" << std::endl;
    //myVector.Find()
    myVector.Find(0);


    std::cout << " " << std::endl;
    std::cout << "myVector.PushFront():" << std::endl;
    //myVector.PushFront(3); 
    //myVector.PushFront(3);


    std::cout << " " << std::endl;
    std::cout << "myVector.Insert:" << std::endl;
    //myVector.Insert()




    //myVector.~Vector();   THIS FUNCTIONS!
    std::cout << " " << std::endl;
    std::cout << "myVector.~Vector()" << std::endl;
    myVector.~Vector();
    //std::cout << "Memory DeAllocated." << std::endl;
    std::cin.get();
}

这些部分只是我在主文件中测试它们。有些东西被标记为损坏,因为它们不起作用。如果有人可以帮助我,我更专注于修复 PushFront 和 Insert。我尝试在网上查找,但似乎找不到我理解的内容。

【问题讨论】:

  • 那么,如何在前面插入一个元素呢?插入中间时几乎相同。但是您似乎有一个更具体的问题?但是这里有很多错误。例如,为什么使用vPointer?它由Vector所有 个实例共享。您可能想使用成员变量contents
  • 想象一下,您的办公桌上有一排您最喜欢的小零食,全部排成一排,中间没有空间。您想在中间插入另一个。你会怎么做呢?
  • 留意size == newSize; 之类的拼写错误,这些比较应该是作业?
  • 嗨,雷。对于未来,stackoverflow 不喜欢过长的代码。它被称为最小工作示例,因此下次请尽量避免粘贴整个项目,只粘贴您遇到问题的部分。

标签: c++ arrays vector integer project


【解决方案1】:

只需 PushBack 元素,然后将其旋转到位

void Vector::Insert(int value, int index) // Insert the given element at the given position. Position 0 should insert the element at the beginning of the container
{
  PushBack(value);
  std::rotate(contents + index, contents + size - 1, contents + size);
}

【讨论】:

    猜你喜欢
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    相关资源
    最近更新 更多