动态数组Dynamic array/ Vector class

动态数组:动态的变化数组大小,创建新大小的数组,然后删除老数组
称为Vector class,其实是Dynamic array;其为内存连续的数组,并且尽量使用对象而不是指针进行修改

静态数组:

#include <iostream>
#include <string>
int main()
{
    Vertex* vertices = new Vertex[5];
	vertices[0];
	std::cin.get();
}

动态数组:使用标准模板库的Vector类(std::vector)

#include <iostream>
#include <string>
#include <vector>

struct Vertex //结构体
{
	float x, y, z;
};

std::ostream& operator<<(std::ostream& stream, const Vertex& vertex)
{
	stream << vertex.x << "," << vertex.y << "," << vertex.z;
	return stream;
} //输出运算符的重载,可以很容易把它打印给控制台了

int main()
{
	std::vector<Vertex> vertices; //C++和C#都可以传递原始类型Vertex,在Java中不行需要用Integer类或其他
	vertices.push_back({ 1, 2, 3 });
	vertices.push_back({ 4, 5, 6 });

	for (int i = 0; i < vertices.size(); i++)
		std::cout << vertices[i] << std::endl;   //C++和C#可以堆索引操作符进行重载;std::cout << vertices.get() Java没有操作符重载所以要这么写

	// vertices.clear(); //数组清0
	vertices.erase(vertices.begin() + 1); //将数组的第二个元素4,5,6清除

	for (Vertex& v : vertices)
		std::cout << v << std::endl;

	std::cin.get();
}

将Vector传递给函数或数组时,通过引用传递他们

#include <iostream>
#include <string>
#include <vector>

struct Vertex //结构体
{
	float x, y, z;
};

std::ostream& operator<<(std::ostream& stream, const Vertex& vertex)
{
	stream << vertex.x << "," << vertex.y << "," << vertex.z;
	return stream;
}

//将Vector传递给函数或数组时,通过引用传递他们
void Function(const std::vector<Vertex>& vertices)
{
}

int main()
{
	std::vector<Vertex> vertices;
	vertices.push_back({ 1, 2, 3 });
	vertices.push_back({ 4, 5, 6 });
	Function(vertices);

	for (int i = 0; i < vertices.size(); i++)
		std::cout << vertices[i] << std::endl; 
	vertices.erase(vertices.begin() + 1); //将数组的第二个元素4,5,6清除
	for (Vertex& v : vertices)
		std::cout << v << std::endl;

	std::cin.get();
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's the code for the Vector class template that meets the requirements you specified: ```c++ template <class T> class Vector { public: Vector() : data(nullptr), size(0), capacity(0) {} ~Vector() { delete[] data; } T& operator[](int index) { return data[index]; } void add(T value) { if (size == capacity) { reserve(capacity == 0 ? 1 : capacity * 2); } data[size++] = value; } void reserve(int newCapacity) { T* newData = new T[newCapacity]; for (int i = 0; i < size; ++i) { newData[i] = data[i]; } capacity = newCapacity; std::swap(data, newData); delete[] newData; } int getSize() const { return size; } private: T* data; int size; int capacity; }; ``` The Vector class template has the following features: - An empty constructor that initializes the data pointer to `nullptr`, the size and capacity to 0. - A destructor that deallocates the data array. - An `operator[]` that returns a reference to the element at the specified index. - An `add` method that appends the specified element to the end of the data array. If the array is full, it calls the `reserve` method to increase the capacity. - A `reserve` method that resizes the data array to the specified capacity. It allocates a new array, copies the existing elements to it, swaps the pointers and deallocates the old array. - A `getSize` method that returns the current size of the data array. Note that the behavior of this Vector class may differ from that of std::vector in terms of performance and exception guarantees. Therefore, it should be used with caution and tested thoroughly for reliability and maintainability in the context of your simulation.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值