From b9f8ebecb3c01653c9525f82709ec152a90906e0 Mon Sep 17 00:00:00 2001 From: Butcher3Years Date: Thu, 12 Feb 2026 02:05:09 +0530 Subject: [PATCH] About Dynamic array and its properties --- Dynamic Arrays/Vector.cpp | 53 +++++++++++++++++++++++++++++++++++++++ Dynamic Arrays/main.cpp | 30 ++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 Dynamic Arrays/Vector.cpp create mode 100644 Dynamic Arrays/main.cpp diff --git a/Dynamic Arrays/Vector.cpp b/Dynamic Arrays/Vector.cpp new file mode 100644 index 0000000..8101c81 --- /dev/null +++ b/Dynamic Arrays/Vector.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +struct Vertex +{ + float x, y, z; +}; + +std::ostream& operator<<(std::ostream& stream, const Vertex& vertex) +{ + stream << vertex.x << ", " << vertex.y << ", " << vertex.z; + return stream; +} + +void Funtion(const std::vector& vertices) //by const ref we ensuring we are not copying while linking to Dynamic array +{ + +} + +int main() +{ + std::vector vertices; + vertices.push_back({ 1, 2, 3}); //to add stuff .Add in other languages we have constructor here but we have used init list + vertices.push_back({ 4, 5, 6}); + Funtion(vertices); + + for (int i = 0; i < vertices.size(); i++) + std::cout << vertices[i] << std::endl; + + + //vertices.clear(); //make array size back to 0 + vertices.erase(vertices.begin() + 1 ); //to remove 2nd element we cant write like 2 ,it takes const iterator + + for (const Vertex& v : vertices) //range for loops and with const ref we are not copying data + std::cout << v << std::endl; //by writting after erase it did eliminate 2nd element + + + std::cin.get(); +} + + + + // we need to memtion and name adn if there we even can pass primitive types like int float + // pointers placement depends on which type did it take heap or inline stack [class or struct] on vector + // means we need to use vertex pointers or vertex objects + // it is optimal to store vertex obj than vector ptrs + //cause they are inline and dynamic arrays memory are contigious[in row] + //in vector strings everything need to recopy and reallocate for resizing which is slow,where as in ptrs the actual mem stays intact cause were just holding ptrs and that mem stored in diff parts + // moving instead of copying largely solves this particular issue ,but there still some copying which is not ideal + // as vector is a class we do know size + //in c++ we done through index like normal array ,in java it is .get(); + \ No newline at end of file diff --git a/Dynamic Arrays/main.cpp b/Dynamic Arrays/main.cpp new file mode 100644 index 0000000..567be9d --- /dev/null +++ b/Dynamic Arrays/main.cpp @@ -0,0 +1,30 @@ +#include +#include + +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() +{ + //Vertex vertices[5]; + Vertex* vertices = new Vertex[5]; + vertices[4]; + + std::cin.get(); +} + +//this is about templated standard library[a library filled with container types contain data] +//whole thing made of templates and data type the conatainer contains that actually you to decide!! +//you can provide the underlying data type that this datastructure actually hanldles +//it called arraylist rather than vector cause these are dynamic arrays !! +//its a set that doesnt enforce any kind of uniqueness to its actual elments so ,unlike arrays these can resize +//suppose we have 10 elements initially and later its 11 we can resize array from 10 to 11[creates new array copies everything from array 10 and delete old one ] +//ignoring std array for static array and size tied to both stack and heap for continously adding inputs we can create array of very large size [not preferrable] \ No newline at end of file