Skip to content

Instantly share code, notes, and snippets.

View john9francis's full-sized avatar

John Francis john9francis

View GitHub Profile
@john9francis
john9francis / map_counting.cc
Created May 4, 2024 12:43
Use case for std::map: counting instances of something
#include <iostream>
#include <string>
#include <map>
#include <vector>
int main()
{
// a map is a really good way to keep track of information.
// if the key doesn't exist, it will be created.
// if the key does exist, it will just reassign the value
@john9francis
john9francis / this_inheritance.cc
Created June 19, 2024 20:33
How c++ 'this' keyword works with inheritance. Does it take the parent object or the child object?
// explanation:
// Defines 3 classes: Parent, Child, and Thing.
// Thing takes in a parent into its constructor.
// Parent initializes a Thing in its constructor using 'this.'
// Child does nothing but initialize Parent().
//
// main initializes a Child class, and calls a function from Thing.
// Thing, which was initialized using 'this' in the parent constructor,
// uses that 'this' to refer to the Child, not the Parent.
@john9francis
john9francis / entangled_copy.cc
Created June 22, 2024 15:11
Copying a class in C++ creates a new instance of that class, but if any variables were declared with "new," those variables don't get copied, only their addresses do. Therefore the original instance and the copy essentially share the same variable.
// showing how copying an instance of a class
// doesn't copy the dynamically allocated variables,
// it simply copies over their address. So the new
// copy shares the old variable.
#include <iostream>
class Thing{
public:
Thing(){ pInt = new int; }
@john9francis
john9francis / how_many_threads.cc
Last active July 12, 2024 17:29
This simple program has a function that should take 5 seconds. The program creates a user-defined number of threads that each execute the function. This was to test how many threads c++ can handle.
// testing how many threads work in c++
// spoiler alert: at least 2000 work, probably more
#include <thread>
#include <iostream>
#include <chrono>
#include <vector>
void count(int threadNumber){
// Function that counts 5 seconds