├── .gitignore ├── README.md ├── algorithms └── permutations │ └── permutations.cpp ├── associative_containers ├── .gitignore ├── CMakeLists.txt ├── README.md ├── build.sh ├── clean.sh └── src │ └── main.cpp ├── classes ├── .gitignore ├── CMakeLists.txt ├── README.md ├── build.sh ├── clean.sh └── src │ └── main.cpp ├── cmake ├── .gitignore ├── CMakeLists.txt ├── README.md └── src │ └── main.cpp ├── compile_time_arithmetic └── README.md ├── conan └── README.md ├── constexpr_vectors ├── .gitignore ├── CMakeLists.txt ├── README.md ├── build.sh ├── clean.sh └── src │ └── main.cpp ├── containers ├── .gitignore ├── CMakeLists.txt ├── README.md ├── build.sh ├── clean.sh └── src │ └── main.cpp ├── exceptions └── README.md ├── functions ├── .gitignore ├── CMakeLists.txt ├── README.md ├── build.sh ├── clean.sh └── src │ └── main.cpp ├── heap ├── .gitignore ├── CMakeLists.txt ├── README.md ├── build.sh ├── clean.sh └── src │ └── main.cpp ├── iostreams └── README.md ├── localization └── README.md ├── math ├── README.md └── limits.cpp ├── move_semantics ├── .gitignore ├── CMakeLists.txt ├── README.md ├── build.sh ├── clean.sh └── src │ └── main.cpp ├── operator_overloading ├── .gitignore ├── CMakeLists.txt ├── README.md ├── build.sh ├── clean.sh └── src │ └── main.cpp ├── random └── README.md ├── regex └── README.md ├── smart_ptrs └── README.md ├── stack ├── .gitignore ├── CMakeLists.txt ├── README.md ├── build.sh ├── clean.sh └── src │ └── main.cpp ├── staticarrays ├── .gitignore ├── CMakeLists.txt ├── README.md ├── build.sh ├── clean.sh └── src │ └── main.cpp ├── strings └── README.md ├── time └── README.md └── tuples └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/c,c++ 2 | 3 | ### C ### 4 | # Prerequisites 5 | *.d 6 | 7 | # Object files 8 | *.o 9 | *.ko 10 | *.obj 11 | *.elf 12 | 13 | # Linker output 14 | *.ilk 15 | *.map 16 | *.exp 17 | 18 | # Precompiled Headers 19 | *.gch 20 | *.pch 21 | 22 | # Libraries 23 | *.lib 24 | *.a 25 | *.la 26 | *.lo 27 | 28 | # Shared objects (inc. Windows DLLs) 29 | *.dll 30 | *.so 31 | *.so.* 32 | *.dylib 33 | 34 | # Executables 35 | *.exe 36 | *.out 37 | *.app 38 | *.i*86 39 | *.x86_64 40 | *.hex 41 | 42 | # Debug files 43 | *.dSYM/ 44 | *.su 45 | *.idb 46 | *.pdb 47 | 48 | # Kernel Module Compile Results 49 | *.mod* 50 | *.cmd 51 | .tmp_versions/ 52 | modules.order 53 | Module.symvers 54 | Mkfile.old 55 | dkms.conf 56 | 57 | ### C++ ### 58 | # Prerequisites 59 | 60 | # Compiled Object files 61 | *.slo 62 | 63 | # Precompiled Headers 64 | 65 | # Compiled Dynamic libraries 66 | 67 | # Fortran module files 68 | *.mod 69 | *.smod 70 | 71 | # Compiled Static libraries 72 | *.lai 73 | 74 | # Executables 75 | bin/ 76 | bin/* 77 | 78 | # End of https://www.gitignore.io/api/c,c++ 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Professional C++ 2 | -------------------------------------------------------------------------------- /algorithms/permutations/permutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | auto main() -> int 6 | { 7 | auto i{0x01B99644}; 8 | std::string x{"DFaeeillnor"}; 9 | while (i--) { 10 | std::next_permutation(x.begin(), x.end()); 11 | } 12 | std::cout << x << "\n"; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /associative_containers/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | build/* 3 | build/*/* 4 | -------------------------------------------------------------------------------- /associative_containers/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0..3.20.1) 2 | PROJECT(associative_containers) 3 | 4 | SET(CMAKE_BUILD_TYPE Release) 5 | 6 | SET(CMAKE_CXX_STANDARD 20) 7 | SET(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | 9 | SET(SOURCES_CPP "src/main.cpp") 10 | 11 | ADD_EXECUTABLE(associative_containers ${SOURCES_CPP}) 12 | 13 | INSTALL(TARGETS associative_containers DESTINATION /usr/local/bin) 14 | -------------------------------------------------------------------------------- /associative_containers/README.md: -------------------------------------------------------------------------------- 1 | # Associative Containers 2 | -------------------------------------------------------------------------------- /associative_containers/build.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | mkdir -p build 3 | cd build 4 | cmake .. 5 | make -j2 all 6 | ./associative_containers 7 | cd .. 8 | -------------------------------------------------------------------------------- /associative_containers/clean.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | -------------------------------------------------------------------------------- /associative_containers/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class map { 10 | class iterator { 11 | 12 | 13 | }; 14 | }; 15 | 16 | auto foo() -> void { 17 | // 18 | auto customComp = [](const std::string& lhs, const std::string& rhs){ 19 | return lhs.size() < rhs.size(); 20 | }; 21 | //std::map> qtyMultiplier; 22 | std::map qtyMultiplier; 23 | 24 | qtyMultiplier.insert(std::make_pair("BTC_USD", 10'000'000)); 25 | 26 | // another way 27 | qtyMultiplier["BTC_USDT"] = 1'000'000; 28 | 29 | for(auto& [k, v]: qtyMultiplier) { 30 | std::cout << k << " = " << v << "\n"; 31 | } 32 | } 33 | 34 | auto main() -> int { 35 | foo(); 36 | } 37 | -------------------------------------------------------------------------------- /classes/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | build/* 3 | build/*/* 4 | -------------------------------------------------------------------------------- /classes/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0..3.20.1) 2 | PROJECT(classes) 3 | 4 | SET(CMAKE_BUILD_TYPE Release) 5 | 6 | SET(CMAKE_CXX_STANDARD 20) 7 | SET(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | 9 | SET(SOURCES_CPP "src/main.cpp") 10 | 11 | ADD_EXECUTABLE(classes ${SOURCES_CPP}) 12 | 13 | INSTALL(TARGETS classes DESTINATION /usr/local/bin) 14 | -------------------------------------------------------------------------------- /classes/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/classes/README.md -------------------------------------------------------------------------------- /classes/build.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | mkdir -p build 3 | cd build 4 | cmake .. 5 | make -j2 all 6 | ./classes 7 | cd .. 8 | -------------------------------------------------------------------------------- /classes/clean.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | -------------------------------------------------------------------------------- /classes/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // OOP 4 | struct Person { 5 | std::string name_; 6 | // std::string m_name; 7 | int age_; 8 | // Special member function 9 | // there is a default. the compiler generates it 10 | // sometimes the compiler does not generate it 11 | // and you need to define it yourself 12 | // Person() {} -> this is fine 13 | Person() = default; 14 | Person(std::string name, int age): name_{name}, age_{age}{ 15 | // list initialization 16 | } 17 | // Overloading the constructor 18 | Person(std::string name): name_{name}, age_{100}{} 19 | 20 | // - Copy Constructor 21 | Person(const Person& copy) { 22 | name_ = copy.name_; 23 | age_ = copy.age_ * 10; 24 | } 25 | // - Copy Assignment 26 | Person& operator=(const Person& copy){ 27 | name_ = copy.name_; 28 | age_ = copy.age_ / 5; 29 | return *this; 30 | } 31 | // - Move constructor // Introduced in C++11 with move semantics 32 | Person(Person&& movedPerson){ 33 | name_ = std::move(movedPerson.name_); 34 | age_ = movedPerson.age_; 35 | } 36 | // - Move Assignment // Introduced in C++11 with move semantics 37 | // - Destructor 38 | ~Person() { 39 | } 40 | }; 41 | 42 | class Animal { 43 | std::string name_; 44 | int height_; 45 | }; 46 | 47 | auto main(int argc, char** argv) -> int { 48 | //Person p("bob"); // alocating the struct on 49 | //Person p2(p); // Calls the copy constructor 50 | //p2 = p; // Calls the copy assignment 51 | //std::cout << p2.age_ << "\n"; 52 | std::string name = "this is my name"; 53 | std::cout << &name << "\n"; 54 | std::string movedName = std::move(name); 55 | std::cout << &movedName << "\n"; 56 | std::cout << movedName << "\n"; 57 | // moved from state it's still in a valid but unspecified state 58 | } 59 | -------------------------------------------------------------------------------- /cmake/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/cmake/.gitignore -------------------------------------------------------------------------------- /cmake/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0..3.20.1) 2 | PROJECT(simple) 3 | 4 | SET(CMAKE_BUILD_TYPE Release) 5 | 6 | SET(CMAKE_CXX_STANDARD 20) 7 | SET(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | 9 | SET(SOURCES_CPP "src/main.cpp") 10 | 11 | ADD_EXECUTABLE(simple ${SOURCES_CPP}) 12 | 13 | INSTALL(TARGETS simple DESTINATION /usr/local/bin) 14 | -------------------------------------------------------------------------------- /cmake/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/cmake/README.md -------------------------------------------------------------------------------- /cmake/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | auto main(int argc, char** argv) -> int { 4 | std::cout << "test" << std::endl; 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /compile_time_arithmetic/README.md: -------------------------------------------------------------------------------- 1 | “The compile-time rational arithmetic library is new to C++11 and provides a ratio template class defined in the header file. This ratio class can exactly represent any finite rational number defined by a numerator and denominator.” 2 | 3 | Excerpt From: Marc Gregoire. “Professional C++.” Apple Books. 4 | -------------------------------------------------------------------------------- /conan/README.md: -------------------------------------------------------------------------------- 1 | # How to Use Conan 2 | -------------------------------------------------------------------------------- /constexpr_vectors/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | build/* 3 | build/*/* 4 | -------------------------------------------------------------------------------- /constexpr_vectors/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0..3.20.1) 2 | PROJECT(constexpr_vectors) 3 | 4 | SET(CMAKE_BUILD_TYPE Release) 5 | 6 | SET(CMAKE_CXX_STANDARD 20) 7 | SET(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | 9 | SET(SOURCES_CPP "src/main.cpp") 10 | 11 | ADD_EXECUTABLE(constexpr_vectors ${SOURCES_CPP}) 12 | 13 | INSTALL(TARGETS constexpr_vectors DESTINATION /usr/local/bin) 14 | -------------------------------------------------------------------------------- /constexpr_vectors/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/constexpr_vectors/README.md -------------------------------------------------------------------------------- /constexpr_vectors/build.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | mkdir -p build 3 | cd build 4 | cmake .. 5 | make -j2 all 6 | ./constexpr_vectors 7 | cd .. 8 | -------------------------------------------------------------------------------- /constexpr_vectors/clean.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | -------------------------------------------------------------------------------- /constexpr_vectors/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | auto main(int argc, char** argv) -> int { 4 | } 5 | -------------------------------------------------------------------------------- /containers/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | build/* 3 | build/*/* 4 | -------------------------------------------------------------------------------- /containers/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0..3.20.1) 2 | PROJECT(containers) 3 | 4 | SET(CMAKE_BUILD_TYPE Release) 5 | 6 | SET(CMAKE_CXX_STANDARD 20) 7 | SET(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | 9 | SET(SOURCES_CPP "src/main.cpp") 10 | 11 | ADD_EXECUTABLE(containers ${SOURCES_CPP}) 12 | 13 | INSTALL(TARGETS containers DESTINATION /usr/local/bin) 14 | -------------------------------------------------------------------------------- /containers/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/containers/README.md -------------------------------------------------------------------------------- /containers/build.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | mkdir -p build 3 | cd build 4 | cmake .. 5 | make -j2 all 6 | ./containers 7 | cd .. 8 | -------------------------------------------------------------------------------- /containers/clean.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | -------------------------------------------------------------------------------- /containers/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | auto foo() -> void { 10 | // Capacity 11 | // Size 12 | std::vector value{1, 3, 4}; 13 | value.reserve(5); 14 | 15 | std::cout << "Size is " << value.size() << "\n"; 16 | std::cout << "Capacity is " << value.capacity() << "\n"; 17 | 18 | std::cout << "Elements of value are, by copy:" << "\n"; 19 | 20 | for (auto e: value) { 21 | std::cout << e << "\n"; 22 | } 23 | 24 | std::cout << "Elements of value are, by reference:" << "\n"; 25 | 26 | // auto& e == auto & e == auto &e 27 | 28 | for (auto &e: value) { 29 | std::cout << e << "\n"; 30 | } 31 | 32 | std::cout << "With iterators \n"; 33 | std::vector::iterator it = value.begin(); 34 | for (; it != value.end(); ++it) { 35 | std::cout << *it << "\n"; 36 | } 37 | 38 | value.push_back(10); 39 | value.push_back(10); 40 | value.push_back(10); 41 | value.push_back(10); 42 | value.push_back(10); 43 | value.front(); 44 | value.back(); 45 | 46 | // So erase always takes the iterator as a parameter 47 | value.erase( value.begin() + 1); 48 | 49 | std::cout << "Size is " << value.size() << "\n"; 50 | std::cout << "Capacity is " << value.capacity() << "\n"; 51 | } 52 | 53 | auto main() -> int { 54 | foo(); 55 | } 56 | -------------------------------------------------------------------------------- /exceptions/README.md: -------------------------------------------------------------------------------- 1 | “The C++ language supports exceptions, which allow functions or methods to pass errors of various types up to calling functions or methods. The C++ standard library provides a class hierarchy of exceptions that you can use in your program as is, or that you can subclass to create your own exception types. Chapter 10 covers the details of exceptions and the standard exception classes.” 2 | 3 | Excerpt From: Marc Gregoire. “Professional C++.” Apple Books. 4 | -------------------------------------------------------------------------------- /functions/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | build/* 3 | build/*/* 4 | -------------------------------------------------------------------------------- /functions/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0..3.20.1) 2 | PROJECT(function) 3 | 4 | SET(CMAKE_BUILD_TYPE Release) 5 | 6 | SET(CMAKE_CXX_STANDARD 20) 7 | SET(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | 9 | SET(SOURCES_CPP "src/main.cpp") 10 | 11 | ADD_EXECUTABLE(function ${SOURCES_CPP}) 12 | 13 | INSTALL(TARGETS function DESTINATION /usr/local/bin) 14 | -------------------------------------------------------------------------------- /functions/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/functions/README.md -------------------------------------------------------------------------------- /functions/build.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | mkdir -p build 3 | cd build 4 | cmake .. 5 | make -j2 all 6 | ./function 7 | cd .. 8 | -------------------------------------------------------------------------------- /functions/clean.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | -------------------------------------------------------------------------------- /functions/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | auto foo(float a, float b) -> float { 4 | // Trailing return type 5 | float c = a + b; 6 | // The compiler will deduce the type of c 7 | // to be integer 8 | return c; 9 | } 10 | 11 | auto main(int argc, char** argv) -> int { 12 | // narrowing conversion 13 | auto b = 3.2f; 14 | // without suffix literal f, the default type would be double 15 | // but with the suffix literal f, it would be defined as float 16 | // during compile time this is changed to 3 instead of 3.2 17 | auto a = 2.0f; 18 | std::clog << foo(a, b) << std::endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /heap/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | build/* 3 | build/*/* 4 | -------------------------------------------------------------------------------- /heap/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0..3.20.1) 2 | PROJECT(heap) 3 | 4 | SET(CMAKE_BUILD_TYPE Release) 5 | 6 | SET(CMAKE_CXX_STANDARD 20) 7 | SET(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | 9 | SET(SOURCES_CPP "src/main.cpp") 10 | 11 | ADD_EXECUTABLE(heap ${SOURCES_CPP}) 12 | 13 | INSTALL(TARGETS heap DESTINATION /usr/local/bin) 14 | -------------------------------------------------------------------------------- /heap/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/heap/README.md -------------------------------------------------------------------------------- /heap/build.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | mkdir -p build 3 | cd build 4 | cmake .. 5 | make -j2 all 6 | ./heap 7 | cd .. 8 | -------------------------------------------------------------------------------- /heap/clean.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | -------------------------------------------------------------------------------- /heap/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int square(int num) { 6 | return num * num; 7 | } 8 | 9 | // Stack and heap allocation 10 | // Object lifetime 11 | // Prefer nullptr to 0 or NULL 12 | // Distinguish between () (Parent Init) and {} (Braced Init, Uniform Init) for initialization 13 | 14 | auto foo() -> int { 15 | // Stack object are automatically destroyed 16 | int a = 100; 17 | int *aheap = new int(400); // maloc(sizeof(int)) 18 | 19 | std::cout << "\nHeap allocated var:" << *aheap << "\n"; 20 | { 21 | int a = 102; 22 | // a is allocated on the stack 23 | std::cout << a << "\n"; 24 | // a is then called to print, then destoyed 25 | std::cout << "\nHeap allocated var:" << *aheap << "\n"; 26 | *aheap = 500; 27 | } 28 | std::cout << "\nHeap allocated var:" << *aheap << "\n"; 29 | // The closing parenthesis marks the completion of the scope 30 | std::cout << a << "\n"; 31 | delete aheap; 32 | return 0; 33 | } 34 | 35 | void print(const std::vector& v) { 36 | for(auto e: v) { 37 | std::cout << e << "\n"; 38 | } 39 | } 40 | 41 | auto initialization() -> int { 42 | // this is called parentheses initialization 43 | std::vector parentheses_initialization(2, 10); 44 | print(parentheses_initialization); 45 | std::vector uniform_initialization{2, 10}; 46 | print(uniform_initialization); 47 | // this is called uniform initialization 48 | // { } most vexing parser safe 49 | // { } prevents narrowing conversion 50 | // what it means is that if you do 51 | int getName(3.4); // this is ok 52 | // int getName{3.4}; // this is not ok 53 | // { } also defaults the vaues, call the constructor for user defined types 54 | int vold; 55 | std::cout << vold << "\n"; 56 | int test = 0; // copy assignment initialization 57 | std::cout << test << "\n"; 58 | return 0; 59 | } 60 | 61 | void f(int){ 62 | std::cout << "int foo called\n"; 63 | } 64 | void f(bool){ 65 | std::cout << "bool foo called\n"; 66 | } 67 | void f(void*){ 68 | std::cout << "void star foo called\n"; 69 | } 70 | 71 | auto main(int argc, char** argv) -> int { 72 | int* height = new int; 73 | if (height == nullptr){ 74 | std::cout << "Memory full \n"; 75 | } 76 | f(0); 77 | //f(NULL); // error: call of overloaded ‘f(NULL)’ is ambiguous 78 | f(nullptr); 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /iostreams/README.md: -------------------------------------------------------------------------------- 1 | “C++ introduces a new model for input and output using streams. The C++ library provides routines for reading and writing built-in types from and to files, console/keyboard, and strings. C++ also provides the facilities for coding your own routines for reading and writing your own objects. Chapter 1 reviews the basics of I/O streams. Chapter 15 provides the details of streams.” 2 | 3 | Excerpt From: Marc Gregoire. “Professional C++.” Apple Books. 4 | -------------------------------------------------------------------------------- /localization/README.md: -------------------------------------------------------------------------------- 1 | “C++ also provides support for localization. These features allow you to write programs that work with different languages, character formats, and number formats. Chapter 14 discusses localization.” 2 | 3 | Excerpt From: Marc Gregoire. “Professional C++.” Apple Books. 4 | -------------------------------------------------------------------------------- /math/README.md: -------------------------------------------------------------------------------- 1 | # Mathematical Utilites 2 | 3 | “The C++ library provides some mathematical utility classes. Although they are templatized so that you can use them with any type, they are not generally considered part of the standard template library. Unless you are using C++ for numeric computation, you will probably not need to use these utilities.” 4 | 5 | Excerpt From: Marc Gregoire. “Professional C++.” Apple Books. 6 | -------------------------------------------------------------------------------- /math/limits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | cout << "Max int value " 9 | << numeric_limits::max() 10 | << endl; 11 | 12 | cout << "Lowest int value " 13 | << numeric_limits::lowest() 14 | << endl; 15 | 16 | cout << "Max double value " 17 | << numeric_limits::max() 18 | << endl; 19 | 20 | cout << "Lowest double value " 21 | << numeric_limits::lowest() 22 | << endl; 23 | } 24 | -------------------------------------------------------------------------------- /move_semantics/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | build/* 3 | build/*/* 4 | -------------------------------------------------------------------------------- /move_semantics/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0..3.20.1) 2 | PROJECT(move_semantics) 3 | 4 | SET(CMAKE_BUILD_TYPE Release) 5 | 6 | SET(CMAKE_CXX_STANDARD 20) 7 | SET(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | 9 | SET(SOURCES_CPP "src/main.cpp") 10 | 11 | ADD_EXECUTABLE(move_semantics ${SOURCES_CPP}) 12 | 13 | INSTALL(TARGETS move_semantics DESTINATION /usr/local/bin) 14 | -------------------------------------------------------------------------------- /move_semantics/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/move_semantics/README.md -------------------------------------------------------------------------------- /move_semantics/build.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | mkdir -p build 3 | cd build 4 | cmake .. 5 | make -j2 all 6 | ./move_semantics 7 | cd .. 8 | -------------------------------------------------------------------------------- /move_semantics/clean.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | -------------------------------------------------------------------------------- /move_semantics/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | auto foo2() -> void { 7 | std::string something = "hello"; 8 | 9 | // rvalue casting 10 | // the object myString is no longer needed please steal its memory 11 | std::cout << something << "\n"; 12 | std::string new_something = std::move(something); 13 | std::cout << something << "\n" << new_something << "\n"; 14 | } 15 | 16 | auto foo() -> void { 17 | std::string myString = "Hello this is a long string so SSO cannot optimize it"; // SSO 18 | std::printf("%p \n", myString.c_str()); 19 | std::string newStr = std::move(myString); 20 | std::printf("%p \n", newStr.c_str()); 21 | std::cout << myString << "\n" << newStr << "\n"; 22 | } 23 | 24 | auto main() -> int { 25 | foo2(); 26 | foo(); 27 | } 28 | -------------------------------------------------------------------------------- /operator_overloading/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | build/* 3 | build/*/* 4 | -------------------------------------------------------------------------------- /operator_overloading/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0..3.20.1) 2 | PROJECT(operator_overloading) 3 | 4 | SET(CMAKE_BUILD_TYPE Release) 5 | 6 | SET(CMAKE_CXX_STANDARD 20) 7 | SET(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | 9 | SET(SOURCES_CPP "src/main.cpp") 10 | 11 | ADD_EXECUTABLE(operator_overloading ${SOURCES_CPP}) 12 | 13 | INSTALL(TARGETS operator_overloading DESTINATION /usr/local/bin) 14 | -------------------------------------------------------------------------------- /operator_overloading/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/operator_overloading/README.md -------------------------------------------------------------------------------- /operator_overloading/build.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | mkdir -p build 3 | cd build 4 | cmake .. 5 | make -j2 all 6 | ./operator_overloading 7 | cd .. 8 | -------------------------------------------------------------------------------- /operator_overloading/clean.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | -------------------------------------------------------------------------------- /operator_overloading/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | // Create a matrix class 9 | // - Overload the operator * 10 | // - Overload the operator + 11 | // - Overload the operator - 12 | 13 | class Matrix { 14 | public: 15 | std::vector> matrixElements_; 16 | 17 | //public: 18 | // hidden friend 19 | 20 | // Constructor 21 | Matrix(std::vector> initializedMatrixElements): matrixElements_{initializedMatrixElements}{} 22 | friend Matrix& operator+(Matrix& lhs, Matrix rhs); 23 | friend Matrix& operator-(Matrix& lhs, Matrix& rhs); 24 | friend Matrix& operator*(Matrix& lhs, Matrix& rhs); 25 | }; 26 | 27 | Matrix& operator+(Matrix& lhs, Matrix rhs){ 28 | // matrixElements_ is a vector of a vector 29 | // here we need to access the elements 30 | // so better to use the C-styled loops 31 | for (int x = 0; x < rhs.matrixElements_.size(); ++x) { 32 | for (int y = 0; y < rhs.matrixElements_[x].size(); ++y) { 33 | lhs.matrixElements_[x][y] += rhs.matrixElements_[x][y]; 34 | } 35 | } 36 | return lhs; 37 | } 38 | 39 | Matrix& operator-(Matrix& lhs, Matrix& rhs){ 40 | for (int x = 0; x < rhs.matrixElements_.size(); ++x) { 41 | for (int y = 0; y < rhs.matrixElements_[x].size(); ++y) { 42 | lhs.matrixElements_[x][y] -= rhs.matrixElements_[x][y]; 43 | } 44 | } 45 | return lhs; 46 | } 47 | 48 | // [1 2 3 4] * [11] = [30 30] 49 | // [1 2 3 4] [22] [30 30] 50 | // [33] 51 | // [44] 52 | // 53 | // [1 2] * [1 2] = [3 5] 54 | // [1 2] [1 2] [3 5] 55 | 56 | Matrix& operator*(Matrix& lhs, Matrix& rhs){ 57 | std::vector> resultElements; 58 | Matrix result(resultElements); 59 | 60 | // lhs.matrixElements_.size = rows 61 | if (lhs.matrixElements_.size() != rhs.matrixElements_[0].size()) { 62 | //std::cout << "The size of the two matrices are different. Aborting.." << "\n"; 63 | throw std::runtime_error("The size of the two matrices are different. Aborting.."); 64 | } 65 | 66 | int i = 0; 67 | for (int x = 0; x < rhs.matrixElements_.size(); ++x) { 68 | int sum = 0; 69 | for (int y = 0; y < rhs.matrixElements_[x].size(); ++y) { 70 | std::cout << "Multiplying " << lhs.matrixElements_[x][y] << " x " << rhs.matrixElements_[y][x] << "\n"; 71 | sum += lhs.matrixElements_[x][y] * rhs.matrixElements_[y][x]; 72 | } 73 | result.matrixElements_[x][i] = sum; 74 | ++i; 75 | } 76 | return result; 77 | } 78 | 79 | class ComplexNumber { 80 | private: 81 | int real_; 82 | int imaginary_; 83 | 84 | public: 85 | //ComplexNumber& operator+(ComplexNumber& rhs){ 86 | //real_ += rhs.real_; 87 | //return *this; 88 | //} 89 | 90 | ComplexNumber(int real, int imag): real_{real}, imaginary_{imag}{} 91 | friend ComplexNumber operator+(ComplexNumber& lhs, ComplexNumber & rhs); 92 | }; 93 | 94 | // There are two places where we can implement operator overloading 95 | // - Inside the class: when it's modifying the lhs 96 | // - Outside the class: when it does not modify the lhs 97 | ComplexNumber operator+(ComplexNumber& lhs, ComplexNumber &rhs) { 98 | return ComplexNumber(lhs.real_ + rhs.real_, lhs.imaginary_ + rhs.imaginary_); 99 | } 100 | 101 | 102 | auto goo() -> void { 103 | ComplexNumber c1(1, 2); 104 | ComplexNumber c2(3, 4); 105 | 106 | ComplexNumber r = c1 + c2; 107 | // ComplexNumber r = operator+(c1, c2); 108 | } 109 | 110 | auto foo() -> void { 111 | std::vector> m1Elements{{1, 2}, {1, 2}}; 112 | std::vector> m2Elements{{1, 2}, {1, 2}}; 113 | 114 | Matrix m1(m1Elements); 115 | Matrix m2(m2Elements); 116 | 117 | auto m3 = m1 + m2; 118 | 119 | for (auto& x: m3.matrixElements_){ 120 | for (auto& y: x) { 121 | std::cout << y << "\n"; 122 | } 123 | } 124 | 125 | auto m4 = m1 * m2; 126 | 127 | std::cout << "Matrix multiplication" << "\n"; 128 | 129 | for (auto& x: m3.matrixElements_){ 130 | for (auto& y: x) { 131 | std::cout << y << "\n"; 132 | } 133 | } 134 | 135 | } 136 | 137 | 138 | auto main() -> int { 139 | foo(); 140 | //goo(); 141 | } 142 | -------------------------------------------------------------------------------- /random/README.md: -------------------------------------------------------------------------------- 1 | # Random Numbers 2 | 3 | “C++11 adds a complete random number library, which is much more powerful than the old C++ way of generating random numbers. The new library comes with uniform random number generators, random number engines, random number engine adaptors, and random number distributions. All of these can be used to give you random numbers more suited to your problem domain, such as normal distributions, negative exponential distributions, etc.” 4 | 5 | Excerpt From: Marc Gregoire. “Professional C++.” Apple Books. 6 | -------------------------------------------------------------------------------- /regex/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/regex/README.md -------------------------------------------------------------------------------- /smart_ptrs/README.md: -------------------------------------------------------------------------------- 1 | # Smart Pointers 2 | 3 | One of the problems faced in doing robust programming is knowing when to delete an object. There are several failures that can happen. 4 | 5 | 1. “A first problem is not deleting the object at all (failing to free the storage). This is known as memory leaks, where objects accumulate and take up space but are not used. ” 6 | 7 | 2. “Another problem is where someone deletes the storage but others are still pointing to that storage, resulting in pointers to storage which is either no longer in use or has been reallocated for another purpose. This is known as dangling pointers. ” 8 | 9 | 3. “One more problem is when one piece of code frees the storage, and another piece of code attempts to free the same storage. This is known as double-freeing. ” 10 | 11 | Excerpt From: Marc Gregoire. “Professional C++.” Apple Books. 12 | -------------------------------------------------------------------------------- /stack/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | build/* 3 | build/*/* 4 | -------------------------------------------------------------------------------- /stack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0..3.20.1) 2 | PROJECT(heapstack) 3 | 4 | SET(CMAKE_BUILD_TYPE Release) 5 | 6 | SET(CMAKE_CXX_STANDARD 20) 7 | SET(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | 9 | SET(SOURCES_CPP "src/main.cpp") 10 | 11 | ADD_EXECUTABLE(heapstack ${SOURCES_CPP}) 12 | 13 | INSTALL(TARGETS heapstack DESTINATION /usr/local/bin) 14 | -------------------------------------------------------------------------------- /stack/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/stack/README.md -------------------------------------------------------------------------------- /stack/build.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | mkdir -p build 3 | cd build 4 | cmake .. 5 | make -j2 all 6 | ./heapstack 7 | cd .. 8 | -------------------------------------------------------------------------------- /stack/clean.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | -------------------------------------------------------------------------------- /stack/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | auto main(int argc, char** argv) -> int { 5 | // Stack object are automatically destroyed 6 | int a = 100; 7 | { 8 | int a = 102; 9 | // a is allocated on the stack 10 | std::cout << a << "\n"; 11 | // a is then called to print, then destoyed 12 | } 13 | // The closing parenthesis marks the completion of the scope 14 | std::cout << a << "\n"; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /staticarrays/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | build/* 3 | build/*/* 4 | -------------------------------------------------------------------------------- /staticarrays/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0..3.20.1) 2 | PROJECT(staticarrays) 3 | 4 | SET(CMAKE_BUILD_TYPE Release) 5 | 6 | SET(CMAKE_CXX_STANDARD 20) 7 | SET(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | 9 | SET(SOURCES_CPP "src/main.cpp") 10 | 11 | ADD_EXECUTABLE(staticarrays ${SOURCES_CPP}) 12 | 13 | INSTALL(TARGETS staticarrays DESTINATION /usr/local/bin) 14 | -------------------------------------------------------------------------------- /staticarrays/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dendisuhubdy/cpp_exercises/54451303c98258853c51aad13d02e57d90d5f6d4/staticarrays/README.md -------------------------------------------------------------------------------- /staticarrays/build.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | mkdir -p build 3 | cd build 4 | cmake .. 5 | make -j2 all 6 | ./staticarrays 7 | cd .. 8 | -------------------------------------------------------------------------------- /staticarrays/clean.sh: -------------------------------------------------------------------------------- 1 | rm -rf build 2 | -------------------------------------------------------------------------------- /staticarrays/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | // Type aliasing 8 | using Emails = std::string; 9 | using Price = std::uint64_t; 10 | 11 | // ADL argument Deduction lookup 12 | 13 | auto foo() -> void { 14 | // R, G, B 15 | std::array staticArr { 1, 0, 1 }; 16 | // transitive include 17 | std::array colors{"RED", "GREEN", "BLUE"}; 18 | 19 | std::cout << colors[0] << "\n"; 20 | 21 | std::set emails; 22 | auto i = emails.insert("sdmg15@bitwyre.com"); 23 | auto v = emails.insert("sdmg15@bitwyre.com"); 24 | 25 | std::set intSet; 26 | intSet.insert(1); 27 | intSet.insert(1); 28 | intSet.insert(1); 29 | 30 | std::cout << intSet.size() << "\n"; 31 | 32 | std::cout << (i.first == v.first) << "\n"; 33 | } 34 | 35 | auto main() -> int { 36 | foo(); 37 | } 38 | -------------------------------------------------------------------------------- /strings/README.md: -------------------------------------------------------------------------------- 1 | “C++ provides a built-in string class. Although you may still use C-style strings of character arrays, the C++ string class is superior in almost every way. It handles the memory management; provides some bounds checking, assignment semantics, and comparisons; and supports manipulations such as concatenation, substring extraction, and substring or character replacement.” 2 | 3 | Excerpt From: Marc Gregoire. “Professional C++.” Apple Books. 4 | -------------------------------------------------------------------------------- /time/README.md: -------------------------------------------------------------------------------- 1 | # Time Utilites C++ 2 | 3 | header 4 | -------------------------------------------------------------------------------- /tuples/README.md: -------------------------------------------------------------------------------- 1 | “Tuples are sequences with a fixed size that can have heterogeneous elements and are defined in the header file. All standard template library containers discussed further in this chapter store homogenous elements, meaning that all the elements in a container must have the same type. A tuple allows you to store elements of completely unrelated types in one object. ” 2 | 3 | Excerpt From: Marc Gregoire. “Professional C++.” Apple Books. 4 | --------------------------------------------------------------------------------