c++ - 您如何在数组的特定位置插入数字?

标签 c++ arrays vector integer project

#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
    {

    }

这是我遇到问题的地方^^^。我尝试编程的某些东西根本根本不起作用,我不能使用,因为该项目是使用数组制作“ vector ”类。在这种情况下,我迷失了如何在数组中插入整数,因为我找不到分配的数组。老师在这里说了它,但我似乎根本不称呼它。
#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。我尝试在网上查找,但似乎找不到我了解的内容。

最佳答案

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

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);
}

关于c++ - 您如何在数组的特定位置插入数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60875948/

相关文章:

C++ header 保护

c++ - 为什么此代码会出现段错误?

php - 如何将 html 输入转换为 php 数组

vector - Clojure:对 map 向量中 map 中的每个值应用函数

c++ - 有没有更简单的方法可以在 hh :mm:ss format? 中获取当前时间

c++ - 无法在Lambda中使用asm函数?

c++ - 文件输入到 struct struct 数组中

python - 标准化的 numpy/scipy `asarray` 装饰器

c++ - STL vector 性能

r - R中如何将向量转化为矩阵?