add a vector class and switch to std::format

this adds a vector class which is a container for data.
This commit is contained in:
Fries 2023-06-22 19:04:01 -07:00
parent 9a72aa2728
commit 9ee12a7aba
5 changed files with 72 additions and 3 deletions

View file

@ -10,3 +10,4 @@ add_executable(FunctionArguments FunctionArguments.cc)
add_executable(exceptions exceptions.cc) add_executable(exceptions exceptions.cc)
add_executable(HeapMemory HeapMemory.cc) add_executable(HeapMemory HeapMemory.cc)
add_executable(AbstractClasses AbstractClasses.cc) add_executable(AbstractClasses AbstractClasses.cc)
add_executable(vector vector.cc)

View file

@ -1,3 +1,4 @@
#include <format>
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -11,7 +12,7 @@ void sus(std::string susValue, std::string& susReference)
void print_string(std::string nameOf, std::string& string) void print_string(std::string nameOf, std::string& string)
{ {
std::cout << "The value of " << nameOf << " is " << string << std::endl; std::cout << std::format("The value of {} is {}", nameOf, string) << std::endl;
} }
void print_sussy_strings(std::string& sus1, std::string& sus2) void print_sussy_strings(std::string& sus1, std::string& sus2)

View file

@ -1,3 +1,4 @@
#include <format>
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -11,6 +12,7 @@ public:
static void PrintName(Crewmate crewmate) static void PrintName(Crewmate crewmate)
{ {
std::cout << "Crewmates Name: " << crewmate.name << std::endl; std::cout << "Crewmates Name: " << crewmate.name << std::endl;
std::cout << std::format("Crewmates Name: {}", crewmate.name) << std::endl;
} }
}; };
@ -33,6 +35,6 @@ int main()
crewmate.PrintName(crewmate); crewmate.PrintName(crewmate);
if (imposter.imposter) { if (imposter.imposter) {
std::cout << "Imposter is a sussy imposter and their name is " << imposter.name << std::endl; std::cout << std::format("Imposter is a sussy imposter and their name is {}", imposter.name) << std::endl;
} }
} }

View file

@ -1,4 +1,5 @@
import Math; import Math;
#include <format>
#include <iostream> #include <iostream>
void print_squared(double x); void print_squared(double x);
@ -11,5 +12,5 @@ int main()
void print_squared(double x) void print_squared(double x)
{ {
std::cout << "the squared of " << x << " is " << Math::square(x) << std::endl; std::cout << std::format("the squared of {} is {}", x, Math::square(x)) << std::endl;
} }

64
src/vector.cc Normal file
View file

@ -0,0 +1,64 @@
#include <iostream>
#include <stdexcept>
#include <format>
class Vector {
public:
Vector(int size)
: elem { new double[size] }
, sz { size }
{
for (int i = 0; i != size; ++i) {
elem[i] = 0;
}
}
~Vector()
{
delete[] elem;
}
double& operator[](int i)
{
if (i < 0 || i > size() - 1) {
throw std::out_of_range { "Vector::operator[]" };
}
return elem[i];
}
void print_element(int i)
{
if (i < 0 || i > size() - 1) {
throw std::out_of_range { "Vector::print_element(int i)" };
}
std::cout << std::format("element[{}] : {}", i, elem[i]) << std::endl;
}
int size() const
{
return sz;
}
private:
double* elem;
int sz;
};
int main()
{
Vector vec(3);
vec[0] = 1;
vec[1] = 2;
vec[2] = 3;
vec.print_element(0);
vec.print_element(1);
vec.print_element(2);
Vector longVector(100000);
for (int i = 0; i < longVector.size(); i++) {
longVector[i] = 21;
}
}