Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Dynamic Arrays/Vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#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;
}

void Funtion(const std::vector<Vertex>& vertices) //by const ref we ensuring we are not copying while linking to Dynamic array
{

}

int main()
{
std::vector<Vertex> 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 <type> 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();

30 changes: 30 additions & 0 deletions Dynamic Arrays/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <string>

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]