├── src ├── arrays │ ├── text.html │ ├── dangerous_main.cc │ └── BUILD ├── lib │ ├── inheritance │ │ └── main.cc │ ├── segment_tree │ │ ├── segment_tree.cc │ │ └── BUILD │ ├── consonant_vowel_imbalance │ │ ├── consonant_vowel_imbalance.cc │ │ └── BUILD │ ├── solution.cc │ ├── sort │ │ └── BUILD │ ├── search │ │ ├── BUILD │ │ └── search.h │ ├── backtracking │ │ └── BUILD │ ├── dynamic_programming │ │ ├── BUILD │ │ └── dynamic_programming.h │ ├── vector_union.h │ ├── bst.h │ ├── vector_union.cc │ ├── graph │ │ ├── adjacency_matrix.h │ │ ├── vector_v_vector_pair_e.h │ │ ├── adjacency_vector_of_set.h │ │ ├── adjacency_vector_of_set.cc │ │ ├── pointer.h │ │ └── vector_v_vector_pair_e.cc │ ├── solution.h │ ├── factory │ │ ├── img_create.h │ │ └── img_create_map.h │ ├── bst.cc │ ├── heap.h │ └── tree.h ├── string_view │ ├── output │ │ └── string_view_benchmark.csv │ └── BUILD ├── benchmark │ ├── lib.h │ ├── lib.cc │ ├── output │ │ ├── plus_equal.csv │ │ ├── search_collection.csv │ │ ├── split_string.csv │ │ └── insert_into_collection.csv │ └── variable_in_loop.cc ├── deep_c │ ├── 33.cc │ ├── 245.cc │ ├── main.cc │ ├── 379.cc │ ├── BUILD │ └── 388.cc ├── stack │ ├── BUILD │ └── main.cc ├── cpp17 │ ├── Ex01 │ │ └── BUILD │ ├── file_system3.cc │ ├── if_constexpr.cc │ ├── file_system4.cc │ ├── no_discard.cc │ ├── template_auto.cc │ ├── if_init.cc │ └── memory_resources.cc ├── cpp20 │ ├── BUILD │ └── coroutine.cc ├── tbb │ └── BUILD ├── preprocessor │ ├── hazf.h │ ├── BUILD │ ├── xmacro.cc │ └── tokens.h ├── explicit │ ├── BUILD │ └── main.cc ├── tuple │ ├── BUILD │ └── main.cc ├── enum │ └── BUILD ├── hello_world │ ├── BUILD │ └── main.cc ├── nullptr │ ├── BUILD │ └── main.cc ├── queue │ └── BUILD ├── string │ └── BUILD ├── linked_list │ ├── BUILD │ └── main.cc ├── patterns │ ├── proxy │ │ ├── BUILD │ │ └── main.cc │ ├── adapter │ │ └── BUILD │ ├── mediator │ │ ├── BUILD │ │ └── main.cc │ ├── memento │ │ └── BUILD │ ├── observer │ │ └── BUILD │ ├── singleton │ │ ├── BUILD │ │ └── main.cc │ └── factory │ │ └── BUILD ├── override │ ├── BUILD │ └── main.cc ├── priority_queue │ ├── BUILD │ └── main.cc ├── sort │ ├── BUILD │ └── main.cc ├── uniform_initialization │ ├── BUILD │ ├── initializer_list │ │ ├── BUILD │ │ ├── explicit_constructor_vs_cast.cc │ │ ├── variable_arguments.cc │ │ └── explicit_constructor.cc │ ├── main.cc │ └── aggregate_initialization.cc ├── vector │ └── BUILD ├── list │ └── BUILD ├── pointers │ ├── pointer_to_void │ │ ├── BUILD │ │ └── main.cc │ ├── void_pointer │ │ └── BUILD │ ├── set_new_handler.cc │ ├── deep_copy_main.cc │ ├── shallow_copy_main.cc │ ├── BUILD │ ├── unsafe_pointers_main.cc │ └── heap_main.cc ├── moving_average │ └── BUILD ├── heap │ └── BUILD ├── const │ ├── main.cc │ ├── main copy.cc │ ├── mutable.cc │ ├── BUILD │ └── const_functions.cc ├── questions │ └── sharks │ │ └── BUILD ├── templates │ ├── factorial.cc │ ├── create_op.cc │ ├── member_templates.cc │ ├── meta_programming.cc │ ├── non_type.cc │ ├── variadic_with_init_list.cc │ ├── numbers.cc │ ├── parameter_independant.cc │ ├── fibonacci.cc │ ├── enable_if.cc │ ├── static_assert.cc │ ├── is_same.cc │ ├── traits.cc │ ├── is_char.cc │ ├── non_member_functions_inside_templates.cc │ ├── specialized.cc │ └── alias_template.cc ├── decltype │ ├── BUILD │ └── find_deduced_type.cc ├── move │ ├── swap.cc │ ├── copy_ellision_main.cc │ ├── move_so_main.cc │ ├── copy_elision.cc │ ├── perfect_forwarding.cc │ ├── std_forward.cc │ └── copy_assignment.cc ├── typeid │ ├── BUILD │ ├── 2.cc │ └── main.cc ├── main │ ├── BUILD │ ├── main.cc │ └── bishop_moves.cc ├── smart_pointer │ ├── pimple │ │ ├── main.cc │ │ ├── BUILD │ │ ├── widget.h │ │ └── widget.cc │ ├── stl_unique_main.cc │ ├── BUILD │ └── shared_ptr_costum_delete copy.cc ├── stl_algorithm │ └── BUILD ├── result_of │ ├── BUILD │ └── invoke.cc ├── amortization │ ├── BUILD │ └── binary_counter_main.cc ├── set_examples │ ├── BUILD │ ├── vector_union_main.cc │ └── set_insert_n_items_main.cc ├── graph │ ├── shortest_path │ │ └── BUILD │ ├── pointer_main.cc │ ├── vector_v_vector_pair_e_main.cc │ ├── adjacency_vector_of_set_main.cc │ ├── set_v_set_pair_e_main.cc │ ├── adjacency_matrix_main.cc │ ├── map_main.cc │ ├── set_of_set.cc │ ├── vector_of_set.cc │ └── unordered_set_of_unordered_set.cc ├── opreators │ ├── bool_main.cc │ ├── BUILD │ └── float_main.cc ├── random │ ├── rand_main.cc │ ├── uniform_bind_main.cc │ ├── uniform_main.cc │ ├── uniform_float_main.cc │ └── BUILD ├── cast │ ├── BUILD │ └── static_cast_constructor.cc ├── scope │ ├── namespace_unnamed_main.cc │ ├── BUILD │ ├── namespace_nested_main.cc │ ├── namespace_extend_main.cc │ └── namespace_main.cc ├── cpp14 │ ├── deprecated.cc │ ├── BUILD │ ├── return_type_deduction.cc │ ├── lambda_capture_init.cc │ ├── generic_lambda.cc │ └── other.cc ├── variables │ ├── BUILD │ ├── float_main.cc │ └── main.cc ├── constructor ├── tree │ ├── main.cc │ ├── BUILD │ └── std_any_main.cc ├── references │ ├── pointer_vs_reference_main.cc │ ├── main.cc │ ├── BUILD │ └── references_in_loops_main.cc ├── exceptions │ ├── stack_unwinding2.cc │ ├── std_exception.cc │ └── BUILD ├── control │ ├── switch_main.cc │ ├── BUILD │ ├── if_main.cc │ └── while_main.cc ├── memory_allocator │ ├── main3.cc │ ├── std_allocator1.cc │ ├── global_new_delete.cc │ ├── override_new_delete2.cc │ ├── BUILD │ └── operator_new_delete.cc ├── inheritance │ ├── base_pointer.cc │ ├── final.cc │ ├── scope.cc │ ├── override.cc │ ├── hiding.cc │ ├── BUILD │ ├── virtual.cc │ ├── pure_virtual_function.cc │ ├── private_inheritance.cc │ └── call_virtual_during_ctr copy.cc ├── function │ ├── generate_main.cc │ ├── inline.cc │ ├── all_off_main.cc │ ├── advanced_transform_main.cc │ ├── adjacent_diff_main.cc │ ├── std_function.cc │ ├── transform.cc │ ├── copy_if_main.cc │ ├── lambda_event_class.cc │ ├── main.cc │ ├── lambda_main.cc │ └── filter.cc ├── dynamic_programming │ ├── memoization.cc │ ├── fib_table_main.cc │ ├── coin_tab_main.cc │ ├── coin_main.cc │ ├── knap_01_main.cc │ ├── fib_main.cc │ ├── lpss_main.cc │ ├── lscs_table_main.cc │ ├── fib_memo_main.cc │ ├── coin_memo_main.cc │ └── lscs_main.cc ├── iterator │ ├── istream_iterator.cc │ ├── BUILD │ └── main2.cc ├── passing_variables │ ├── BUILD │ ├── main.cc │ └── pass_by_reference_main.cc ├── class │ ├── struct.cc │ ├── brackets.cc │ └── constructor_implicit_default_deleted.cc ├── hash │ ├── main.cc │ ├── std_hash_main.cc │ ├── set_main.cc │ ├── BUILD │ └── index_function_main.cc ├── bst │ ├── BUILD │ └── main.cc ├── multi_threading │ ├── main.cc │ ├── main_vector.cc │ ├── vector_of_threads_async_main.cc │ ├── parallel_fib.cc │ ├── vector_of_threads_main.cc │ ├── thread_functor.cc │ └── vector_of_threads_lambda_main.cc ├── modifiers │ ├── BUILD │ ├── constants_main.cc │ └── sizeof_examples_main.cc └── command_line │ └── BUILD ├── tree.png ├── .gitignore ├── tests ├── heap_test │ └── BUILD ├── segment_tree │ └── BUILD ├── consonant_vowel_imbalance │ └── BUILD ├── dynamic_programming │ ├── BUILD │ ├── lpss_test_table.cc │ ├── lpss_test_memo.cc │ └── lpss_test.cc ├── graph │ ├── adjacency_matrix_test.cc │ ├── adjacency_vector_of_set_test.cc │ └── vector_v_vector_pair_e_test.cc └── vector_union_test.cc ├── .vscode ├── c_cpp_properties.json └── extensions.json ├── third_party └── usr_local.BUILD ├── WORKSPACE ├── cpp-template └── README.md ├── README.md └── .bazelrc /src/arrays/text.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/lib/inheritance/main.cc: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/string_view/output/string_view_benchmark.csv: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/benchmark/lib.h: -------------------------------------------------------------------------------- 1 | inline void MyInlineFunction() ; 2 | -------------------------------------------------------------------------------- /src/lib/segment_tree/segment_tree.cc: -------------------------------------------------------------------------------- 1 | #include "segment_tree.h" -------------------------------------------------------------------------------- /src/benchmark/lib.cc: -------------------------------------------------------------------------------- 1 | inline void MyInlineFunction() { 2 | 3 | } -------------------------------------------------------------------------------- /tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ourarash/cpp_tour/HEAD/tree.png -------------------------------------------------------------------------------- /src/lib/consonant_vowel_imbalance/consonant_vowel_imbalance.cc: -------------------------------------------------------------------------------- 1 | #include "consonant_vowel_imbalance.h" -------------------------------------------------------------------------------- /src/deep_c/33.cc: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) { 3 | int a = 42; 4 | printf("%d\n", a); 5 | } -------------------------------------------------------------------------------- /src/stack/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | ) 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /bazel-Bazel_with_GTest 2 | /bazel-bin 3 | /bazel-genfiles 4 | /bazel-out 5 | /bazel-testlogs 6 | /bazel-template 7 | /bazel-* 8 | -------------------------------------------------------------------------------- /src/cpp17/Ex01/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/lib/solution.cc: -------------------------------------------------------------------------------- 1 | #include "solution.h" 2 | 3 | std::string Solution::PrintHelloWorld() { 4 | return "**** Hello World ****"; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /src/cpp20/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "coroutine", 5 | srcs = ["coroutine.cc"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/tbb/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = [""], 6 | ) -------------------------------------------------------------------------------- /src/deep_c/245.cc: -------------------------------------------------------------------------------- 1 | #include 2 | struct X { 3 | int a; 4 | char b; 5 | int c; 6 | }; 7 | int main(void) { std::cout << sizeof(X) << std::endl; } -------------------------------------------------------------------------------- /src/preprocessor/hazf.h: -------------------------------------------------------------------------------- 1 | // #ifndef _MYFILE_H_ 2 | // #define _MYFILE_H_ 3 | 4 | // // stuff here 5 | 6 | // #endif // _MYFILE_H_ 7 | 8 | #pragma once 9 | -------------------------------------------------------------------------------- /src/explicit/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = [], 6 | ) 7 | 8 | -------------------------------------------------------------------------------- /src/lib/sort/BUILD: -------------------------------------------------------------------------------- 1 | cc_library( 2 | name = "SortLib", 3 | srcs = ["sort.cc"], 4 | hdrs = ["sort.h"], 5 | visibility = ["//visibility:public"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/tuple/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:utility"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/enum/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/hello_world/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) -------------------------------------------------------------------------------- /src/lib/search/BUILD: -------------------------------------------------------------------------------- 1 | cc_library( 2 | name = "SearchLib", 3 | srcs = ["search.cc"], 4 | hdrs = ["search.h"], 5 | visibility = ["//visibility:public"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/nullptr/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) -------------------------------------------------------------------------------- /src/queue/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/string/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/linked_list/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/patterns/proxy/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:utility"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/override/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = ["//src/lib:SolutionLib"], 7 | ) 8 | -------------------------------------------------------------------------------- /src/patterns/adapter/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:utility"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/patterns/mediator/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:utility"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/patterns/memento/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:utility"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/patterns/observer/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:utility"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/patterns/singleton/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:utility"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/priority_queue/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | # deps = ["//src/lib:SolutionLib"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/sort/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib", "//src/lib/sort:SortLib"], 6 | ) 7 | -------------------------------------------------------------------------------- /src/uniform_initialization/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = ["//src/lib:SolutionLib"], 7 | ) 8 | -------------------------------------------------------------------------------- /src/vector/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib", "//src/lib:utility",], 6 | ) 7 | -------------------------------------------------------------------------------- /src/list/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib" , "//src/lib:utility", 6 | ], 7 | ) 8 | -------------------------------------------------------------------------------- /src/pointers/pointer_to_void/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib", "//src/lib:utility",], 6 | ) 7 | -------------------------------------------------------------------------------- /src/pointers/void_pointer/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib", "//src/lib:utility",], 6 | ) 7 | -------------------------------------------------------------------------------- /src/moving_average/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = [ 7 | "//src/lib:utility", 8 | ], 9 | ) 10 | -------------------------------------------------------------------------------- /src/heap/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = ["//src/lib:heap_lib", "//src/lib:utility"], 7 | 8 | 9 | ) 10 | -------------------------------------------------------------------------------- /src/hello_world/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | // My first C++ program 5 | 6 | int main() { 7 | std::cout << "**** Hello world!" << std::endl; 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /src/const/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | const int i = 1; 7 | const int j = i + 1; // Initializing is ok 8 | 9 | // i++; // Don't change the const! 10 | } -------------------------------------------------------------------------------- /src/benchmark/output/plus_equal.csv: -------------------------------------------------------------------------------- 1 | name,iterations,real_time,cpu_time,time_unit,bytes_per_second,items_per_second,label,error_occurred,error_message 2 | "BM_Plus",263612775,2.62297,2.59063,ns,,,,, 3 | "BM_PlusEqual",270773135,2.59264,2.56477,ns,,,,, 4 | -------------------------------------------------------------------------------- /src/questions/sharks/BUILD: -------------------------------------------------------------------------------- 1 | cc_test( 2 | name = "sharks", 3 | srcs = ["sharks.cc"], 4 | copts = ["--std=c++17"], 5 | deps = [ 6 | "//src/lib:utility", 7 | "@com_google_googletest//:gtest_main", 8 | ], 9 | ) 10 | -------------------------------------------------------------------------------- /tests/heap_test/BUILD: -------------------------------------------------------------------------------- 1 | cc_test( 2 | name = "heap_test", 3 | srcs = ["heap_test.cc"], 4 | deps = [ 5 | "//src/lib:utility", 6 | "//src/lib:heap_lib", 7 | "@com_google_googletest//:gtest_main", 8 | ], 9 | ) 10 | -------------------------------------------------------------------------------- /src/deep_c/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | struct X { 3 | int a; 4 | char b; 5 | int c; 6 | char* d; 7 | }; 8 | int main(void) { 9 | printf("%zu\n", sizeof(int)); 10 | printf("%zu\n", sizeof(char)); 11 | printf("%zu\n", sizeof(struct X)); 12 | } -------------------------------------------------------------------------------- /src/const/main copy.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | const int i = 1; 7 | const int j = i + 1; // Initializing is ok 8 | 9 | 10 | 11 | i++; // Don't change the const! 12 | } -------------------------------------------------------------------------------- /src/templates/factorial.cc: -------------------------------------------------------------------------------- 1 | #include 2 | template 3 | struct factorial { 4 | enum { value = n * factorial::value }; 5 | }; 6 | 7 | template <> 8 | struct factorial<0> { 9 | enum { value = 1 }; 10 | }; 11 | 12 | int main() { return 0; } 13 | -------------------------------------------------------------------------------- /src/decltype/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib", "//src/lib:utility",], 6 | ) 7 | cc_binary( 8 | name = "find_deduced_type", 9 | srcs = ["find_deduced_type.cc"], 10 | ) -------------------------------------------------------------------------------- /src/lib/backtracking/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_library") 2 | 3 | cc_library( 4 | name = "BacktrackingLib", 5 | srcs = ["backtracking.cc"], 6 | hdrs = ["backtracking.h"], 7 | visibility = ["//visibility:public"], 8 | deps = ["//src/lib:utility"], 9 | ) 10 | -------------------------------------------------------------------------------- /src/lib/segment_tree/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_library") 2 | 3 | cc_library( 4 | name = "SegmentTreeLib", 5 | srcs = ["segment_tree.cc"], 6 | hdrs = ["segment_tree.h"], 7 | visibility = ["//visibility:public"], 8 | deps = ["//src/lib:utility"], 9 | ) 10 | -------------------------------------------------------------------------------- /src/move/swap.cc: -------------------------------------------------------------------------------- 1 | #include 2 | template 3 | void SwapMove(T& a, T& b) { 4 | T t(std::move(a)); 5 | 6 | a = std::move(b); 7 | 8 | b = std::move(t); 9 | } 10 | 11 | template 12 | void Swap(T& a, T& b) { 13 | T t(a); 14 | a = b; 15 | b = t; 16 | } -------------------------------------------------------------------------------- /src/sort/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "src/lib/sort/sort.h" 5 | #include "src/lib/utility.h" 6 | 7 | int main() { 8 | std::vector in = {5, 3, 1, 77, -1, 100}; 9 | Sort::QuickSort(in); 10 | Print(in); 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /src/typeid/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "2", 10 | srcs = ["2.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | ) 13 | 14 | -------------------------------------------------------------------------------- /tests/segment_tree/BUILD: -------------------------------------------------------------------------------- 1 | 2 | cc_test( 3 | name = "segment_tree_test", 4 | srcs = ["segment_tree_test.cc"], 5 | deps = [ 6 | "//src/lib/segment_tree:SegmentTreeLib", 7 | "@com_google_googletest//:gtest_main", 8 | "//src/lib:utility", 9 | 10 | ], 11 | ) -------------------------------------------------------------------------------- /src/main/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "bishop_moves", 10 | srcs = ["bishop_moves.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | 13 | ) -------------------------------------------------------------------------------- /src/smart_pointer/pimple/main.cc: -------------------------------------------------------------------------------- 1 | 2 | //----------------------------------------------------------------------------- 3 | #include "widget.h" 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | int main(int argc, char const *argv[]) { 10 | Widget w; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /src/stl_algorithm/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:utility"], 6 | ) 7 | 8 | cc_binary( 9 | name = "simple_algorithms", 10 | srcs = ["simple_algorithms.cc"], 11 | deps = ["//src/lib:utility"], 12 | ) 13 | -------------------------------------------------------------------------------- /src/lib/dynamic_programming/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_library") 2 | 3 | cc_library( 4 | name = "DynamicProgrammingLib", 5 | srcs = ["dynamic_programming.cc"], 6 | hdrs = ["dynamic_programming.h"], 7 | visibility = ["//visibility:public"], 8 | deps = ["//src/lib:utility"], 9 | ) 10 | -------------------------------------------------------------------------------- /src/result_of/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = [ 7 | "//src/lib:SolutionLib", 8 | "//src/lib:utility", 9 | ], 10 | ) 11 | 12 | cc_binary( 13 | name = "invoke", 14 | srcs = ["invoke.cc"], 15 | ) -------------------------------------------------------------------------------- /src/patterns/mediator/main.cc: -------------------------------------------------------------------------------- 1 | class Button {}; 2 | class ListBox {}; 3 | class EntryField {}; 4 | class Message {}; 5 | 6 | class FontDialog { 7 | public: 8 | void OnMessage(Message* msg); 9 | 10 | private: 11 | Button* mOk; 12 | Button* mCancel; 13 | ListBox* mFontList; 14 | EntryField* mFontName; 15 | }; 16 | -------------------------------------------------------------------------------- /src/lib/vector_union.h: -------------------------------------------------------------------------------- 1 | #ifndef VECTOR_UNION_SOLUTION_H 2 | #define VECTOR_UNION_SOLUTION_H 3 | 4 | #include 5 | #include 6 | 7 | class VectorUnionSolution { 8 | public: 9 | std::vector Calculate(const std::vector &a, 10 | const std::vector &b); 11 | }; 12 | 13 | #endif -------------------------------------------------------------------------------- /src/amortization/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "binary_counter_main", 10 | srcs = ["binary_counter_main.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | ) 13 | 14 | -------------------------------------------------------------------------------- /src/lib/consonant_vowel_imbalance/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_library") 2 | 3 | cc_library( 4 | name = "ConsonantVowelImbalanceLib", 5 | srcs = ["consonant_vowel_imbalance.cc"], 6 | hdrs = ["consonant_vowel_imbalance.h"], 7 | visibility = ["//visibility:public"], 8 | deps = ["//src/lib:utility"], 9 | ) 10 | -------------------------------------------------------------------------------- /src/patterns/factory/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main_factory", 4 | srcs = ["main_factory.cc"], 5 | deps = ["//src/lib:utility"], 6 | ) 7 | 8 | 9 | cc_binary( 10 | name = "main_no_factory", 11 | srcs = ["main_no_factory.cc"], 12 | deps = ["//src/lib:utility"], 13 | ) 14 | -------------------------------------------------------------------------------- /src/set_examples/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | 9 | cc_binary( 10 | name = "vector_union_main", 11 | srcs = ["vector_union_main.cc"], 12 | deps = ["//src/lib:SolutionLib"], 13 | ) 14 | 15 | -------------------------------------------------------------------------------- /src/graph/shortest_path/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "distance_matrix_main", 10 | srcs = ["distance_matrix_main.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | ) 13 | 14 | -------------------------------------------------------------------------------- /src/lib/bst.h: -------------------------------------------------------------------------------- 1 | #ifndef BST_TREE_H 2 | #define BST_TREE_H 3 | 4 | #include "tree.h" 5 | 6 | class BST : public Tree { 7 | public: 8 | BST() { root_ = nullptr; } 9 | bool empty(); 10 | int size(); 11 | 12 | void insert(TreeNode *&root, int v); 13 | TreeNode* search(TreeNode *root, int v); 14 | 15 | TreeNode *root_; 16 | }; 17 | 18 | #endif -------------------------------------------------------------------------------- /tests/consonant_vowel_imbalance/BUILD: -------------------------------------------------------------------------------- 1 | cc_test( 2 | name = "consonant_vowel_imbalance_test", 3 | srcs = ["consonant_vowel_imbalance_test.cc"], 4 | deps = [ 5 | "//src/lib:utility", 6 | "//src/lib/consonant_vowel_imbalance:ConsonantVowelImbalanceLib", 7 | "@com_google_googletest//:gtest_main", 8 | ], 9 | ) 10 | -------------------------------------------------------------------------------- /src/opreators/bool_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | 5 | int main() { 6 | 7 | bool t = true; 8 | bool f = false; 9 | bool c = (1 == 5); 10 | 11 | std::cout << "t: " << t << std::endl; 12 | std::cout << "f: " << f << std::endl; 13 | std::cout << "c: " << c << std::endl; 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /src/preprocessor/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = [], 6 | ) 7 | 8 | cc_binary( 9 | name = "constexpr", 10 | srcs = ["constexpr.cc"], 11 | deps = [], 12 | ) 13 | cc_binary( 14 | name = "constexpr_class", 15 | srcs = ["constexpr_class.cc"], 16 | ) 17 | 18 | -------------------------------------------------------------------------------- /src/deep_c/379.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct A { 4 | A() { puts("A()"); } 5 | A(int v) { puts("A(int)"); } 6 | ~A() { puts("~A()"); } 7 | }; 8 | struct X { 9 | X(int v) { a = v; } 10 | X(long v) : a(v) {} 11 | A a; 12 | }; 13 | int main() { 14 | puts("bad style"); 15 | { X slow(int(2)); } 16 | puts("good style"); 17 | { X fast(long(2)); } 18 | } -------------------------------------------------------------------------------- /src/uniform_initialization/initializer_list/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = ["//src/lib:SolutionLib"], 7 | ) 8 | 9 | 10 | cc_binary( 11 | name = "explicit_constructor", 12 | srcs = ["explicit_constructor.cc"], 13 | deps = ["//src/lib:SolutionLib"], 14 | ) 15 | -------------------------------------------------------------------------------- /src/random/rand_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "src/lib/solution.h" 5 | 6 | int main() { 7 | srand(2); 8 | 9 | // Generate random number between 1 and 10 10 | for (size_t i = 0; i < 10; i++) { 11 | auto random_number = rand() % 10 + 1; 12 | std::cout << "random_number: " << random_number << std::endl; 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /src/arrays/dangerous_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | 5 | struct Person { 6 | std::string first_name; 7 | std::string last_name; 8 | int age; 9 | }; 10 | 11 | int main() { 12 | 13 | // Index out of bound 14 | int arr[4]; 15 | 16 | for (int i = 0; i < 10; i++) { 17 | arr[i] = 0; 18 | } 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /src/cpp17/file_system3.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | namespace fs = std::filesystem; 6 | 7 | int main() { 8 | fs::path p5 = "/usr/a/b/c"; 9 | fs::path p6 = "/usr/a/b/c"; 10 | assert(p5 == p6); 11 | assert(p5.compare(p6) == 0); 12 | 13 | p5 = "/usr/a/b/c"; 14 | p6 = "/usr/a/b/c/d"; 15 | assert(p5 < p6); 16 | assert(p5.compare(p6) < 0); 17 | } -------------------------------------------------------------------------------- /src/graph/pointer_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/graph/pointer.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() { 10 | PointerGraph pg; 11 | pg.CreateSampleGraph1(); 12 | bool result = pg.HasCycle(); 13 | std::cout << "result: " << result << std::endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /src/nullptr/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void func(int) { std::cout << "int" << std::endl; } 4 | void func(int *) { std::cout << "pointer" << std::endl; } 5 | void func(bool) { std::cout << "bool" << std::endl; } 6 | // void func(long *) { std::cout << "pointer" << std::endl; } 7 | 8 | int main() { 9 | func(nullptr); 10 | // func(NULL); 11 | 12 | return 0; // End of a line 13 | } -------------------------------------------------------------------------------- /src/cast/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = ["//src/lib:SolutionLib"], 7 | ) 8 | 9 | 10 | cc_binary( 11 | name = "static_cast_constructor", 12 | srcs = ["static_cast_constructor.cc"], 13 | ) 14 | 15 | cc_binary( 16 | name = "static_cast_inheritance", 17 | srcs = ["static_cast_inheritance.cc"], 18 | ) -------------------------------------------------------------------------------- /src/scope/namespace_unnamed_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | // namespaces 6 | #include 7 | int x = 0; 8 | 9 | namespace { 10 | void PrintSomething() { std::cout << "Printing example 2." << std::endl; } 11 | 12 | }; // namespace ns1 13 | 14 | int main() { 15 | 16 | PrintSomething(); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Mac", 5 | "includePath": [ 6 | "${workspaceFolder}/src/lib", 7 | "${workspaceFolder}/src/lib/*", 8 | "/usr/local/include", 9 | "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/" 10 | ] 11 | } 12 | ], 13 | "version": 4 14 | } -------------------------------------------------------------------------------- /src/set_examples/vector_union_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include "src/lib/vector_union.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | // Find union of two vectors 10 | int main() { 11 | VectorUnionSolution s; 12 | std::vector a = {1, 2, 3}, b = {3, 54, 6}; 13 | auto r = s.Calculate(a, b); 14 | PrintVector(r); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /src/cpp17/if_constexpr.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char const *argv[]) { 7 | constexpr int a = 1, b = 1; 8 | if constexpr (a == b) { 9 | std::cout << "a: " << a << ", b: " << b << std::endl; 10 | } else if constexpr (a != b) { 11 | std::cout << "a: " << a << ", b: " << b << std::endl; 12 | } 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /src/cpp14/deprecated.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | // Warning: 'old_and_busted' is deprecated 6 | class [[deprecated]] old_and_busted1{}; 7 | 8 | // Warning: 'old_and_busted' is deprecated: use new_hotness 9 | class [[deprecated("use new_hotness")]] old_and_busted2{}; 10 | 11 | int main() { 12 | old_and_busted1 a; 13 | old_and_busted2 b; 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /src/smart_pointer/pimple/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = [":pimple"] 7 | ) 8 | 9 | cc_library( 10 | name = "pimple", 11 | srcs = [ 12 | "widget.cc", 13 | ], 14 | hdrs = [ 15 | "widget.h", 16 | ], 17 | # includes = ["/Users/ari/github/cpp_tour/src/smart_pointer/pimple/widget.h"], 18 | ) 19 | -------------------------------------------------------------------------------- /src/lib/search/search.h: -------------------------------------------------------------------------------- 1 | #ifndef SORT_H 2 | #define SORT_H 3 | 4 | #include 5 | 6 | class Search { 7 | public: 8 | Search() {} 9 | static int BinarySearchRecursive(std::vector &nums, int n); 10 | static int BinarySearchRecursive_aux(std::vector &nums, int n, int l, 11 | int r); 12 | static int BinarySearchIterative(std::vector &nums, int n); 13 | }; 14 | 15 | #endif -------------------------------------------------------------------------------- /src/opreators/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "float_main", 10 | srcs = ["float_main.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | ) 13 | 14 | cc_binary( 15 | name = "bool_main", 16 | srcs = ["bool_main.cc"], 17 | deps = ["//src/lib:SolutionLib"], 18 | ) -------------------------------------------------------------------------------- /src/variables/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "float_main", 10 | srcs = ["float_main.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | ) 13 | 14 | cc_binary( 15 | name = "bool_main", 16 | srcs = ["bool_main.cc"], 17 | deps = ["//src/lib:SolutionLib"], 18 | ) -------------------------------------------------------------------------------- /src/cpp17/file_system4.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | namespace fs = std::filesystem; 4 | int main() { 5 | fs::path p = fs::path(".") / "src" / ".." / "src"; 6 | std::cout << "Current path is " << fs::current_path() << '\n' 7 | << "Canonical path for " << p << " is " << fs::canonical(p) << '\n' 8 | << "weakly Canonical path for " << p << " is " 9 | << fs::weakly_canonical(p) << '\n'; 10 | } -------------------------------------------------------------------------------- /src/main/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "src/lib/solution.h" 5 | 6 | int main() { 7 | Solution solution; 8 | 9 | std::vector inputs = {1, 2, 3, 4, 5}; 10 | int max = solution.FindMax(inputs); 11 | std::cout << "max: " << max << std::endl; 12 | 13 | inputs = {}; 14 | max = solution.FindMax(inputs); 15 | std::cout << "max: " << max << std::endl; 16 | 17 | return 0; // End of a line 18 | } 19 | 20 | -------------------------------------------------------------------------------- /src/pointers/set_new_handler.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | // function to call if operator new can't allocate enough memory 4 | void outOfMem() { 5 | std::cout << "Unable to satisfy request for memory\n"; 6 | std::abort(); 7 | } 8 | int main() { 9 | std::set_new_handler(outOfMem); 10 | int *pBigDataArray = new int[100000000000000L]; 11 | pBigDataArray[5]=2; 12 | std::cout << "sizeof: " << sizeof(pBigDataArray) << std::endl; 13 | } -------------------------------------------------------------------------------- /src/smart_pointer/pimple/widget.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Widget { // in "widget.h" 4 | public: 5 | Widget(); 6 | ~Widget(); // declaration only 7 | 8 | Widget(Widget&& rhs); // declarations 9 | Widget& operator=(Widget&& rhs); // only 10 | private: 11 | struct Impl; 12 | std::unique_ptr pImpl; // use smart pointer 13 | }; 14 | //----------------------------------------------------------------------------- 15 | -------------------------------------------------------------------------------- /src/deep_c/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = ["//src/lib:SolutionLib"], 7 | ) 8 | 9 | cc_binary( 10 | name = "245", 11 | srcs = ["245.cc"], 12 | ) 13 | 14 | cc_binary( 15 | name = "379", 16 | srcs = ["379.cc"], 17 | ) 18 | cc_binary( 19 | name = "388", 20 | srcs = ["388.cc"], 21 | ) 22 | 23 | cc_binary( 24 | name = "33", 25 | srcs = ["33.cc"], 26 | ) -------------------------------------------------------------------------------- /src/set_examples/set_insert_n_items_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include "src/lib/vector_union.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | std::set InsertInSet(const std::vector &v) { 10 | std::set s; 11 | 12 | for (const &n : v) { 13 | s.insert(n); 14 | } 15 | } 16 | 17 | int main() { 18 | 19 | std::vector v = {1, 2, 3, -1, 33, -5, 1, 5}; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /src/scope/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "namespace_main", 10 | srcs = ["namespace_main.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | ) 13 | 14 | cc_binary( 15 | name = "const_class_main", 16 | srcs = ["const_class_main.cc"], 17 | deps = ["//src/lib:SolutionLib"], 18 | ) 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/constructor/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "const_param_main", 10 | srcs = ["const_param_main.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | ) 13 | 14 | cc_binary( 15 | name = "const_class_main", 16 | srcs = ["const_class_main.cc"], 17 | deps = ["//src/lib:SolutionLib"], 18 | ) 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/tree/main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/tree.h" 2 | #include "src/lib/utility.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() { 10 | 11 | Tree t; 12 | t.CreateSampleTree2(); 13 | std::cout << "InOrder:" << std::endl; 14 | t.InOrder(t.root_); 15 | std::cout << "PreOrder:" << std::endl; 16 | t.PreOrder(t.root_); 17 | std::cout << "PostOrder:" << std::endl; 18 | t.PostOrder(t.root_); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /src/references/pointer_vs_reference_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | int i = 10; 8 | int j = 20; 9 | 10 | int &k = i; 11 | // &k= j; // Error! 12 | std::cout << "k: " << k << std::endl; 13 | 14 | // Pointers can be reassigned 15 | int *p = &i; 16 | std::cout << "*p: " << *p << std::endl; 17 | 18 | p = &j; 19 | std::cout << "*p: " << *p << std::endl; 20 | 21 | 22 | } -------------------------------------------------------------------------------- /src/arrays/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = [ 7 | "//src/lib:utility", 8 | ], 9 | ) 10 | 11 | cc_binary( 12 | name = "dangerous_main", 13 | srcs = ["dangerous_main.cc"], 14 | deps = ["//src/lib:SolutionLib"], 15 | ) 16 | 17 | cc_binary( 18 | name = "dynamic_arrays_main", 19 | srcs = ["dynamic_arrays_main.cc"], 20 | deps = ["//src/lib:SolutionLib"], 21 | ) 22 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations. 3 | // Extension identifier format: ${publisher}.${name}. Example: vscode.csharp 4 | 5 | // List of extensions which should be recommended for users of this workspace. 6 | "recommendations": [ 7 | 8 | ], 9 | // List of extensions recommended by VS Code that should not be recommended for users of this workspace. 10 | "unwantedRecommendations": [ 11 | 12 | ] 13 | } -------------------------------------------------------------------------------- /src/override/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class A { 6 | public: 7 | virtual void TakeDamage(int amount) { std::cout << "A" << std::endl; } 8 | }; 9 | 10 | class B : public A { 11 | public: 12 | // void TakeDamage() { std::cout << "B" << std::endl; } 13 | 14 | // void TakeDamage() override { std::cout << "B" << std::endl; } 15 | }; 16 | 17 | int main(int argc, char const *argv[]) { 18 | A a; 19 | B b; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /src/preprocessor/xmacro.cc: -------------------------------------------------------------------------------- 1 | enum Tokens { 2 | #define TOKEN(a, b, c) a, 3 | #include "tokens.h" 4 | #undef TOKEN 5 | }; 6 | 7 | static const char* Names_data[] = { 8 | #define TOKEN(a, b, c) #a, 9 | #include "tokens.h" 10 | #undef TOKEN 11 | }; 12 | 13 | static const char* Values_data[] = { 14 | #define TOKEN(a, b, c) b, 15 | #include "tokens.h" 16 | #undef TOKEN 17 | }; 18 | 19 | static const int Lengths_data[] = { 20 | #define TOKEN(a, b, c) c, 21 | #include "tokens.h" 22 | #undef TOKEN 23 | }; 24 | -------------------------------------------------------------------------------- /src/random/uniform_bind_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "src/lib/solution.h" 6 | 7 | int main() { 8 | auto GenRandomValue = std::bind(std::uniform_int_distribution<>(low, high), 9 | std::default_random_engine()); 10 | 11 | for (size_t i = 0; i < 10; i++) { 12 | int index1 = myDist(randGen); 13 | 14 | std::cout << "index1: " << index1 << std::endl; 15 | } 16 | 17 | return 0; // End of a line 18 | } -------------------------------------------------------------------------------- /src/templates/create_op.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | struct Op { 7 | virtual const char* GetName() const = 0; 8 | virtual void Parse(const std::string& params) = 0; 9 | virtual void Execute(class Machine& machine) = 0; 10 | }; 11 | 12 | template 13 | std::shared_ptr CreateOp() { 14 | return std::make_shared(); 15 | } 16 | 17 | std::map()>> opMap; 18 | -------------------------------------------------------------------------------- /src/explicit/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | class Foo { 3 | public: 4 | // single parameter constructor, can be used as an implicit conversion 5 | Foo(int foo) : m_foo(foo) {} 6 | 7 | int GetFoo() { return m_foo; } 8 | 9 | private: 10 | int m_foo; 11 | }; 12 | 13 | int DoBar(Foo foo) { 14 | int i = foo.GetFoo(); 15 | return i; 16 | } 17 | 18 | //----------------------------------------------------- 19 | int main() { 20 | auto i = DoBar(42); 21 | std::cout << "i: " << i << std::endl; 22 | } -------------------------------------------------------------------------------- /src/lib/vector_union.cc: -------------------------------------------------------------------------------- 1 | #include "vector_union.h" 2 | #include 3 | std::vector VectorUnionSolution::Calculate(const std::vector &a, 4 | const std::vector &b) { 5 | std::vector r; 6 | std::set my_set; 7 | for (auto n : a) { 8 | my_set.insert(n); 9 | } 10 | 11 | for (auto n : b) { 12 | my_set.insert(n); 13 | } 14 | 15 | for (auto &n : my_set) { 16 | r.push_back(n); 17 | } 18 | 19 | return r; 20 | } 21 | -------------------------------------------------------------------------------- /src/random/uniform_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "src/lib/solution.h" 6 | 7 | int main() { 8 | // Pick a random value from [0, 10] 9 | auto seed = 1; 10 | 11 | std::mt19937 randGen(seed); 12 | std::uniform_int_distribution myDist(0, 10); 13 | 14 | for (size_t i = 0; i < 10; i++) { 15 | int index1 = myDist(randGen); 16 | 17 | std::cout << "index1: " << index1 << std::endl; 18 | } 19 | 20 | return 0; // End of a line 21 | } -------------------------------------------------------------------------------- /src/exceptions/stack_unwinding2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class AA {}; 5 | struct ExceptThis : public std::exception {}; 6 | void fb(); 7 | void fc(); 8 | 9 | void fa() { 10 | try { 11 | AA myAA; 12 | fb(); 13 | } catch (std::exception& e) { 14 | std::cout << "Uh oh\n"; 15 | } 16 | } 17 | void fb() { 18 | try { 19 | AA myAA2; 20 | fc(); 21 | } catch (const char* e) { 22 | std::cout << "Not good\n"; 23 | } 24 | } 25 | void fc() { throw ExceptThis(); } 26 | -------------------------------------------------------------------------------- /tests/dynamic_programming/BUILD: -------------------------------------------------------------------------------- 1 | # cc_test( 2 | # name = "lpss", 3 | # srcs = glob(["**/*.cc"]), 4 | # deps = [ 5 | # "//src/lib:dynamic_programming_lpss_lib", 6 | # "@com_google_googletest//:gtest_main", 7 | # ], 8 | # ) 9 | 10 | 11 | cc_test( 12 | name = "dynamic_programming_test", 13 | srcs = ["dynamic_programming_test.cc"], 14 | deps = [ 15 | "//src/lib/dynamic_programming:DynamicProgrammingLib", 16 | "@com_google_googletest//:gtest_main", 17 | ], 18 | ) -------------------------------------------------------------------------------- /src/graph/vector_v_vector_pair_e_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "src/lib/graph/vector_v_vector_pair_e.h" 8 | #include "src/lib/utility.h" 9 | 10 | int main() { 11 | std::vector v = {0, 1, 2, 3}; 12 | std::vector> e = {{1, 3}, {1, 3}, {3, 0}, {3, 0}, 13 | {0, 2}, {2, 1}, {2, 3}}; 14 | Graph g(v, e); 15 | std::cout << g.IsEulerWalkable() << std::endl; 16 | } 17 | -------------------------------------------------------------------------------- /src/preprocessor/tokens.h: -------------------------------------------------------------------------------- 1 | // Expression Operators 2 | TOKEN(Assign,"=",1) 3 | TOKEN(Plus,"+",1) 4 | TOKEN(Minus,"-",1) 5 | TOKEN(Mult,"*",1) 6 | TOKEN(Div,"/",1) 7 | TOKEN(Mod,"%",1) 8 | TOKEN(Inc,"++",2) 9 | TOKEN(Dec,"--",2) 10 | TOKEN(LBracket,"[",1) 11 | TOKEN(RBracket,"]",1) 12 | TOKEN(EqualTo,"==",2) 13 | TOKEN(NotEqual,"!=",2) 14 | TOKEN(Or,"||",2) 15 | TOKEN(And,"&&",2) 16 | TOKEN(Not,"!",1) 17 | TOKEN(LessThan,"<",1) 18 | TOKEN(GreaterThan,">",1) 19 | TOKEN(LParen,"(",1) 20 | TOKEN(RParen,")",1) 21 | TOKEN(Addr,"&",1) -------------------------------------------------------------------------------- /src/lib/graph/adjacency_matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef FC9DFAA4_708A_495F_A141_B69F7BCF9CAD 2 | #define FC9DFAA4_708A_495F_A141_B69F7BCF9CAD 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class Graph { 11 | public: 12 | Graph(std::vector> &adjacency) : adjacency_(adjacency) {} 13 | bool IsEulerWalkable(); 14 | void PrintGraph(); 15 | std::vector> adjacency_; 16 | }; 17 | 18 | #endif /* FC9DFAA4_708A_495F_A141_B69F7BCF9CAD */ 19 | -------------------------------------------------------------------------------- /src/random/uniform_float_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "src/lib/solution.h" 6 | 7 | int main(int argc, char** argv) { 8 | // Pick a random value from [0, 10] 9 | auto seed = 1; 10 | 11 | std::mt19937 randGen(seed); 12 | std::uniform_real_distribution<> myDist(0, 10); 13 | 14 | for (size_t i = 0; i < 10; i++) { 15 | auto index1 = myDist(randGen); 16 | 17 | std::cout << "index1: " << index1 << std::endl; 18 | } 19 | 20 | return 0; // End of a line 21 | } -------------------------------------------------------------------------------- /src/control/switch_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | 6 | int value; 7 | std::cout << "Please enter a value: " << std::endl; 8 | std::cin >> value; 9 | 10 | switch (value) { 11 | case 1: 12 | std::cout << "You entered 1!" << std::endl; 13 | break; 14 | 15 | case 2: 16 | case 3: 17 | std::cout << "You entered 2 or 3!" << std::endl; 18 | break; 19 | 20 | default: 21 | std::cout << "You entered an invalid number!" << std::endl; 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /src/exceptions/std_exception.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | class MyException : public virtual std::exception { 4 | // what is a virtual function which returns a description 5 | const char* what() const noexcept override { 6 | // throw std::exception(); 7 | return "MY EXCEPTION!"; 8 | } 9 | }; 10 | // later on... 11 | 12 | int main() { 13 | try { 14 | throw MyException(); 15 | } catch (std::exception& e) { 16 | std::cout << e.what() << std::endl; // "MY EXCEPTION!" 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /src/move/copy_ellision_main.cc: -------------------------------------------------------------------------------- 1 | // Shows the omission of copy constructor call 2 | // Run with bazel --copt="-fno-elide-constructors" to disable copy elision. 3 | 4 | #include 5 | 6 | class B { 7 | public: 8 | B(const char* str = "\0") // default constructor 9 | { 10 | std::cout << "Constructor called" << std::endl; 11 | } 12 | 13 | B(const B& b) // copy constructor 14 | { 15 | std::cout << "Copy constructor called" << std::endl; 16 | } 17 | }; 18 | 19 | int main() { 20 | B ob = "copy me"; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /src/cpp20/coroutine.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | generator generatorForNumbers(int begin, int inc = 1) { 5 | for (int i = begin;; i += inc) { 6 | co_yield i; 7 | } 8 | } 9 | 10 | int main() { 11 | std::cout << std::endl; 12 | 13 | const auto numbers = generatorForNumbers(-10); // (2) 14 | 15 | for (int i = 1; i <= 20; ++i) std::cout << numbers << " "; // (4) 16 | 17 | std::cout << "\n\n"; 18 | 19 | for (auto n : generatorForNumbers(0, 5)) std::cout << n << " "; // (3) 20 | 21 | std::cout << "\n\n"; 22 | } -------------------------------------------------------------------------------- /src/memory_allocator/main3.cc: -------------------------------------------------------------------------------- 1 | #include 2 | // sized class-specific deallocation functions 3 | struct X { 4 | static void operator delete(void* ptr, std::size_t sz) { 5 | std::cout << "custom delete for size " << sz << '\n'; 6 | ::operator delete(ptr); 7 | } 8 | static void operator delete[](void* ptr, std::size_t sz) { 9 | std::cout << "custom delete for size " << sz << '\n'; 10 | ::operator delete(ptr); 11 | } 12 | }; 13 | int main() { 14 | X* p1 = new X; 15 | delete p1; 16 | X* p2 = new X[10]; 17 | delete[] p2; 18 | } -------------------------------------------------------------------------------- /src/deep_c/388.cc: -------------------------------------------------------------------------------- 1 | #include 2 | struct base { 3 | virtual void f(){std::cout << "base f" << std::endl;} 4 | virtual void g(){std::cout << "base g" << std::endl;} 5 | int a, b; 6 | }; 7 | 8 | struct derived : base { 9 | virtual void g(){std::cout << "derrived g" << std::endl;} 10 | virtual void h(){std::cout << "derrived h" << std::endl;} 11 | int c; 12 | }; 13 | 14 | void poly(base *ptr) { 15 | ptr->f(); 16 | ptr->g(); 17 | } 18 | 19 | int main() { 20 | base *b = new base(); 21 | base *d = new derived(); 22 | poly(b); 23 | poly(d); 24 | } -------------------------------------------------------------------------------- /src/lib/graph/vector_v_vector_pair_e.h: -------------------------------------------------------------------------------- 1 | #ifndef F92F9E56_8740_4FD4_9259_76E174846CF4 2 | #define F92F9E56_8740_4FD4_9259_76E174846CF4 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class Graph { 11 | public: 12 | Graph(std::vector &v, std::vector> &e) 13 | : v_(v), e_(e) {} 14 | bool IsEulerWalkable(); 15 | void PrintGraph(); 16 | std::vector v_; 17 | std::vector> e_; 18 | }; 19 | #endif /* F92F9E56_8740_4FD4_9259_76E174846CF4 */ 20 | -------------------------------------------------------------------------------- /src/cpp17/no_discard.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | struct Vector3 { 6 | Vector3(int x1, int y1, int z1) : x(x1), y(y1), z(z1) {} 7 | int x; 8 | int y; 9 | int z; 10 | [[nodiscard]] static Vector3 Normalize(const Vector3& vec) { 11 | Vector3 temp = vec; 12 | // temp.Normalize(); 13 | return temp; 14 | } 15 | }; 16 | 17 | int main(int argc, char const* argv[]) { 18 | Vector3 temp(2, 3, 4); // Oops, this doesn’t change temp 19 | Vector3::Normalize(temp); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /src/templates/member_templates.cc: -------------------------------------------------------------------------------- 1 | // Effective C++, Item 45 2 | #include 3 | template 4 | class shared_ptr { 5 | public: 6 | shared_ptr(shared_ptr const& r); // copy constructor 7 | template // generalized 8 | shared_ptr(shared_ptr const& r); // copy constructor 9 | shared_ptr& operator=(shared_ptr const& r); // copy assignment 10 | template // generalized 11 | shared_ptr& operator=(shared_ptr const& r); // copy assignment 12 | }; -------------------------------------------------------------------------------- /src/inheritance/base_pointer.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Base { 4 | public: 5 | void Talk() { std::cout << "Base talking" << std::endl; } 6 | }; 7 | class Derived : public Base { 8 | public: 9 | void Talk() { std::cout << "Derived talking" << std::endl; } 10 | }; 11 | 12 | int main(int argc, char const *argv[]) { 13 | Derived d; 14 | 15 | Base *pb = &d; // implicitly convert Derived* Base* 16 | 17 | std::cout << "&d: " << &d << std::endl; 18 | std::cout << "pb: " << pb << std::endl; 19 | 20 | d.Talk(); 21 | pb->Talk(); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /src/move/move_so_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Test { 5 | // Default constructor 6 | Test() { 7 | std::cout << "Constructor is called." << std::endl; 8 | mValue = 0; 9 | } 10 | 11 | // Copy constructor 12 | // Test(const Test& rhs) { 13 | // std::cout << "Copy Constructor is called." << std::endl; 14 | // mName = rhs.mName; 15 | // mValue = rhs.mValue; 16 | // } 17 | 18 | std::string mName; 19 | int mValue; 20 | }; 21 | 22 | int main() { 23 | Test a; 24 | Test b = std::move(a); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /src/templates/meta_programming.cc: -------------------------------------------------------------------------------- 1 | // Effective C++, Item 48 2 | #include 3 | template // general case: the value of 4 | struct Factorial { // Factorial is n times the value 5 | // of Factorial 6 | enum { value = n * Factorial::value }; 7 | }; 8 | template <> // special case: the value of 9 | struct Factorial<0> { // Factorial<0> is 1 10 | enum { value = 1 }; 11 | }; 12 | int main(int argc, char const *argv[]) { 13 | auto i = Factorial<10>::value; 14 | 15 | std::cout << "i: " << i << std::endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /src/const/mutable.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | class CTextBlock { 4 | public: 5 | std::size_t length() const; 6 | 7 | private: 8 | char *pText; 9 | mutable std::size_t textLength; // these data members may 10 | mutable bool lengthIsValid; // always be modified, even in 11 | }; // const member functions 12 | std::size_t CTextBlock::length() const { 13 | if (!lengthIsValid) { 14 | textLength = std::strlen(pText); // now fine 15 | lengthIsValid = true; // also fine 16 | } 17 | return textLength; 18 | } -------------------------------------------------------------------------------- /src/cpp17/template_auto.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | int f() { 9 | return value + 1; 10 | } 11 | 12 | int main(int argc, char const *argv[]) { 13 | { 14 | // C++14 15 | auto p1 = std::make_pair(5, 'a'); 16 | // Also C++14 17 | std::pair p2(10, 'b'); 18 | 19 | // C++17! 20 | std::pair p3(15, 'c'); 21 | } 22 | { 23 | auto x = f<10>(); // deduces int 24 | std::cout << "x: " << x << std::endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /src/function/generate_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/lib/utility.h" 9 | //----------------------------------------------------- 10 | int main() { 11 | // Make a vector w/ 10 elements 12 | std::vector v(10); 13 | 14 | 15 | std::generate(v.begin(), // start of range 16 | v.end(), // end of range 17 | // Lambda that returns generated value 18 | []() { return rand(); }); 19 | 20 | Print(v); 21 | } -------------------------------------------------------------------------------- /src/lib/solution.h: -------------------------------------------------------------------------------- 1 | #ifndef TEMPLATE_SOLUTION_H 2 | #define TEMPLATE_SOLUTION_H 3 | 4 | #include 5 | #include 6 | 7 | class Solution { 8 | public: 9 | std::string PrintHelloWorld(); 10 | 11 | int FindMax(const std::vector &inputs) { 12 | if (inputs.empty()) { 13 | return -1; 14 | } 15 | 16 | int result = INT32_MIN; 17 | 18 | // Range based loop 19 | for (auto n : inputs) { 20 | if (n > result) { 21 | result = n; 22 | } 23 | } 24 | // inputs[0] = 1000; 25 | return result; 26 | } 27 | }; 28 | 29 | #endif -------------------------------------------------------------------------------- /src/tree/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "vector_union_main", 10 | srcs = ["vector_union_main.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | ) 13 | 14 | cc_binary( 15 | name = "tree_map_main", 16 | srcs = ["tree_map_main.cc"], 17 | deps = ["//src/lib:SolutionLib"], 18 | ) 19 | 20 | cc_binary( 21 | name = "std_any_main", 22 | srcs = ["std_any_main.cc"], 23 | deps = ["//src/lib:SolutionLib"], 24 | ) 25 | -------------------------------------------------------------------------------- /src/amortization/binary_counter_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "src/lib/utility.h" 4 | 5 | void BinaryCounter(std::string &counter, int n) { 6 | for (int i = 0; i < n; i++) { 7 | int j = counter.size() - 1; 8 | 9 | while (counter[j] == '1') { 10 | counter[j] = '0'; 11 | j--; 12 | } 13 | 14 | if (j >= 0) { 15 | counter[j] = '1'; 16 | } 17 | std::cout << "counter: " << counter << std::endl; 18 | } 19 | } 20 | 21 | int main() { 22 | std::string counter = "0000"; 23 | BinaryCounter(counter, 17); 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /src/cast/static_cast_constructor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | class Class { 5 | public: 6 | //<< conversion constructor 7 | Class(int) { std::cout << "Constructor!" << std::endl; } 8 | }; 9 | 10 | int main(int argc, char const *argv[]) { 11 | // These all call the conversion constructor! 12 | { 13 | int what = 0; 14 | Class object = static_cast(what); 15 | } 16 | { 17 | int what = 0; 18 | Class object = (Class)what; 19 | } 20 | { 21 | int what = 0; 22 | Class object = Class(what); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /tests/graph/adjacency_matrix_test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "gtest/gtest.h" 4 | #include "src/lib/graph/adjacency_matrix.h" 5 | 6 | TEST(vector_v_vector_pair_e, euler_walkable1) { 7 | std::vector> adjacency = {{0, 1, 1, 0, 0}, 8 | {1, 0, 1, 1, 1}, 9 | {1, 1, 0, 1, 0}, 10 | {0, 1, 1, 0, 1}, 11 | {0, 1, 0, 1, 0}}; 12 | Graph g(adjacency); 13 | EXPECT_EQ(true, g.IsEulerWalkable()); 14 | } 15 | -------------------------------------------------------------------------------- /tests/graph/adjacency_vector_of_set_test.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/graph/adjacency_vector_of_set.h" 2 | 3 | #include 4 | 5 | #include "gtest/gtest.h" 6 | 7 | TEST(vector_v_vector_pair_e, euler_walkable1) { 8 | Graph g({Vertex(0, {1, 2}), Vertex(1, {0, 2, 3, 4}), Vertex(2, {0, 1, 3}), 9 | Vertex(3, {1, 2, 4})}); 10 | EXPECT_EQ(true, g.IsEulerWalkable()); 11 | } 12 | TEST(vector_v_vector_pair_e, euler_walkable2) { 13 | Graph g({Vertex(0, {1, 2, 3}), Vertex(1, {0, 2, 3}), Vertex(2, {0, 1, 3}), 14 | Vertex(3, {0, 1, 2})}); 15 | EXPECT_EQ(false, g.IsEulerWalkable()); 16 | } 17 | -------------------------------------------------------------------------------- /src/lib/graph/adjacency_vector_of_set.h: -------------------------------------------------------------------------------- 1 | #ifndef D90B5B66_BEE3_4C49_90C9_6047CD8ABD19 2 | #define D90B5B66_BEE3_4C49_90C9_6047CD8ABD19 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | struct Vertex { 11 | Vertex(int v, std::set a) : vertex_number(v), adjacents(a) {} 12 | int vertex_number; 13 | std::set adjacents; 14 | }; 15 | 16 | class Graph { 17 | public: 18 | Graph(std::vector v) : v_(v) {} 19 | bool IsEulerWalkable(); 20 | std::vector v_; 21 | }; 22 | #endif /* D90B5B66_BEE3_4C49_90C9_6047CD8ABD19 */ 23 | -------------------------------------------------------------------------------- /src/templates/non_type.cc: -------------------------------------------------------------------------------- 1 | #include 2 | template 3 | class A { 4 | public: 5 | A() { std::cout << "size: " << size << std::endl; } 6 | void PrintSize() { std::cout << "size: " << size << std::endl; } 7 | }; 8 | 9 | // Can't do this for floats 10 | // template 11 | // class B { 12 | // B() { std::cout << "size: " << size << std::endl; } 13 | // void PrintSize() { std::cout << "size: " << size << std::endl; } 14 | // }; 15 | 16 | int main(int argc, char const *argv[]) { 17 | { 18 | A a; 19 | a.PrintSize(); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /src/typeid/2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | class Person { 7 | public: 8 | // virtual ~Person() {} 9 | void Talk() { std::cout << "Person talking!" << std::endl; } 10 | }; 11 | class Employee : public Person { 12 | void Talk() { std::cout << "Employee Talking!" << std::endl; } 13 | }; 14 | 15 | int main(int argc, char const* argv[]) { 16 | // Later... 17 | Person* ptr = new Employee(); 18 | 19 | ptr->Talk(); 20 | 21 | // Outputs "class Person" 22 | std::cout << typeid(*ptr).name() << std::endl; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/dynamic_programming/memoization.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "src/lib/utility.h" 7 | 8 | int f(int input) { 9 | int result; 10 | result = VeryComplexFunction(input); 11 | 12 | return result; 13 | } 14 | 15 | // memo map works like a cache 16 | std::map memo; 17 | int f(int input) { 18 | // If it exists in the cache return the value immediately 19 | if (memo.count(input) > 0) { 20 | return memo[input]; 21 | } 22 | 23 | memo[input] = VeryComplexFunction(input); 24 | 25 | return memo[input]; 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/exceptions/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = [], 6 | ) 7 | 8 | cc_binary( 9 | name = "stack_unwinding", 10 | srcs = ["stack_unwinding.cc"], 11 | deps = [], 12 | ) 13 | 14 | cc_binary( 15 | name = "std_exception", 16 | srcs = ["std_exception.cc"], 17 | deps = [], 18 | ) 19 | 20 | cc_binary( 21 | name = "rtti", 22 | srcs = ["rtti.cc"], 23 | deps = [], 24 | ) 25 | 26 | 27 | cc_binary( 28 | name = "custom_rtti", 29 | srcs = ["custom_rtti.cc"], 30 | deps = [], 31 | ) 32 | 33 | -------------------------------------------------------------------------------- /src/graph/adjacency_vector_of_set_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "src/lib/graph/adjacency_vector_of_set.h" 8 | #include "src/lib/utility.h" 9 | 10 | int main() { 11 | Graph g( 12 | { // Vector 13 | Vertex(0, {1, 2, 3}), // Vertex object 14 | Vertex(1, {0, 2, 3}), 15 | Vertex(2, {0, 1, 3}), 16 | Vertex(3, {0, 1, 2})} 17 | ); 18 | std::cout << g.IsEulerWalkable() << std::endl; 19 | } 20 | 21 | 22 | 1 -------> 2 23 | 24 | { 1, {2}}; 25 | { 2, {}}; 26 | std::pair<> 27 | -------------------------------------------------------------------------------- /src/random/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "rand_main", 4 | srcs = ["rand_main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | 9 | cc_binary( 10 | name = "uniform_main", 11 | srcs = ["uniform_main.cc"], 12 | deps = ["//src/lib:SolutionLib"], 13 | ) 14 | 15 | cc_binary( 16 | name = "uniform_float_main", 17 | srcs = ["uniform_float_main.cc"], 18 | deps = ["//src/lib:SolutionLib"], 19 | ) 20 | 21 | cc_binary( 22 | name = "uniform_bind_main", 23 | srcs = ["uniform_bind_main.cc"], 24 | deps = ["//src/lib:SolutionLib"], 25 | ) -------------------------------------------------------------------------------- /src/templates/variadic_with_init_list.cc: -------------------------------------------------------------------------------- 1 | // Effective modern C++ Item 7 2 | #include 3 | 4 | #include 5 | template // types of arguments to use 7 | void doSomeWork(Ts &&... params) { 8 | // create local T object from params... 9 | 10 | T localObject1(std::forward(params)...); // using parens 11 | T localObject2{std::forward(params)...}; // using braces 12 | } 13 | 14 | int main(int argc, char const *argv[]) { 15 | std::vector v; 16 | doSomeWork>(10, 20); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /src/inheritance/final.cc: -------------------------------------------------------------------------------- 1 | #include 2 | class Base { 3 | public: 4 | Base() { std::cout << "Base!" << std::endl; } 5 | 6 | // No virtual destructor defined 7 | virtual void Foo() = 0; // abstract method 8 | }; 9 | class Derived final : public Base { 10 | public: 11 | Derived() { std::cout << "Derived!" << std::endl; } 12 | ~Derived() { std::cout << "Derived destructor!" << std::endl; } 13 | void Foo(){}; // Will also be virtual 14 | }; 15 | 16 | int main() { 17 | { 18 | Base *b = new Derived(); 19 | // b->Talk(); 20 | // b->Speak(); 21 | 22 | delete b; 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /src/iterator/istream_iterator.cc: -------------------------------------------------------------------------------- 1 | // istream_iterator example 2 | #include // std::cin, std::cout 3 | #include // std::istream_iterator 4 | 5 | int main() { 6 | double value1, value2; 7 | std::cout << "Please, insert two values: " << std::endl; 8 | 9 | std::istream_iterator eos; // end-of-stream iterator 10 | std::istream_iterator iit(std::cin); // stdin iterator 11 | 12 | if (iit != eos) value1 = *iit; 13 | 14 | ++iit; 15 | if (iit != eos) value2 = *iit; 16 | 17 | std::cout << value1 << "*" << value2 << "=" << (value1 * value2) << '\n'; 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /src/memory_allocator/std_allocator1.cc: -------------------------------------------------------------------------------- 1 | // C++ program for illustration 2 | // of std::allocator() function 3 | #include 4 | #include 5 | using namespace std; 6 | int main() { 7 | // allocator for integer values 8 | allocator myAllocator; 9 | 10 | // allocate space for five ints 11 | int* arr = myAllocator.allocate(5); 12 | 13 | // construct arr[0] and arr[3] 14 | myAllocator.construct(arr, 100); 15 | arr[3] = 10; 16 | 17 | cout << arr[3] << endl; 18 | cout << arr[0] << endl; 19 | 20 | // deallocate space for five ints 21 | myAllocator.deallocate(arr, 5); 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /src/uniform_initialization/initializer_list/explicit_constructor_vs_cast.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class Widget { 6 | public: 7 | explicit Widget(int size); 8 | }; 9 | 10 | void doSomeWork(const Widget& w); 11 | int main(int argc, char const* argv[]) { 12 | // doSomeWork(15); ---> Doesn't compile due to explicit. 13 | doSomeWork(Widget(15)); // create Widget from int 14 | // with function-style cast 15 | doSomeWork(static_cast(15)); // create Widget from int 16 | // with C++-style cast 17 | 18 | Widget z = static_cast(15); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/cpp14/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "lambda_capture_init", 5 | srcs = ["lambda_capture_init.cc"], 6 | ) 7 | 8 | cc_binary( 9 | name = "generic_lambda", 10 | srcs = ["generic_lambda.cc"], 11 | ) 12 | 13 | cc_binary( 14 | name = "return_type_deduction", 15 | srcs = ["return_type_deduction.cc"], 16 | ) 17 | 18 | cc_binary( 19 | name = "deprecated", 20 | srcs = ["deprecated.cc"], 21 | ) 22 | 23 | cc_binary( 24 | name = "other", 25 | srcs = ["other.cc"], 26 | ) 27 | 28 | cc_binary( 29 | name = "auto", 30 | srcs = ["auto.cc"], 31 | ) -------------------------------------------------------------------------------- /src/function/inline.cc: -------------------------------------------------------------------------------- 1 | #include 2 | inline int f(int a, int b) { 3 | return a + b; 4 | } // assume compilers are willing to inline calls to f 5 | int main(int argc, char const *argv[]) { 6 | int (*pf)(int, int) = f; // pf points to f 7 | int x; 8 | std::cin >> x; 9 | int a = f(x, 100); // this call will be inlined, because it's a "normal" call 10 | int b = pf(x, 120); // this call probably won't be, because it's through 11 | std::cout << "pf: " << pf << std::endl; 12 | // a function pointer 13 | std::cout << "a: " << a << std::endl; 14 | std::cout << "b: " << b << std::endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /src/passing_variables/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | cc_binary( 8 | name = "pass_by_reference_main", 9 | srcs = ["pass_by_reference_main.cc"], 10 | deps = ["//src/lib:SolutionLib"], 11 | ) 12 | 13 | cc_binary( 14 | name = "pass_by_reference_avoiding_copy_main", 15 | srcs = ["pass_by_reference_avoiding_copy_main.cc"], 16 | deps = ["//src/lib:SolutionLib"], 17 | ) 18 | 19 | cc_binary( 20 | name = "pass_by_reference_slicing", 21 | srcs = ["pass_by_reference_slicing.cc"], 22 | ) 23 | -------------------------------------------------------------------------------- /src/class/struct.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Point { 6 | int x; 7 | int y; 8 | std::string name; 9 | }; 10 | 11 | void PrintPoint(Point &p) { 12 | std::cout << "p.x: " << p.x << ", p.y: " << p.y << ", p.name: " << p.name 13 | << std::endl; 14 | } 15 | 16 | int main() { 17 | Point p1; 18 | 19 | p1.x = 20; 20 | p1.y = 30; 21 | p1.name = "My Point 1"; 22 | 23 | PrintPoint(p1); 24 | 25 | Point p2; 26 | p2 = p1; 27 | p2.x++; 28 | p2.name = "My Point 2"; 29 | PrintPoint(p2); 30 | 31 | // Doesn't compile 32 | // if(p1==p2){ 33 | 34 | // } 35 | } -------------------------------------------------------------------------------- /src/const/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "const_param_main", 10 | srcs = ["const_param_main.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | ) 13 | 14 | cc_binary( 15 | name = "const_class_main", 16 | srcs = ["const_class_main.cc"], 17 | deps = ["//src/lib:SolutionLib"], 18 | ) 19 | 20 | cc_binary( 21 | name = "const_pointer", 22 | srcs = ["const_pointer.cc"], 23 | ) 24 | 25 | cc_binary( 26 | name = "const_functions", 27 | srcs = ["const_functions.cc"], 28 | ) -------------------------------------------------------------------------------- /src/dynamic_programming/fib_table_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "src/lib/utility.h" 7 | 8 | // Fibonacci with memoization 9 | int Fibonacci(int n) { 10 | // Allocate for 0 to n included 11 | std::vector d(n + 1); 12 | 13 | for (int i = 0; i <= n; i++) { 14 | if (i == 0 || i == 1) { 15 | d[i] = 1; 16 | } else { 17 | d[i] = d[i - 1] + d[i - 2]; 18 | } 19 | } 20 | 21 | return d[n]; 22 | } 23 | 24 | // Driver function 25 | int main() { 26 | int n = Fibonacci(42); 27 | std::cout << "n: " << n << std::endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /src/iterator/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = [ 7 | "//src/lib:SolutionLib", 8 | "//src/lib:utility", 9 | ], 10 | ) 11 | 12 | cc_binary( 13 | name = "main2", 14 | srcs = ["main2.cc"], 15 | deps = [ 16 | "//src/lib:SolutionLib", 17 | "//src/lib:utility", 18 | ], 19 | ) 20 | 21 | cc_binary( 22 | name = "istream_iterator", 23 | srcs = ["istream_iterator.cc"], 24 | ) 25 | 26 | cc_binary( 27 | name = "iterator_implementation", 28 | srcs = ["iterator_implementation.cc"], 29 | ) 30 | -------------------------------------------------------------------------------- /src/templates/numbers.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // The size cannot be greater than 100 4 | template 5 | class A { 6 | public: 7 | int get_size() { return size; } 8 | }; 9 | 10 | // Not valid for float 11 | // template 12 | // class B { 13 | // int get_size() { return size; } 14 | // }; 15 | 16 | int main() { 17 | A<3> a1; 18 | A<4> a2; 19 | A<101> a3; 20 | A<> a4; 21 | 22 | std::cout << "a1.get_size(): " << a1.get_size() << std::endl; 23 | std::cout << "a2.get_size(): " << a2.get_size() << std::endl; 24 | std::cout << "a3.get_size(): " << a3.get_size() << std::endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /src/hash/main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | 6 | struct MyPerson { 7 | MyPerson() : name("No Name"), age(0) {} 8 | MyPerson(const std::string &name_p, int age_p) : name(name_p), age(age_p) {} 9 | std::string name; 10 | int age; 11 | }; 12 | 13 | int main() { 14 | std::vector persons(4); //Vector of size 4 15 | 16 | persons[0] = MyPerson("Tom", 12); 17 | persons[1] = MyPerson("Alice", 18); 18 | persons[2] = MyPerson("Bob", 15); 19 | persons[3] = MyPerson("Ted", 57); 20 | 21 | std::cout << "persons[1].name: " << persons[1].name << std::endl; 22 | 23 | } -------------------------------------------------------------------------------- /src/function/all_off_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | bool MyPredicate(const int& i) { return (i % 2) == 0; } 6 | 7 | //----------------------------------------------------- 8 | int main() { 9 | std::vector v1{2, 4, 6, 8, 10}; 10 | auto all_are_even = std::all_of(v1.begin(), v1.end(), MyPredicate); 11 | 12 | auto all_are_odd = std::all_of(v1.begin(), v1.end(), 13 | [](const int& i) { return (i % 2) == 1; }); 14 | 15 | std::copy_n() 16 | 17 | std::cout << "all_are_even: " << all_are_even 18 | << ", all_are_odd: " << all_are_odd << std::endl; 19 | } -------------------------------------------------------------------------------- /src/control/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "if_main", 5 | srcs = ["if_main.cc"], 6 | deps = [ 7 | "//src/lib:utility", 8 | ], 9 | ) 10 | 11 | cc_binary( 12 | name = "switch_main", 13 | srcs = ["switch_main.cc"], 14 | deps = [ 15 | "//src/lib:utility", 16 | ], 17 | ) 18 | 19 | cc_binary( 20 | name = "for_main", 21 | srcs = ["for_main.cc"], 22 | deps = [ 23 | "//src/lib:utility", 24 | ], 25 | ) 26 | 27 | cc_binary( 28 | name = "while_main", 29 | srcs = ["while_main.cc"], 30 | deps = [ 31 | "//src/lib:utility", 32 | ], 33 | ) 34 | -------------------------------------------------------------------------------- /src/cpp14/return_type_deduction.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | // This is fine in C++14 7 | auto sq2(double d) { return d * d; } 8 | 9 | // auto func1(bool b) { 10 | // if (b) { 11 | // return 1; 12 | // } else { 13 | // return 1.0; // Error! 14 | // } 15 | // } 16 | 17 | auto factorial(unsigned n) { 18 | if (n == 0) { 19 | return (unsigned) 1; 20 | } else { 21 | return n * factorial(n - 1); // OK 22 | } 23 | } 24 | 25 | int main() { 26 | { 27 | // This is fine in C++11 28 | auto sq1 = [](double d) { return d * d; }; 29 | 30 | sq1(10); 31 | } 32 | return 1; 33 | } -------------------------------------------------------------------------------- /src/templates/parameter_independant.cc: -------------------------------------------------------------------------------- 1 | // Effective C++, Item 44 2 | #include 3 | 4 | template // objects of type T; see below for info 6 | class SquareMatrix { // on the size_t parameter 7 | public: 8 | void invert(); // invert the matrix in place 9 | }; 10 | 11 | int main(int argc, char const *argv[]) { 12 | // Two copies of invert will be instantiated! 13 | SquareMatrix sm1; 14 | sm1.invert(); // call SquareMatrix::invert 15 | SquareMatrix sm2; 16 | sm2.invert(); // call SquareMatrix::invert 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/graph/set_v_set_pair_e_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "src/lib/utility.h" 8 | 9 | // class Graph { 10 | // public: 11 | // Graph(std::set &v, std::set> &e) : v_(v), e_(e) {} 12 | // bool IsEulerWalkable(); 13 | // std::set v_; 14 | // std::set> e_; 15 | // }; 16 | 17 | // bool Graph::IsEulerWalkable(){ 18 | // std::vector inDegrees; 19 | 20 | // } 21 | 22 | int main() { 23 | std::set v = {1, 2, 3, 4}; 24 | std::set> e = {{1, 2}, {2, 4}, {4, 3}, {3, 1}}; 25 | Graph g(v, e); 26 | } 27 | -------------------------------------------------------------------------------- /third_party/usr_local.BUILD: -------------------------------------------------------------------------------- 1 | # third_party/usr_local.BUILD 2 | # Demos how to use third party include/lib files 3 | cc_library( 4 | name = "glog", 5 | hdrs = glob(["include/glog/**"]), 6 | srcs = [ 7 | "lib/libglog.a", 8 | ], 9 | includes = [ 10 | "include/glog", 11 | "include/gflags", 12 | "include", 13 | ], 14 | visibility = ["//visibility:public"], 15 | ) 16 | 17 | cc_library( 18 | name = "tbb", 19 | hdrs = glob(["include/tbb/**"]), 20 | srcs = [ 21 | "lib/libglog.a", 22 | ], 23 | includes = [ 24 | "include/glog", 25 | "include/gflags", 26 | "include", 27 | ], 28 | visibility = ["//visibility:public"], 29 | ) 30 | 31 | -------------------------------------------------------------------------------- /src/scope/namespace_nested_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | // namespaces 6 | #include 7 | int x = 0; 8 | void PrintSomething() { std::cout << "Printing example 0." << std::endl; } 9 | 10 | namespace ns1 { 11 | void PrintSomething1() { std::cout << "Printing example 2." << std::endl; } 12 | 13 | namespace ns2 { 14 | int x = 1; 15 | void PrintSomething1() { std::cout << "Printing example 1." << std::endl; } 16 | } // namespace ns2 17 | }; // namespace ns1 18 | 19 | int main() { 20 | 21 | ns1::PrintSomething1(); 22 | ns1::ns2::PrintSomething1(); 23 | ::PrintSomething(); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /src/bst/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = [ 7 | "//src/lib:BSTLib", 8 | "//src/lib:SolutionLib", 9 | "//src/lib:utility", 10 | ], 11 | ) 12 | 13 | cc_binary( 14 | name = "vector_union_main", 15 | srcs = ["vector_union_main.cc"], 16 | deps = ["//src/lib:SolutionLib"], 17 | ) 18 | 19 | cc_binary( 20 | name = "tree_map_main", 21 | srcs = ["tree_map_main.cc"], 22 | deps = ["//src/lib:SolutionLib"], 23 | ) 24 | 25 | cc_binary( 26 | name = "std_any_main", 27 | srcs = ["std_any_main.cc"], 28 | deps = ["//src/lib:SolutionLib"], 29 | ) 30 | -------------------------------------------------------------------------------- /src/tree/std_any_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/lib/utility.h" 9 | 10 | int main() { 11 | std::map Notebook; 12 | 13 | std::string name{"Pluto"}; 14 | int year = 2015; 15 | 16 | Notebook["PetName"] = name; 17 | Notebook["Born"] = year; 18 | 19 | std::string strS = 20 | std::any_cast(Notebook["PetName"]); // = "Pluto" 21 | int intI = std::any_cast(Notebook["Born"]); // = 2015 22 | 23 | std::cout << "strS: " << strS << std::endl; 24 | std::cout << "intI: " << intI << std::endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /src/bst/main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/bst.h" 2 | #include "src/lib/utility.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() { 10 | 11 | BST t; 12 | 13 | t.insert(t.root_, 8); 14 | t.insert(t.root_, 3); 15 | t.insert(t.root_, 10); 16 | t.insert(t.root_, 2); 17 | t.insert(t.root_, 5); 18 | t.insert(t.root_, 4); 19 | t.insert(t.root_, 7); 20 | t.insert(t.root_, 14); 21 | t.insert(t.root_, 12); 22 | 23 | t.InOrder(t.root_); 24 | 25 | auto r1 = t.search(t.root_, 14); 26 | std::cout << "r1: " << r1 << std::endl; 27 | auto r2 = t.search(t.root_, 100); 28 | std::cout << "r2: " << r2 << std::endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /src/constructor/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class MyClass { 6 | public: 7 | MyClass(int a) : a_(a), b_(0) {} 8 | MyClass(int a, int b) : a_(a), b_(b) {} 9 | void Print() { std::cout << "a: " << a_ << ", b: " << b_ << std::endl; } 10 | int a_; 11 | int b_; 12 | }; 13 | int add(int a, int b) { 14 | int c = a + b; 15 | return c; 16 | } 17 | int main() { 18 | MyClass my_class1(3); 19 | my_class1.Print(); 20 | MyClass my_class2({1, 2}); //--> Passing two parameters to a constructor 21 | //(uniform initialization) 22 | my_class2.Print(); 23 | 24 | // auto x = add({1, 2}); //---> won't compile! 25 | } -------------------------------------------------------------------------------- /src/function/advanced_transform_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | bool IsOdd(int i) { return ((i % 2) == 1); } 9 | int MapSum(int value, std::pair i) { 10 | return value + i.second; 11 | } 12 | 13 | int main() { 14 | 15 | // Accumulate 16 | { 17 | std::map m = { 18 | {"Tommy", 18}, {"David", 30}, {"John", 18}, {"Ari", 30}}; 19 | auto sum = std::accumulate(m.begin(), m.end(), /*init=*/0, 20 | /*binary_op=*/MapSum); 21 | std::cout << "sum: " << sum << std::endl; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /src/templates/fibonacci.cc: -------------------------------------------------------------------------------- 1 | #include 2 | // Calculate a Fibonacci number at COMPILE TIME 3 | template 4 | struct Fibonacci { 5 | static const int value = Fibonacci::value + Fibonacci::value; 6 | static_assert(i != 0, "Fibonacci of 0 is undefined."); 7 | }; 8 | // The "base cases" are i == 1 or i == 2 9 | // So use specialization... 10 | template <> 11 | struct Fibonacci<1> { 12 | static const int value = 1; 13 | }; 14 | template <> 15 | struct Fibonacci<2> { 16 | static const int value = 1; 17 | }; 18 | 19 | int main() { 20 | // This would be 610 (at COMPILE TIME) 21 | int fib = Fibonacci<15>::value; 22 | std::cout << "fib: " << fib << std::endl; 23 | } 24 | -------------------------------------------------------------------------------- /src/lib/graph/adjacency_vector_of_set.cc: -------------------------------------------------------------------------------- 1 | #include "adjacency_vector_of_set.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | bool Graph::IsEulerWalkable() { 10 | // Create a table to hold degree of each vertex 11 | std::vector degrees(v_.size()); 12 | 13 | // Iterate vertices 14 | for (auto v : v_) { 15 | degrees[v.vertex_number] = v.adjacents.size(); 16 | } 17 | 18 | // Iterate through degree table and count the number of odd ones 19 | 20 | int countOdds = 0; 21 | 22 | for (auto d : degrees) { 23 | if (d % 2 == 1) { 24 | countOdds++; 25 | } 26 | } 27 | return (countOdds == 0 || countOdds == 2); 28 | } 29 | -------------------------------------------------------------------------------- /src/linked_list/main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/linked_list.h" 2 | #include "src/lib/utility.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() { 10 | SinglyLinkedList l; 11 | l.push_back(1); 12 | l.push_back(2); 13 | std::cout << "l.size(): " << l.size() << std::endl; 14 | 15 | l.push_back(3); 16 | l.print(); 17 | std::cout << "l.size(): " << l.size() << std::endl; 18 | 19 | SinglyLinkedList l2 = l; 20 | 21 | // Test [] operator 22 | l.print(); 23 | int item = l[0]; 24 | std::cout << "l[0]: " << l[0] << ", l[1]: " << l[1] << ", l[2]: " << l[2] 25 | << std::endl; 26 | l[0] += 2; 27 | l.print(); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /src/move/copy_elision.cc: -------------------------------------------------------------------------------- 1 | // The output is different if you run this code with C++17 vs C++11 and 2 | // -fno-elide-constructors 3 | #include 4 | using namespace std; 5 | 6 | class B { 7 | public: 8 | B(const char* str = "\0") // default constructor 9 | { 10 | cout << "Constructor called" << endl; 11 | } 12 | 13 | B(const B& b) // copy constructor 14 | { 15 | cout << "Copy constructor called" << endl; 16 | } 17 | }; 18 | 19 | int main() { 20 | { 21 | // This should convert to: 22 | // B ob("copy me"); 23 | B ob = "copy me"; 24 | } 25 | 26 | { 27 | // This should convert to: 28 | // B ob("copy me"); 29 | B ob = B("copy me"); 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /src/function/adjacent_diff_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/lib/utility.h" 9 | //----------------------------------------------------- 10 | int main() { 11 | std::vector from({10, 15, 20, 25, 30, 35}); 12 | std::vector to; 13 | std::adjacent_difference( 14 | from.begin(), // Start of range 15 | from.end(), // End of range 16 | std::back_inserter(to), // Collection to insert into 17 | // Lambda to compute difference (defaults to subtraction) 18 | [](int a, int b) { return a - b; }); 19 | 20 | Print(from); 21 | Print(to); 22 | } -------------------------------------------------------------------------------- /src/lib/factory/img_create.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Image {}; 5 | class BitmapImage : public Image {}; 6 | class PngImage : public Image {}; 7 | class JpgImage : public Image {}; 8 | 9 | struct IImageFactory { 10 | virtual std::shared_ptr Create(std::string_view type) = 0; 11 | }; 12 | 13 | struct ImageFactory : public IImageFactory { 14 | virtual std::shared_ptr Create(std::string_view type) override { 15 | if (type == "bmp") 16 | return std::make_shared(); 17 | else if (type == "png") 18 | return std::make_shared(); 19 | else if (type == "jpg") 20 | return std::make_shared(); 21 | return nullptr; 22 | } 23 | }; -------------------------------------------------------------------------------- /src/graph/adjacency_matrix_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/lib/graph/adjacency_matrix.h" 9 | #include "src/lib/utility.h" 10 | 11 | int main() { 12 | std::vector> adjacency = {{0, 1, 1, 0, 0}, 13 | {1, 0, 1, 1, 1}, 14 | {1, 1, 0, 1, 0}, 15 | {0, 1, 1, 0, 1}, 16 | {0, 1, 0, 1, 0}}; 17 | Graph g(adjacency); 18 | std::cout << "g.isEulerWalkable(): " << g.IsEulerWalkable() << std::endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/pointers/pointer_to_void/main.cc: -------------------------------------------------------------------------------- 1 | // including c++ input and output libraries 2 | #include 3 | using namespace std; 4 | // main method for run c++ application 5 | int main() { 6 | // declaring void pointer 7 | void* pointer; 8 | // declaring and initializing float variable 9 | float money = 55.50; 10 | // initializing void pointer variable 11 | pointer = &money; // converting float to void pointer 12 | // displaying output 13 | cout << &money 14 | << endl; // displayed money address, it may varied from system to system 15 | // displaying output 16 | cout << pointer << endl; // displayed pointer address, it may varied from 17 | // system to system 18 | return 0; 19 | } -------------------------------------------------------------------------------- /src/multi_threading/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "src/lib/utility.h" 8 | 9 | int main() { 10 | const int number_of_threads = 2; 11 | const int step = 1000 * 1000 * 1000 / number_of_threads; 12 | std::vector partial_sums(number_of_threads); 13 | 14 | std::thread t1(GetRangeSum, 0, step); 15 | std::thread t2(GetRangeSum, step, 2 * step + 1); 16 | 17 | t1.join(); 18 | t2.join(); 19 | 20 | uint64_t total = 21 | std::accumulate(partial_sums.begin(), partial_sums.end(), uint64_t(0)); 22 | Print(partial_sums); 23 | std::cout << "total: " << total << std::endl; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /src/inheritance/scope.cc: -------------------------------------------------------------------------------- 1 | class Base { 2 | private: 3 | int x; 4 | 5 | public: 6 | virtual void mf1() = 0; 7 | virtual void mf1(int); 8 | virtual void mf2(); 9 | void mf3(); //--> 10 | void mf3(double); 11 | }; 12 | class Derived : public Base { 13 | public: 14 | // using Base::mf1; // make all things in Base named mf1 and mf3 15 | // using Base::mf3; // visible (and public) in Derived's scope 16 | 17 | virtual void mf1(); 18 | void mf3(); //--> hides both mf3 in Base 19 | void mf4(); 20 | }; 21 | int main(int argc, char const *argv[]) { 22 | /* code */ 23 | Derived d; 24 | // d.mf3(5.5); // --> Doesn't compile unles we have "using" statements 25 | d.Base::mf3(5.5); // -> Compiles 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /src/lib/bst.cc: -------------------------------------------------------------------------------- 1 | #include "bst.h" 2 | #include 3 | 4 | bool BST::empty() { return root_ == nullptr; } 5 | 6 | int BST::size() { return 0; } 7 | 8 | void BST::insert(TreeNode *&root, int v) { 9 | if (root == nullptr) { 10 | root = new TreeNode(v); 11 | } else if (v < root->val) { 12 | insert(root->left, v); 13 | } else if (v > root->val) { 14 | insert(root->right, v); 15 | } 16 | } 17 | 18 | TreeNode *BST::search(TreeNode *root, int v) { 19 | if (root == nullptr) { 20 | return root; 21 | } 22 | if (root->val == v) { 23 | return root; 24 | } 25 | if (v < root->val) { 26 | return search(root->left, v); 27 | } else // v > root->val 28 | { 29 | return search(root->right, v); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/tuple/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | { 5 | auto myTuple = std::make_tuple(50, "Hello", 10.0); 6 | 7 | std::cout << "std::get<0>(myTuple): " << std::get<0>(myTuple) << std::endl; 8 | std::cout << "std::get<1>(myTuple): " << std::get<1>(myTuple) << std::endl; 9 | std::cout << "std::get<2>(myTuple): " << std::get<2>(myTuple) << std::endl; 10 | } 11 | 12 | { 13 | auto myTuple = std::make_tuple(1337); 14 | auto otherTuple = std::make_tuple(std::string("Yo!")); 15 | 16 | auto cat = std::tuple_cat(myTuple, otherTuple); 17 | std::cout << "std::get<0>(cat): " << std::get<0>(cat) << std::endl; 18 | std::cout << "std::get<1>(cat): " << std::get<1>(cat) << std::endl; 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /src/modifiers/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "machine_independent_main", 10 | srcs = ["machine_independent_main.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | ) 13 | 14 | cc_binary( 15 | name = "constants_main", 16 | srcs = ["constants_main.cc"], 17 | deps = ["//src/lib:SolutionLib"], 18 | ) 19 | 20 | 21 | cc_binary( 22 | name = "sizeof_main", 23 | srcs = ["sizeof_main.cc"], 24 | deps = ["//src/lib:utility"], 25 | ) 26 | 27 | cc_binary( 28 | name = "sizeof_examples_main", 29 | srcs = ["sizeof_examples_main.cc"], 30 | deps = ["//src/lib:utility"], 31 | ) -------------------------------------------------------------------------------- /src/dynamic_programming/coin_tab_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "src/lib/utility.h" 7 | 8 | int coinChange(std::vector &coins, int amount) { 9 | std::vector d(amount + 1); 10 | d[0] = 0; 11 | 12 | for (int i = 1; i <= amount; i++) { 13 | d[i] = INT_MAX-1; 14 | 15 | for (auto coin : coins) { 16 | if (i - coin >= 0) { 17 | d[i] = std::min(d[i - coin] + 1, d[i]); 18 | } 19 | } 20 | } 21 | 22 | return d[amount] > amount ? -1 : d[amount]; 23 | } 24 | 25 | int main() { 26 | std::vector coins = {2}; 27 | int amount = 3; 28 | int n = coinChange(coins, amount); 29 | std::cout << "n: " << n << std::endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /src/graph/map_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "src/lib/utility.h" 8 | 9 | class Graph { 10 | public: 11 | Graph(std::map> &vertices) : v_(vertices) {} 12 | void PrintGraph() { 13 | for (const auto &v : v_) { 14 | std::cout << "Vertex: " << v.first << ", Adjacents: "; 15 | Print(v.second); 16 | } 17 | } 18 | 19 | std::map> v_; 20 | }; 21 | 22 | int main() { 23 | std::map> vertices{ 24 | {1, {2, 3}}, 25 | {2, {1, 3, 4, 5}}, 26 | {3, {1, 2, 4}}, 27 | {4, {2, 4}} 28 | }; 29 | Graph g(vertices); 30 | g.PrintGraph(); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /src/patterns/proxy/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | class BoundingBox {}; 3 | 4 | struct Image { 5 | bool IsLoaded(); 6 | void LoadAsync(std::string fileName, BoundingBox& bounds) { 7 | // Load image on separate thread (without blocking) 8 | } 9 | void Draw() { 10 | // Draws this image at location 11 | } 12 | 13 | private: 14 | BoundingBox mBounds; 15 | }; 16 | 17 | struct ImageProxy { 18 | void Load(std::string fileName, BoundingBox& bounds) { 19 | mImage.LoadAsync(fileName, bounds); 20 | } 21 | void Draw() { 22 | if (mImage.IsLoaded()) { 23 | mImage.Draw(); 24 | } else { 25 | // Draw blank image with appropriate bounding box 26 | } 27 | } 28 | 29 | private: 30 | Image mImage; 31 | BoundingBox mBounds; 32 | }; 33 | -------------------------------------------------------------------------------- /src/references/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int i = 10; // original 6 | int& j = i; // reference 7 | int* p = &i; // pointer 8 | int k = i; // copy! 9 | 10 | std::cout << "i: " << i << std::endl; 11 | std::cout << "j: " << j << std::endl; 12 | std::cout << "k: " << k << std::endl; 13 | 14 | j++; // i++ 15 | (*p)++; // i++ 16 | std::cout << "i: " << i << std::endl; 17 | std::cout << "j: " << j << std::endl; 18 | std::cout << "k: " << k << std::endl; 19 | 20 | p++; 21 | (*p)++; // What is incremented? Might crash your program! 22 | std::cout << "i: " << i << std::endl; 23 | std::cout << "j: " << j << std::endl; 24 | std::cout << "k: " << k << std::endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /src/cpp14/lambda_capture_init.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | { 7 | int x = 4; 8 | auto y = [& r = x, x = x + 1]() { 9 | r += 2; 10 | return x + 2; 11 | }; 12 | 13 | // Updates ::x to 6, and initializes y to 7. 14 | std::cout << "y(): " << y() << std::endl; 15 | std::cout << "x: " << x << std::endl; 16 | } 17 | 18 | // Move inside lambda 19 | { 20 | std::unique_ptr p(new int); 21 | int x = 5; 22 | *p = 11; 23 | std::cout << "p: " << p << std::endl; 24 | auto y = [p = std::move(p)]() { std::cout << "inside: " << *p << "\n"; }; 25 | 26 | y(); 27 | std::cout << "p: " << p << std::endl; 28 | std::cout << "outside: " << *p << "\n"; 29 | } 30 | } -------------------------------------------------------------------------------- /src/lib/heap.h: -------------------------------------------------------------------------------- 1 | #ifndef HEAP_H 2 | #define HEAP_H 3 | #include 4 | 5 | class Heap { 6 | public: 7 | bool empty() { return data_.empty(); }; 8 | int size() { return data_.size(); } 9 | 10 | int GetParent(int i); 11 | int GetLeft(int i); 12 | int GetRight(int i); 13 | 14 | int GetParentIndex(int i); 15 | int GetLeftIndex(int i); 16 | int GetRightIndex(int i); 17 | int GetSmallestChildIndex(int i); 18 | 19 | int top(); 20 | void push(int v); 21 | void pop(); 22 | 23 | void TrickleUp(int i); 24 | void TrickleDown(int i); 25 | 26 | std::vector ConvertToHeap(const std::vector &input); 27 | 28 | // private: 29 | std::vector data_; 30 | }; 31 | 32 | std::vector HeapSort(const std::vector &input); 33 | 34 | #endif -------------------------------------------------------------------------------- /src/passing_variables/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Acts like int j = i 5 | void PassByValue(int j) { j++; } 6 | 7 | // Acts like int *j = &i; 8 | void PassByReferenceUsingPointer(int *j) { (*j)++; } 9 | 10 | // Acts like int &j = i; 11 | void PassByReferenceUsingReference(int &j) { j++; } 12 | 13 | int main() { 14 | int i = 10; 15 | 16 | int j[100]; 17 | 18 | std::vector v; 19 | 20 | for (int i = 0; i < 100000; i++) { 21 | v.push_back(10); 22 | } 23 | PassByValue(i); 24 | std::cout << "i: " << i << std::endl; 25 | 26 | PassByReferenceUsingPointer(&i); 27 | std::cout << "i: " << i << std::endl; 28 | 29 | PassByReferenceUsingReference(i); 30 | std::cout << "i: " << i << std::endl; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /src/smart_pointer/pimple/widget.cc: -------------------------------------------------------------------------------- 1 | 2 | //----------------------------------------------------------------------------- 3 | #include "widget.h" 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | // #include "gadget.h" 10 | // #include "widget.h" // in "widget.cpp" 11 | struct Widget::Impl { // as before 12 | std::string name; 13 | std::vector data; 14 | // Gadget g1, g2, g3; 15 | }; 16 | Widget::Widget() // per Item 21, create 17 | : pImpl(std::make_unique()) // std::unique_ptr 18 | {} 19 | 20 | Widget::~Widget() = default; // ~Widget definition 21 | Widget::Widget(Widget&& rhs) = default; // defini- 22 | Widget& Widget::operator=(Widget&& rhs) = default; // tions 23 | -------------------------------------------------------------------------------- /src/iterator/main2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | { 8 | // std::vector v = {1, 2, 3, 4, 5}; 9 | // for (int i = 0; i < v.size(); i++) { 10 | // v[i] = v[i] + 10; 11 | // } 12 | 13 | std::set s = {1, 2, 3, 4, 5}; 14 | // for (int i = 0; i < s.size(); i++) { 15 | // s[i] = s[i] + 10; 16 | // } 17 | 18 | // for (auto it = v.begin(); it != v.end(); ++it) { 19 | // *it += *it; 20 | // } 21 | 22 | // for (auto &n : v) { 23 | // n += 10; 24 | // } 25 | 26 | for (auto n : s) { 27 | std::cout << "n: " << n << std::endl; 28 | } 29 | 30 | for ( auto&n : s) { 31 | n += 10; 32 | } 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /src/references/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = [ 7 | "//src/lib:SolutionLib", 8 | "//src/lib:utility", 9 | ], 10 | ) 11 | 12 | cc_binary( 13 | name = "references_in_loops_main", 14 | srcs = ["references_in_loops_main.cc"], 15 | deps = [ 16 | "//src/lib:SolutionLib", 17 | "//src/lib:utility", 18 | ], 19 | ) 20 | 21 | cc_binary( 22 | name = "pointer_vs_reference_main", 23 | srcs = ["pointer_vs_reference_main.cc"], 24 | deps = [ 25 | "//src/lib:SolutionLib", 26 | "//src/lib:utility", 27 | ], 28 | ) 29 | 30 | 31 | cc_binary( 32 | name = "returning_handles", 33 | srcs = ["returning_handles.cc"], 34 | ) -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") 2 | 3 | git_repository( 4 | name = "com_google_googletest", 5 | #// Dec 27, 2021 6 | commit = "1b18723e874b256c1e39378c6774a90701d70f7a", 7 | remote = "https://github.com/google/googletest", 8 | # tag = "release-1.11.0", 9 | ) 10 | 11 | git_repository( 12 | name = "com_google_benchmark", 13 | remote = "https://github.com/google/benchmark.git", 14 | tag = "v1.5.1", 15 | ) 16 | 17 | git_repository( 18 | name = "com_google_absl", 19 | remote = "https://github.com/abseil/abseil-cpp.git", 20 | tag = "20200225.2", 21 | ) 22 | 23 | new_local_repository( 24 | name = "usr_local", 25 | build_file = "third_party/usr_local.BUILD", 26 | path = "/usr/local", 27 | ) 28 | -------------------------------------------------------------------------------- /src/cpp14/generic_lambda.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int adder(int a, int b) { return a + b; } 6 | int main() { 7 | { 8 | std::vector ivec = {1, 2, 3, 4}; 9 | std::vector svec = {"red", "green", "blue"}; 10 | 11 | // auto adder = [](auto op1, auto op2) { return op1 + op2; }; 12 | 13 | std::cout << "int result : " 14 | << std::accumulate(ivec.begin(), ivec.end(), 0, adder) 15 | << std::endl; 16 | 17 | std::cout << "string result : " 18 | << std::accumulate( 19 | svec.begin(), svec.end(), std::string(""), 20 | [](auto op1, auto op2) { return op1 + op2; } ) 21 | << std::endl; 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/bishop_moves.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "src/lib/solution.h" 5 | 6 | int main() { 7 | int x, y; 8 | 9 | std::cout << "Input x coordinate: "; 10 | std::cin >> x; 11 | 12 | std::cout << "Input y coordinate: "; 13 | std::cin >> y; 14 | 15 | int array[8][8]; 16 | 17 | for (int i = 0; i < 8; i++) { 18 | for (int j = 0; j < 8; j++) { 19 | if ((i - x == j - y) || (i - x == -(j - y))) { 20 | array[i][j] = 1; 21 | } else { 22 | array[i][j] = 0; 23 | } 24 | } 25 | } 26 | 27 | array[x][y] = 2; 28 | 29 | for (int j = 7; j >= 0; j--) { 30 | for (int i = 0; i < 8; i++) { 31 | std::cout << array[i][j]; 32 | } 33 | std::cout << std::endl; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /src/pointers/deep_copy_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | using namespace std; 5 | class Student_deep { 6 | public: 7 | 8 | Student_deep() { id = new int(0); } 9 | Student_deep(const Student_deep& rhs) { 10 | // 11 | id = new int(*(rhs.id)); 12 | } 13 | 14 | Student_deep& operator=(const Student_deep& rhs) { 15 | // 16 | id = new int(*(rhs.id)); 17 | return *this; 18 | } 19 | ~Student_deep() { 20 | delete id; 21 | id = nullptr; 22 | cout << "Delete Student_deep!" << endl; 23 | } 24 | 25 | int* id; 26 | }; 27 | 28 | int main() { 29 | Student_deep a; 30 | Student_deep b = a; 31 | Student_deep c; 32 | c = a; 33 | cout << *a.id << *b.id << *c.id << endl; 34 | *c.id = 1; 35 | cout << *a.id << *b.id << *c.id << endl; 36 | } -------------------------------------------------------------------------------- /src/hash/std_hash_main.cc: -------------------------------------------------------------------------------- 1 | // hash example 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | char nts1[] = "Test"; 8 | char nts2[] = "Test"; 9 | std::string str1(nts1); 10 | std::string str2(nts2); 11 | 12 | std::hash ptr_hash; 13 | std::hash str_hash; 14 | 15 | std::cout << "same hashes:\n" << std::boolalpha; 16 | std::cout << "nts1 and nts2: " << (ptr_hash(nts1) == ptr_hash(nts2)) << '\n'; 17 | std::cout << "str1 and str2: " << (str_hash(str1) == str_hash(str2)) << '\n'; 18 | std::cout << "ptr_hash(nts1): " << ptr_hash(nts1) << std::endl; 19 | 20 | std::cout << "str_hash(str1): " << str_hash(str1) << std::endl; 21 | std::cout << "str_hash(str2): " << str_hash(str2) << std::endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /src/pointers/shallow_copy_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | class Student_shallow { 5 | public: 6 | Student_shallow() { id = new int(0); } 7 | Student_shallow(const Student_shallow& rhs) { 8 | // 9 | id = rhs.id; 10 | } 11 | 12 | Student_shallow &operator=(const Student_shallow& rhs) { 13 | // 14 | id = rhs.id; 15 | return *this; 16 | } 17 | 18 | ~Student_shallow() { 19 | delete id; 20 | id = nullptr; 21 | cout << "Delete Student_shallow!" << endl; 22 | } 23 | 24 | int* id; 25 | }; 26 | 27 | int main() { 28 | Student_shallow a; 29 | Student_shallow b = a; 30 | Student_shallow c; 31 | c = a; 32 | cout << *a.id << *b.id << *c.id << endl; 33 | *(c.id) = 1; 34 | cout << *a.id << *b.id << *c.id << endl; 35 | } -------------------------------------------------------------------------------- /src/templates/enable_if.cc: -------------------------------------------------------------------------------- 1 | // enable_if example: two ways of using enable_if 2 | #include 3 | #include 4 | 5 | // 1. the return type (bool) is only valid if T is an integral type: 6 | template 7 | typename std::enable_if::value, bool>::type is_odd(T i) { 8 | return bool(i % 2); 9 | } 10 | 11 | template 12 | typename std::enable_if::value, float>::type 13 | return_float(T i) { 14 | return float(i/2); 15 | } 16 | 17 | int main() { 18 | int i = 1; // code does not compile if type of i is not integral 19 | 20 | std::cout << std::boolalpha; 21 | std::cout << "i is odd: " << is_odd(i) << std::endl; 22 | 23 | std::cout << "return_float(2.0f): " << return_float(2.55f) << std::endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /src/hash/set_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | std::set persons; 8 | 9 | persons.insert("Zane"); 10 | persons.insert("Tom"); 11 | persons.insert("Alice"); 12 | persons.insert("Bob"); 13 | persons.insert("Ted"); 14 | persons.insert("Ted"); 15 | persons.insert("Ted"); 16 | persons.insert("Ted"); 17 | persons.insert("Ted"); 18 | persons.insert("Ted"); 19 | 20 | std::cout << "persons.size: " << persons.size() << std::endl; 21 | for (auto& p : persons) { 22 | std::cout << "p: " << p << std::endl; 23 | } 24 | 25 | std::cout << "persons.count(\"Ted\"): " << persons.count("Ted") << std::endl; 26 | std::cout << "persons.count(\"Ari\"): " << persons.count("Ari") << std::endl; 27 | } 28 | -------------------------------------------------------------------------------- /src/inheritance/override.cc: -------------------------------------------------------------------------------- 1 | #include 2 | class Base { 3 | public: 4 | virtual void mf1() const; 5 | virtual void mf2(int x); 6 | virtual void mf3() &; 7 | void mf4() const; 8 | }; 9 | class Derived1 : public Base { 10 | public: 11 | virtual void mf1(); 12 | virtual void mf2(unsigned int x); 13 | virtual void mf3() &&; 14 | void mf4() const; 15 | }; 16 | 17 | class Derived2 : public Base { 18 | public: 19 | virtual void mf1() override; 20 | virtual void mf2(unsigned int x) override; 21 | virtual void mf3() && override; 22 | virtual void mf4() const override; 23 | }; 24 | 25 | class Derived3 : public Base { 26 | public: 27 | virtual void mf1() const override; 28 | virtual void mf2(int x) override; 29 | virtual void mf3() & override; 30 | void mf4() const; 31 | }; -------------------------------------------------------------------------------- /src/multi_threading/main_vector.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "src/lib/utility.h" 8 | 9 | int main() { 10 | const int number_of_threads = 2; 11 | const int step = 1000 * 1000 * 1000 / number_of_threads; 12 | std::vector partial_sums(number_of_threads); 13 | 14 | std::thread t1(CalculateShort, std::ref(partial_sums[0]), 0, step); 15 | std::thread t2(CalculateShort, std::ref(partial_sums[1]), step, 2 * step + 1); 16 | 17 | t1.join(); 18 | t2.join(); 19 | 20 | uint64_t total = 21 | std::accumulate(partial_sums.begin(), partial_sums.end(), uint64_t(0)); 22 | Print(partial_sums); 23 | std::cout << "total: " << total << std::endl; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /src/multi_threading/vector_of_threads_async_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "src/lib/utility.h" 10 | 11 | int main() { 12 | const int number_of_threads = 10; 13 | uint64_t number_of_elements = 1000 * 1000 * 1000; 14 | uint64_t step = number_of_elements / number_of_threads; 15 | std::vector> tasks; 16 | 17 | for (uint64_t i = 0; i < number_of_threads; i++) { 18 | tasks.push_back(std::async(GetRangeSum, i * step, (i + 1) * step)); 19 | } 20 | 21 | uint64_t total = 0; 22 | for (auto &t : tasks) { 23 | total += t.get(); 24 | } 25 | 26 | std::cout << "total: " << total << std::endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /src/modifiers/constants_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | std::cout << "INT8_MIN :" << INT8_MIN << std::endl; 5 | std::cout << "INT8_MAX :" << INT8_MAX << std::endl; 6 | 7 | std::cout << "INT16_MIN:" << INT16_MIN << std::endl; 8 | std::cout << "INT16_MAX:" << INT16_MAX << std::endl; 9 | 10 | std::cout << "INT32_MIN:" << INT32_MIN << std::endl; 11 | std::cout << "INT32_MAX:" << INT32_MAX << std::endl; 12 | 13 | std::cout << "INT64_MIN:" << INT64_MIN << std::endl; 14 | std::cout << "INT64_MAX:" << INT64_MAX << std::endl; 15 | 16 | std::cout << "UINT8_MAX:" << UINT8_MAX << std::endl; 17 | std::cout << "UINT16_MAX:" << UINT16_MAX << std::endl; 18 | std::cout << "UINT32_MAX:" << UINT32_MAX << std::endl; 19 | std::cout << "UINT64_MAX:" << UINT64_MAX << std::endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /src/string_view/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | ) 7 | 8 | cc_binary( 9 | name = "string_view_benchmark", 10 | srcs = ["string_view_benchmark.cc"], 11 | deps = [ 12 | "//src/lib:utility", 13 | "@com_google_benchmark//:benchmark", 14 | ], 15 | ) 16 | cc_binary( 17 | name = "string_view_remove_prefix_benchmark", 18 | srcs = ["string_view_remove_prefix_benchmark.cc"], 19 | deps = [ 20 | "//src/lib:utility", 21 | "@com_google_benchmark//:benchmark", 22 | ], 23 | ) 24 | 25 | cc_binary( 26 | name = "string_view_first", 27 | srcs = ["string_view_first.cc"], 28 | deps = [ 29 | "//src/lib:utility", 30 | "@com_google_benchmark//:benchmark", 31 | ], 32 | ) -------------------------------------------------------------------------------- /src/graph/set_of_set.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | struct Vertex { 10 | Vertex(int v, std::set a) : vertex_number(v), adjacents(a) {} 11 | int vertex_number; 12 | std::set adjacents; 13 | }; 14 | 15 | struct VertextLessThan { 16 | bool operator()(const Vertex &a, const Vertex &b) const { 17 | return (a.vertex_number < b.vertex_number); 18 | } 19 | }; 20 | 21 | class Graph { 22 | public: 23 | Graph(std::set v) : v_(v) {} 24 | std::set v_; 25 | }; 26 | 27 | int main() { 28 | Graph v({Vertex(1, {2, 3}), Vertex(2, {1, 3, 4, 5}), Vertex(3, {1, 2, 4}), 29 | Vertex(4, {2, 4, 4})}); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /src/inheritance/hiding.cc: -------------------------------------------------------------------------------- 1 | class Base { 2 | private: 3 | int x; 4 | 5 | public: 6 | virtual void mf1() = 0; 7 | virtual void mf1(int); 8 | virtual void mf2(); 9 | void mf3(); 10 | void mf3(double); 11 | }; 12 | class Derived : public Base { 13 | public: 14 | // using Base::mf1; // make all things in Base named mf1 and mf3 15 | // using Base::mf3; // visible (and public) in Derived's scope 16 | virtual void mf1(); 17 | void mf3(); 18 | void mf4(); 19 | }; 20 | 21 | int main(int argc, char const *argv[]) { 22 | Derived d; 23 | int x; 24 | d.mf1(); // fine, calls Derived::mf1 25 | d.mf1(x); // error! Derived::mf1 hides Base::mf1 26 | d.mf2(); // fine, calls Base::mf2 27 | d.mf3(); // fine, calls Derived::mf3 28 | d.mf3(x); // error! Derived::mf3 hides Base::mf3 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /src/memory_allocator/global_new_delete.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | // replacement of a minimal set of functions: 5 | void* operator new( 6 | std::size_t sz) // no inline, required by [replacement.functions]/3 7 | { 8 | std::printf("global op new called, size = %zu\n", sz); 9 | if (sz == 0) 10 | ++sz; // avoid std::malloc(0) which may return nullptr on success 11 | 12 | if (void* ptr = std::malloc(sz)) return ptr; 13 | 14 | throw std::bad_alloc{}; // required by [new.delete.single]/3 15 | } 16 | void operator delete(void* ptr) noexcept { 17 | std::puts("global op delete called"); 18 | std::free(ptr); 19 | } 20 | int main() { 21 | int* p1 = new int; 22 | delete p1; 23 | 24 | int* p2 = new int[10]; // guaranteed to call the replacement in C++11 25 | delete[] p2; 26 | } -------------------------------------------------------------------------------- /src/templates/static_assert.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int MyFunction(int i) { 4 | if (i < 0) { 5 | std::cout << "Error, i has to be non-negative" << std::endl; 6 | return -1; 7 | } 8 | return i + 1; 9 | } 10 | 11 | template 12 | class A { 13 | static_assert(size >= 20, "size has to be greater than or equal 20"); 14 | 15 | public: 16 | int get_size() { return size; } 17 | }; 18 | 19 | template 20 | class B { 21 | // Assert T is a character only 22 | static_assert(is_char::value, 23 | "Don't use this template with char."); 24 | }; 25 | 26 | int main() { 27 | A<21> a1; 28 | // A<19> a2; 29 | 30 | std::cout << "a1.get_size(): " << a1.get_size() << std::endl; 31 | // std::cout << "a2.get_size(): " << a2.get_size() << std::endl; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /src/result_of/invoke.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Foo { 5 | Foo(int num) : num_(num) {} 6 | void print_add(int i) const { std::cout << num_ + i << '\n'; } 7 | int num_; 8 | }; 9 | 10 | void print_num(int i) { std::cout << i << '\n'; } 11 | 12 | struct PrintNum { 13 | void operator()(int i) const { std::cout << i << '\n'; } 14 | }; 15 | 16 | int main() { 17 | // invoke a free function 18 | std::invoke(print_num, -9); 19 | 20 | // invoke a lambda 21 | std::invoke([]() { print_num(42); }); 22 | 23 | // invoke a member function 24 | const Foo foo(314159); 25 | std::invoke(&Foo::print_add, foo, 1); 26 | 27 | // invoke (access) a data member 28 | std::cout << "num_: " << std::invoke(&Foo::num_, foo) << '\n'; 29 | 30 | // invoke a function object 31 | std::invoke(PrintNum(), 18); 32 | } -------------------------------------------------------------------------------- /src/scope/namespace_extend_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | // namespaces 6 | #include 7 | int x = 0; 8 | void PrintSomething() { std::cout << "Printing example 0." << std::endl; } 9 | 10 | namespace ns1 { 11 | int x = 1; 12 | void PrintSomething() { std::cout << "Printing example 1." << std::endl; } 13 | 14 | } // namespace ns1 15 | 16 | namespace ns1 { 17 | int x = 2; 18 | void PrintSomething2() { std::cout << "Printing example 2." << std::endl; } 19 | } // namespace ns1 20 | 21 | int main() { 22 | std::cout << "ns1::x: " << ns1::x << std::endl; 23 | std::cout << "ns2::x: " << ns2::x << std::endl; 24 | std::cout << "::x: " << ::x << std::endl; 25 | 26 | ns1::PrintSomething(); 27 | ns1::PrintSomething2(); 28 | ::PrintSomething(); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /src/stack/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | // Empty stack 7 | std::stack s; 8 | 9 | if (!s.empty()) { 10 | int r = s.top(); 11 | s.pop(); // Don't do this! 12 | } 13 | 14 | { 15 | std::stack s; 16 | 17 | std::cout << "s.size(): " << s.size() << std::endl; 18 | // int r = s.top(); 19 | // std::cout << "r: " << r << std::endl; 20 | 21 | s.push(1); 22 | std::cout << "s.top(): " << s.top() << std::endl; 23 | 24 | s.push(2); 25 | std::cout << "s.top(): " << s.top() << std::endl; 26 | 27 | s.pop(); 28 | std::cout << "s.top(): " << s.top() << std::endl; 29 | } 30 | 31 | // OMG! There is not begin and end! 32 | // Can't do this: 33 | // for (auto it = s.begin(); it != s.end(); ++it) { 34 | // } 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /src/dynamic_programming/coin_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | 7 | int coinChange(std::vector &coins, int amount) { 8 | if (amount == 0) { 9 | return 0; 10 | } 11 | 12 | if (amount < 0) { 13 | return -1; 14 | } 15 | 16 | int min = INT_MAX; 17 | for (auto coin : coins) { 18 | int n = coinChange(coins, amount - coin); 19 | if (n >= 0) { 20 | min = std::min(n, min); 21 | } 22 | } 23 | 24 | if (min >= 0 && min < INT_MAX) { 25 | return 1 + min; 26 | } else { 27 | return -1; 28 | } 29 | } 30 | 31 | int main() { 32 | std::vector coins = {84, 457, 478, 309, 350, 349, 33 | 422, 469, 100, 432, 188}; 34 | int amount = 5000; 35 | int n = coinChange(coins, amount); 36 | std::cout << "n: " << n << std::endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /src/inheritance/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | cc_binary( 9 | name = "virtual", 10 | srcs = ["virtual.cc"], 11 | deps = ["//src/lib:SolutionLib"], 12 | ) 13 | 14 | cc_binary( 15 | name = "final", 16 | srcs = ["final.cc"], 17 | ) 18 | 19 | cc_binary( 20 | name = "call_virtual_during_ctr", 21 | srcs = ["call_virtual_during_ctr.cc"], 22 | ) 23 | 24 | cc_binary( 25 | name = "base_pointer", 26 | srcs = ["base_pointer.cc"], 27 | ) 28 | 29 | cc_binary( 30 | name = "pure_virtual_function", 31 | srcs = ["pure_virtual_function.cc"], 32 | ) 33 | 34 | cc_binary( 35 | name = "virtual_default_value", 36 | srcs = ["virtual_default_value.cc"], 37 | ) 38 | 39 | cc_binary( 40 | name = "override", 41 | srcs = ["override.cc"], 42 | ) -------------------------------------------------------------------------------- /src/templates/is_same.cc: -------------------------------------------------------------------------------- 1 | template 2 | struct boolean { 3 | static const bool value = set; 4 | }; 5 | 6 | using true_type = boolean; 7 | using false_type = boolean; 8 | //----------------------------------------------------- 9 | template 10 | struct is_same : false_type {}; 11 | 12 | // Specialization 13 | template 14 | struct is_same : true_type {}; 15 | //----------------------------------------------------- 16 | // Note: T1 and T2 should be the same! 17 | template 18 | class A { 19 | static_assert(is_same::value, "T1 and T2 have to be the same!"); 20 | }; 21 | 22 | //----------------------------------------------------- 23 | int main() { 24 | A a_char_char; // Ok 25 | // A a_char_int; // Generates compile error 😃 26 | } 27 | -------------------------------------------------------------------------------- /src/pointers/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = ["//src/lib:SolutionLib"], 7 | ) 8 | 9 | cc_binary( 10 | name = "unsafe_pointers_main", 11 | srcs = ["unsafe_pointers_main.cc"], 12 | deps = ["//src/lib:SolutionLib"], 13 | ) 14 | 15 | cc_binary( 16 | name = "heap_main", 17 | srcs = ["heap_main.cc"], 18 | deps = ["//src/lib:SolutionLib"], 19 | ) 20 | 21 | cc_binary( 22 | name = "shallow_copy_main", 23 | srcs = ["shallow_copy_main.cc"], 24 | ) 25 | 26 | cc_binary( 27 | name = "deep_copy_main", 28 | srcs = ["deep_copy_main.cc"], 29 | ) 30 | 31 | cc_binary( 32 | name = "set_new_handler", 33 | srcs = ["set_new_handler.cc"], 34 | ) 35 | 36 | cc_binary( 37 | name = "pointer_misuse", 38 | srcs = ["pointer_misuse.cc"], 39 | ) 40 | -------------------------------------------------------------------------------- /src/hash/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | deps = ["//src/lib:SolutionLib"], 6 | ) 7 | 8 | 9 | cc_binary( 10 | name = "index_function_main", 11 | srcs = ["index_function_main.cc"], 12 | deps = ["//src/lib:SolutionLib"], 13 | ) 14 | 15 | cc_binary( 16 | name = "map_main", 17 | srcs = ["map_main.cc"], 18 | deps = ["//src/lib:SolutionLib"], 19 | ) 20 | 21 | cc_binary( 22 | name = "pair_main", 23 | srcs = ["pair_main.cc"], 24 | deps = ["//src/lib:SolutionLib"], 25 | ) 26 | 27 | cc_binary( 28 | name = "set_main", 29 | srcs = ["set_main.cc"], 30 | deps = ["//src/lib:SolutionLib"], 31 | ) 32 | 33 | 34 | cc_binary( 35 | name = "std_hash_main", 36 | srcs = ["std_hash_main.cc"], 37 | deps = ["//src/lib:SolutionLib"], 38 | ) 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/templates/traits.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | // Note: T has to be an integral type (int, short, long, ...) 7 | template 8 | class MyTest { 9 | static_assert(std::is_integral::value, "Not an int!"); 10 | }; 11 | 12 | //----------------------------------------------------- 13 | // Note: T has to be either float or integral or enum. 14 | template 15 | void Write(T inData, uint32_t inBitCount = sizeof(T) * 8) 16 | { 17 | static_assert(std::is_arithmetic::value || 18 | std::is_enum::value, 19 | "Generic Write only supports primitive data types" ); 20 | // WriteBits(&inData, inBitCount); 21 | } 22 | 23 | //----------------------------------------------------- 24 | 25 | int main() { 26 | MyTest test_var; 27 | char b=0; 28 | Write(b); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /src/command_line/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | deps = ["//src/lib:utility"], 7 | ) 8 | 9 | cc_binary( 10 | name = "main_dash_dash", 11 | srcs = ["main_dash_dash.cc"], 12 | deps = ["//src/lib:utility"], 13 | ) 14 | 15 | cc_binary( 16 | name = "abseil_main", 17 | srcs = ["abseil_main.cc"], 18 | deps = [ 19 | "@com_google_absl//absl/flags:flag", 20 | "@com_google_absl//absl/flags:parse", 21 | "@com_google_absl//absl/flags:usage", 22 | ], 23 | ) 24 | 25 | 26 | cc_binary( 27 | name = "abseil_main_user", 28 | srcs = ["abseil_main_user.cc"], 29 | deps = [ 30 | "@com_google_absl//absl/flags:flag", 31 | "@com_google_absl//absl/flags:parse", 32 | "@com_google_absl//absl/flags:usage", 33 | ], 34 | ) 35 | 36 | -------------------------------------------------------------------------------- /src/control/if_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | 6 | int value; 7 | std::cout << "Please enter a value: " << std::endl; 8 | std::cin >> value; 9 | 10 | if (value > 10) { 11 | std::cout << "It was GREATER than 10! " << std::endl; 12 | } else { 13 | std::cout << "It was LESS than or EQUAL to 10! " << std::endl; 14 | } 15 | 16 | // Nested if 17 | std::cout << "Please enter a value: " << std::endl; 18 | std::cin >> value; 19 | 20 | if (value > 10) { 21 | std::cout << "It was GREATER than 10! " << std::endl; 22 | } else if (value == 10) { 23 | std::cout << "It was EQUAL to 10! " << std::endl; 24 | } else { 25 | std::cout << "It was LESS than 10! " << std::endl; 26 | } 27 | 28 | bool b = false; 29 | if (b = true) { 30 | std::cout << "b is true" << std::endl; 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /src/pointers/unsafe_pointers_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int i = 10; 6 | int j = 20; 7 | 8 | // Uninitialized 9 | int *q; 10 | // std::cout << "*q: " << *q << std::endl; // <------- Unsafe! 11 | 12 | // Modifying the value of the pointer 13 | q = &i; 14 | q++; 15 | // std::cout << "*q: " << *q << std::endl; // <------- Unsafe! 16 | 17 | // Using a deleted pointer 18 | int *r = new int; 19 | *r = 5; 20 | std::cout << "*r: " << *r << std::endl; 21 | 22 | delete r; 23 | 24 | std::cout << "*r: " << *r << std::endl; // Unsafe! 25 | 26 | // Deleting a pointer twice has undefined behavior! 27 | delete r; 28 | 29 | 30 | // Memory leak 31 | { 32 | int *p; 33 | p = new int(5); 34 | std::cout << "p: " << p << std::endl; 35 | } 36 | // p does not have scope here! 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /src/function/std_function.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int BinaryOperation(int a, int b, std::function func) { 10 | return func(a, b); 11 | } 12 | 13 | int Add(int a, int b) { return a + b; } 14 | int Mult(int a, int b) { return a * b; } 15 | 16 | int main() { 17 | std::vector> operators = {Add, Mult}; 18 | 19 | int choice = 0; 20 | 21 | while (true) { 22 | std::cout << "Enter operation on 10, 20: 0: Add, 1:Mult" << std::endl; 23 | std::cin >> choice; 24 | if (choice >= operators.size()) { 25 | break; 26 | } 27 | int result = BinaryOperation(10, 20, operators[choice]); 28 | 29 | std::cout << "result: " << result << std::endl; 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /src/dynamic_programming/knap_01_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | int knapSack01(std::vector &weights, std::vector &values, int i, 8 | int w) { 9 | if (i == -1 || w == 0) { 10 | return 0; 11 | } 12 | 13 | // If wi>w, item i is definitely not included 14 | if (weights[i] > w) { 15 | return knapSack01(weights, values, i - 1, w); 16 | } 17 | 18 | return std::max( 19 | knapSack01(weights, values, i - 1, w), 20 | values[i] + knapSack01(weights, values, i - 1, w - weights[i])); 21 | } 22 | 23 | int main() { 24 | std::vector values = {1, 10, 15, 20}; 25 | std::vector weights = {1, 10, 15, 15}; 26 | int w = 25; 27 | 28 | 29 | int n = knapSack01(weights, values, values.size() - 1, w); 30 | std::cout << "n: " << n << std::endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /src/opreators/float_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | 5 | int main() { 6 | 7 | float f = 0.25; // A float literal 8 | double d = 0.24; // A double literal 9 | 10 | std::cout << "f: " << f << std::endl; 11 | std::cout << "d: " << d << std::endl; 12 | 13 | int i = 5; 14 | int j = 2; 15 | 16 | float fraction = i / j; // Right hand side is converted to int 17 | std::cout << "fraction: " << fraction << std::endl; 18 | 19 | fraction = (float)i / j; // Right hand side is float 20 | std::cout << "fraction: " << fraction << std::endl; 21 | 22 | std::cout << "Size of float: " << sizeof(float) << " byte" << std::endl; 23 | 24 | std::cout << "Size of double: " << sizeof(double) << " byte" << std::endl; 25 | std::cout << "Size of long double: " << sizeof(long double) << " byte" 26 | << std::endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /src/variables/float_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | 5 | int main() { 6 | 7 | float f = 0.25; // A float literal 8 | double d = 0.24; // A double literal 9 | 10 | std::cout << "f: " << f << std::endl; 11 | std::cout << "d: " << d << std::endl; 12 | 13 | int i = 5; 14 | int j = 2; 15 | 16 | float fraction = i / j; // Right hand side is converted to int 17 | std::cout << "fraction: " << fraction << std::endl; 18 | 19 | fraction = (float)i / j; // Right hand side is float 20 | std::cout << "fraction: " << fraction << std::endl; 21 | 22 | std::cout << "Size of float: " << sizeof(float) << " byte" << std::endl; 23 | 24 | std::cout << "Size of double: " << sizeof(double) << " byte" << std::endl; 25 | std::cout << "Size of long double: " << sizeof(long double) << " byte" 26 | << std::endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /tests/vector_union_test.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/vector_union.h" 2 | 3 | #include 4 | 5 | #include "gtest/gtest.h" 6 | 7 | TEST(VectorUnion, ShouldHandleEmptySetA) { 8 | VectorUnionSolution s; 9 | std::vector a = {}, b = {1, 2, 3, 4}; 10 | std::vector actual = s.Calculate(a, b); 11 | std::vector expected = b; 12 | EXPECT_EQ(expected, actual); 13 | } 14 | 15 | TEST(VectorUnion, ShouldHandleEmptySetAB) { 16 | VectorUnionSolution s; 17 | std::vector a = {}, b = {}; 18 | std::vector actual = s.Calculate(a, b); 19 | std::vector expected = b; 20 | EXPECT_EQ(expected, actual); 21 | } 22 | 23 | TEST(VectorUnion, ShouldHandleUnion) { 24 | VectorUnionSolution s; 25 | std::vector a = {1, 2, 3}, b = {3, 54, 6}; 26 | std::vector actual = s.Calculate(a, b); 27 | std::vector expected = {1, 2, 3, 6, 54}; 28 | EXPECT_EQ(expected, actual); 29 | } -------------------------------------------------------------------------------- /src/lib/factory/img_create_map.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class Image {}; 6 | class BitmapImage : public Image {}; 7 | class PngImage : public Image {}; 8 | class JpgImage : public Image {}; 9 | 10 | struct IImageFactory { 11 | virtual std::shared_ptr Create(std::string_view type) = 0; 12 | }; 13 | 14 | struct ImageFactory : public IImageFactory { 15 | virtual std::shared_ptr Create(std::string_view type) override { 16 | static std::map()>> 17 | mapping{{"bmp", []() { return std::make_shared(); }}, 18 | {"png", []() { return std::make_shared(); }}, 19 | {"jpg", []() { return std::make_shared(); }}}; 20 | auto it = mapping.find(type.data()); 21 | if (it != mapping.end()) return it->second(); 22 | return nullptr; 23 | } 24 | }; -------------------------------------------------------------------------------- /src/lib/graph/pointer.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_H 2 | #define TREE_H 3 | #include 4 | #include 5 | 6 | enum class NodeStatus { NotVisited, Visiting, Visited }; 7 | 8 | struct GraphNode { 9 | int val; 10 | NodeStatus status; 11 | std::vector children; 12 | GraphNode(int v) : val(v) { status = NodeStatus::NotVisited; } 13 | GraphNode(int v, std::vector &c) : val(v), children(c) {} 14 | }; 15 | 16 | class PointerGraph { 17 | public: 18 | PointerGraph() {} 19 | // Returns true if a cycle is reachable from start_node 20 | bool ReachesACycle(GraphNode *start_node); 21 | 22 | // Returns true if the graph has a cycle 23 | bool HasCycle(); 24 | 25 | // Resets the status of all nodes to NodeStatus::NotVisited 26 | void ResetStatus(); 27 | 28 | bool empty(); 29 | int size(); 30 | void CreateSampleGraph1(); 31 | std::vector _nodes; 32 | }; 33 | 34 | #endif -------------------------------------------------------------------------------- /src/passing_variables/pass_by_reference_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Swaps values of i and j 5 | void SwapUsingReference(int &i, int &j) { 6 | int temp = i; 7 | i = j; 8 | j = temp; 9 | } 10 | 11 | bool MultiplyByTwoAndThree(int i, int &mult_2, int &mult_3) { 12 | if (i >= INT32_MAX / 2) { 13 | return false; 14 | } 15 | mult_2 = i * 2; 16 | mult_3 = i * 3; 17 | return true; 18 | } 19 | 20 | int main() { 21 | 22 | int i = 1; 23 | int j = 2; 24 | std::cout << "i: " << i << ", j: " << j << std::endl; 25 | 26 | SwapUsingReference(i, j); 27 | 28 | std::cout << "i: " << i << ", j: " << j << std::endl; 29 | 30 | int x = 2; 31 | int x_mult_2, x_mult_3; 32 | MultiplyByTwoAndThree(x, x_mult_2, x_mult_3); 33 | std::cout << "x: " << x << ", x_mult_2: " << x_mult_2 34 | << ", x_mult_3: " << x_mult_3 << std::endl; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /src/function/transform.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/lib/utility.h" 9 | 10 | int IncrementByTen(int a) { return a + 10; } 11 | int MathFunction(int a) { return a ^ 2 + 10; } 12 | 13 | int main() { 14 | std::vector inputs = {0, 1, 2, 3, 4, 5, 6, 7, 8}; 15 | std::vector outputs(inputs.size()); 16 | 17 | // result = {0, 10, 20, 30, 40, 50, 60 70,80} 18 | 19 | // map input -> result 20 | 21 | // Increment all of them 22 | std::transform(inputs.begin(), inputs.end(), outputs.begin(), IncrementByTen); 23 | 24 | Print(outputs); 25 | 26 | std::transform(inputs.begin(), inputs.end(), outputs.begin(), MathFunction); 27 | Print(outputs); 28 | 29 | std::transform(inputs.begin(), inputs.end(), inputs.begin(), IncrementByTen); 30 | Print(inputs); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /src/inheritance/virtual.cc: -------------------------------------------------------------------------------- 1 | #include 2 | struct Animal { 3 | virtual ~Animal() = default; 4 | virtual void Eat() { std::cout << "Animal Eat" << std::endl; } 5 | }; 6 | 7 | struct Mammal : virtual Animal { 8 | virtual void Breathe() {} 9 | }; 10 | 11 | struct WingedAnimal : virtual Animal { 12 | virtual void Flap() {} 13 | }; 14 | 15 | // A bat is a winged mammal 16 | struct Bat : Mammal, WingedAnimal {}; 17 | 18 | int main() { 19 | { 20 | Bat bat; 21 | bat.Eat(); // Won't compile unless virtual inheritance. 22 | } 23 | 24 | { 25 | Bat bat; 26 | Animal& a = bat; // Won't compile unless virtual inheritance. 27 | } 28 | 29 | { 30 | // Compile without virtual inheritance. 31 | Bat b; 32 | Animal& mammal = static_cast(b); 33 | mammal.Eat(); 34 | Animal& winged = static_cast(b); 35 | winged.Eat(); 36 | } 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /src/templates/is_char.cc: -------------------------------------------------------------------------------- 1 | template 2 | struct boolean { 3 | static const bool value = set; 4 | }; 5 | 6 | using true_type = boolean; 7 | using false_type = boolean; 8 | // typedef boolean true_type; 9 | 10 | //----------------------------------------------------- 11 | template 12 | struct is_char : false_type {}; 13 | // is_char::value = false (for everything!) 14 | 15 | // Specialized for char 16 | template <> 17 | struct is_char : true_type {}; 18 | // is_char::value = true (if it was given char as parameter) 19 | //----------------------------------------------------- 20 | // Note: T has to be a char! 21 | template 22 | class A { 23 | static_assert(is_char::value, "T has to be char"); 24 | }; 25 | //----------------------------------------------------- 26 | int main() { 27 | // A a_int; // Generates compile error 😃 28 | A a_char; 29 | } 30 | -------------------------------------------------------------------------------- /src/inheritance/pure_virtual_function.cc: -------------------------------------------------------------------------------- 1 | #include 2 | class Shape { 3 | public: 4 | virtual void draw() const = 0; 5 | }; 6 | 7 | // We can still define a definition for draw if we want to! 8 | void Shape::draw() const { std::cout << "Shape::draw" << std::endl; } 9 | 10 | class Rectangle : public Shape { 11 | public: 12 | void draw() const { std::cout << "Rectangle draw" << std::endl; } 13 | }; 14 | class Ellipse : public Shape { 15 | public: 16 | void draw() const { std::cout << "Ellipses draw" << std::endl; } 17 | }; 18 | 19 | int main(int argc, char const *argv[]) { 20 | // Shape *ps = new Shape; // error! Shape is abstract 21 | Shape *ps1 = new Rectangle; // fine 22 | ps1->draw(); // calls Rectangle::draw 23 | 24 | Shape *ps2 = new Ellipse; // fine 25 | ps2->draw(); // calls Ellipse::draw 26 | ps1->Shape::draw(); // calls Shape::draw 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /src/uniform_initialization/initializer_list/variable_arguments.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //----------------------------------------------------- 6 | #include 7 | 8 | int addList(const std::initializer_list& list) { 9 | int retVal = 0; 10 | // Range based for loop 11 | for (auto i : list) { 12 | retVal += i; 13 | } 14 | 15 | return retVal; 16 | } 17 | //----------------------------------------------------- 18 | void printMonths( 19 | const std::initializer_list>& list) { 20 | for (auto i : list) { 21 | std::cout << i.first << ":" << i.second << std::endl; 22 | } 23 | } 24 | 25 | int main(int argc, char const* argv[]) { 26 | std::cout << addList({1, 1, 2, 3, 5, 8, 13}) << std::endl; 27 | printMonths({ 28 | {1, "January"}, {2, "February"}, {3, "March"}, 29 | // ... 30 | }); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /src/dynamic_programming/fib_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Recursive Fibonacci Implementation 5 | int Fibonacci(int n) { 6 | if (n == 0 || n == 1) { 7 | return 1; 8 | } 9 | return Fibonacci(n - 1) + Fibonacci(n - 2); 10 | } 11 | 12 | template 13 | int ExpectEqual(T expected, T actual) { 14 | if (expected == actual) { 15 | std::cout << "PASS: " << expected << " == " << actual << std::endl; 16 | return 0; 17 | } else { 18 | std::cout << "PASS: " << expected << " != " << actual << std::endl; 19 | return 1; 20 | } 21 | } 22 | int main() { 23 | int result = 0; 24 | result += ExpectEqual(Fibonacci(0), 1); 25 | result += ExpectEqual(Fibonacci(1), 1); 26 | result += ExpectEqual(Fibonacci(2), 2); 27 | result += ExpectEqual(Fibonacci(3), 3); 28 | result += ExpectEqual(Fibonacci(4), 5); 29 | std::cout << "Number of failed tests: " << result << std::endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /src/dynamic_programming/lpss_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "src/lib/dynamic_programming/lpss.h" 7 | #include "src/lib/utility.h" 8 | 9 | // Longest Palindromic Subsequence 10 | // Given a string s, find the longest palindromic subsequence's length in s. You 11 | // may assume that the maximum length of s is 1000. 12 | 13 | // Example 1: 14 | // Input: 15 | 16 | // "bbbab" 17 | // Output: 18 | // 4 19 | // One possible longest palindromic subsequence is "bbbb". 20 | // Example 2: 21 | // Input: 22 | 23 | // "cbbd" 24 | // Output: 25 | // 2 26 | // One possible longest palindromic subsequence is "bb". 27 | 28 | // Finds LSCS at index i 29 | 30 | // Driver function 31 | int main() { 32 | std::string in = "bbbab"; 33 | 34 | LPSS_Problem p(in); 35 | auto result = p.LPSS(0, in.size()); 36 | 37 | std::cout << "result: " << result << std::endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /src/memory_allocator/override_new_delete2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class student { 5 | string name; 6 | int age; 7 | 8 | public: 9 | student() { cout << "Constructor is called\n"; } 10 | student(string name, int age) { 11 | this->name = name; 12 | this->age = age; 13 | } 14 | void display() { 15 | cout << "Name:" << name << endl; 16 | cout << "Age:" << age << endl; 17 | } 18 | void* operator new(size_t size) { 19 | cout << "Overloading new operator with size: " << size << endl; 20 | // void *p; 21 | void* p = ::operator new(size); 22 | // void * p = malloc(size); will also work fine 23 | 24 | return p; 25 | } 26 | 27 | void operator delete(void* p) { 28 | cout << "Overloading delete operator " << endl; 29 | free(p); 30 | } 31 | }; 32 | 33 | int main() { 34 | student* p = new student("Yash", 24); 35 | 36 | p->display(); 37 | delete p; 38 | } -------------------------------------------------------------------------------- /src/multi_threading/parallel_fib.cc: -------------------------------------------------------------------------------- 1 | 2 | unsigned serialFib(unsigned n) { 3 | if (n < 2) { 4 | return n; 5 | } else { 6 | return serialFib(n - 2) + serialFib(n - 1); 7 | } 8 | } 9 | 10 | int number_of_cores = 10; 11 | int count = 0; 12 | void parallelFib(unsigned n, unsigned& sum) { 13 | if (n < 2) { 14 | sum = n; 15 | } else { 16 | unsigned x, y; 17 | tbb::parallel_invoke( 18 | [&x, &n, &count]() { 19 | if (count > number_of_cores) { 20 | serialFib(n - 2); 21 | } else { 22 | count++; 23 | parallelFib(n - 2, x); 24 | count--; 25 | } 26 | }, 27 | [&y, &n]() { parallelFib(n - 1, y); }); 28 | sum = x + y; 29 | } 30 | } 31 | 32 | 33 | // // Not ok in STL, but ok in TBB 34 | // In thread 1: v.push_back() 35 | 36 | // In thread 2: v.empty() 37 | 38 | 39 | // This is ok: 40 | // In thread 1: v[1] = 1 41 | // In thread 2: reading v[2] 42 | -------------------------------------------------------------------------------- /src/smart_pointer/stl_unique_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Person { 6 | std::string first_name; 7 | std::string last_name; 8 | int age; 9 | 10 | Person() { std::cout << "Person constructor" << std::endl; } 11 | ~Person() { std::cout << "Person destructor" << std::endl; } 12 | void Talk() { 13 | std::cout << "Hi, my name is: " << first_name + ' ' + last_name 14 | << std::endl; 15 | } 16 | 17 | std::string FullName() { return first_name + ' ' + last_name; } 18 | }; 19 | 20 | void DoStuff(std::unique_ptr p) { p->Talk(); } 21 | 22 | int main() { 23 | // Weak pointer going out of scope before shared pointer 24 | { 25 | std::unique_ptr uptr(new Person()); 26 | 27 | // Can call functions just like a regular pointer! 28 | uptr->first_name = "Ari"; 29 | uptr->last_name = "Saif"; 30 | 31 | uptr->Talk(); 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /src/benchmark/output/search_collection.csv: -------------------------------------------------------------------------------- 1 | name,iterations,real_time,cpu_time,time_unit,bytes_per_second,items_per_second,label,error_occurred,error_message 2 | "BM_SearchInVector/1024",536209,1169.83,1169.39,ns,,,,, 3 | "BM_SearchInVector/2048",609958,1198.17,1195.77,ns,,,,, 4 | "BM_SearchInVector/4096",600225,1174.01,1172.13,ns,,,,, 5 | "BM_SearchInVector/8192",566935,1226.46,1217.76,ns,,,,, 6 | "BM_SearchInVector/16384",431207,1711.17,1692.94,ns,,,,, 7 | "BM_SearchInVector/32768",394293,2030.75,1853.42,ns,,,,, 8 | "BM_SearchInVector/65536",333887,2094.3,2030.36,ns,,,,, 9 | "BM_SearchInList/1024",11048,63807.4,63780.9,ns,,,,, 10 | "BM_SearchInList/2048",5380,127499,127487,ns,,,,, 11 | "BM_SearchInList/4096",2724,257429,257353,ns,,,,, 12 | "BM_SearchInList/8192",1375,505344,505287,ns,,,,, 13 | "BM_SearchInList/16384",645,1.00779e+06,1.00762e+06,ns,,,,, 14 | "BM_SearchInList/32768",258,2.76548e+06,2.76385e+06,ns,,,,, 15 | "BM_SearchInList/65536",125,5.48156e+06,5.47986e+06,ns,,,,, 16 | -------------------------------------------------------------------------------- /src/benchmark/output/split_string.csv: -------------------------------------------------------------------------------- 1 | name,iterations,real_time,cpu_time,time_unit,bytes_per_second,items_per_second,label,error_occurred,error_message 2 | "BM_SplitStackOverflow/1024",9298,73099.4,71916.1,ns,,,,, 3 | "BM_SplitStackOverflow/2048",4926,141594,139229,ns,,,,, 4 | "BM_SplitStackOverflow/4096",2507,274748,270765,ns,,,,, 5 | "BM_SplitStackOverflow/8192",1229,620221,603082,ns,,,,, 6 | "BM_SplitStackOverflow/16384",460,1.26396e+06,1.22816e+06,ns,,,,, 7 | "BM_SplitStackOverflow/32768",307,2.65016e+06,2.5327e+06,ns,,,,, 8 | "BM_SplitStackOverflow/65536",144,5.04111e+06,4.83716e+06,ns,,,,, 9 | "BM_SplitSanjay/1024",18979,38199.7,37528.3,ns,,,,, 10 | "BM_SplitSanjay/2048",9713,83279.9,80284.5,ns,,,,, 11 | "BM_SplitSanjay/4096",4020,160050,155997,ns,,,,, 12 | "BM_SplitSanjay/8192",2286,322125,317238,ns,,,,, 13 | "BM_SplitSanjay/16384",1033,671831,660083,ns,,,,, 14 | "BM_SplitSanjay/32768",505,1.36605e+06,1.34577e+06,ns,,,,, 15 | "BM_SplitSanjay/65536",250,2.795e+06,2.7498e+06,ns,,,,, 16 | -------------------------------------------------------------------------------- /src/hash/index_function_main.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | 6 | struct MyPerson { 7 | MyPerson() : name("No Name"), age(0) {} 8 | MyPerson(const std::string &name_p, int age_p) : name(name_p), age(age_p) {} 9 | std::string name; 10 | int age; 11 | }; 12 | 13 | int IndexFunction(const std::string &name) { 14 | 15 | if (name == "Tom") { 16 | return 0; 17 | } 18 | if (name == "Alice") { 19 | return 1; 20 | } 21 | if (name == "Bob") { 22 | return 2; 23 | } 24 | if (name == "Ted") { 25 | return 3; 26 | } 27 | } 28 | 29 | int main() { 30 | std::vector persons(4); 31 | 32 | persons[0] = MyPerson("Tom", 12); 33 | persons[1] = MyPerson("Alice", 18); 34 | persons[2] = MyPerson("Bob", 15); 35 | persons[3] = MyPerson("Ted", 57); 36 | 37 | std::cout << "Tom's age: " << persons[IndexFunction("Tom")].age << std::endl; 38 | 39 | persons[IndexFunction("Ted")].age; 40 | } -------------------------------------------------------------------------------- /src/references/references_in_loops_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "src/lib/utility.h" 6 | 7 | int main() { 8 | std::vector my_vector = {1, 2, 3, 4, 5, 6, 7, 8}; 9 | 10 | PrintVector(my_vector); 11 | 12 | for (int n : my_vector) { 13 | n++; 14 | std::cout << "n: " << n << std::endl; 15 | } 16 | 17 | PrintVector(my_vector); 18 | 19 | for (auto &n : my_vector) { 20 | n++; 21 | std::cout << "n: " << n << std::endl; 22 | } 23 | PrintVector(my_vector); 24 | 25 | int sum = 0; 26 | for (auto &n : my_vector) { 27 | sum += n; 28 | } 29 | 30 | { 31 | int a = 10; 32 | int b = 10; 33 | std::cout << "a: " << a << ", b: " << b << std::endl; 34 | } 35 | 36 | { 37 | int a = 20; 38 | int b = 30; 39 | { 40 | // int a = 50; 41 | std::cout << "a: " << a << std::endl; 42 | } 43 | std::cout << "a: " << a << ", b: " << b << std::endl; 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /tests/dynamic_programming/lpss_test_table.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/dynamic_programming/lpss.h" 2 | 3 | #include 4 | 5 | #include "gtest/gtest.h" 6 | 7 | TEST(LPSS_Table, bbbab) { 8 | std::string in = "bbbab"; 9 | LPSS_Problem p(in); 10 | auto result = p.LPSS_Table(); 11 | EXPECT_EQ(4, result); 12 | } 13 | 14 | TEST(LPSS_Table, cbbd) { 15 | std::string in = "cbbd"; 16 | 17 | LPSS_Problem p(in); 18 | auto result = p.LPSS_Table(); 19 | EXPECT_EQ(2, result); 20 | } 21 | 22 | TEST(LPSS_Table, Empty) { 23 | std::string in = ""; 24 | LPSS_Problem p(in); 25 | auto result = p.LPSS_Table(); 26 | EXPECT_EQ(0, result); 27 | } 28 | 29 | TEST(LPSS_Table, Character) { 30 | std::string in = "Character"; 31 | LPSS_Problem p(in); 32 | auto result = p.LPSS_Table(); 33 | EXPECT_EQ(3, result); 34 | } 35 | 36 | TEST(LPSS_Table, ABBCDABBC) { 37 | std::string in = "ABBCDABBC"; 38 | 39 | LPSS_Problem p(in); 40 | auto result = p.LPSS_Table(); 41 | EXPECT_EQ(5, result); 42 | } 43 | -------------------------------------------------------------------------------- /src/graph/vector_of_set.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | struct Vertex { 10 | Vertex(int v, std::set a) : vertex_number(v), adjacents(a) {} 11 | int vertex_number; 12 | std::set adjacents; 13 | }; 14 | 15 | class Graph { 16 | public: 17 | Graph() {} 18 | Graph(std::vector v) : v_(v) {} 19 | std::vector v_; 20 | }; 21 | 22 | int main() { 23 | Graph g1({Vertex(1, {2, 3}), Vertex(2, {1, 3, 4, 5}), Vertex(3, {1, 2, 4}), 24 | Vertex(4, {2, 4, 4})}); 25 | 26 | Graph g2; 27 | g2.v_.push_back(Vertex(1, {2, 3})); 28 | g2.v_.push_back(Vertex(2, {1, 3, 4, 5})); 29 | g2.v_.push_back(Vertex(3, {1, 2, 4})); 30 | g2.v_.push_back(Vertex(4, {2, 4, 4})); 31 | for (const auto &v : g2.v_) { 32 | std::cout << "Vertex: " << v.vertex_number << ", Adjacents: "; 33 | Print(v.adjacents); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /src/pointers/heap_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int i = 8; 6 | int *q; 7 | int *p = nullptr; // Doesn't point to anything. 8 | // *p = 5; // Crash your program! 9 | 10 | 11 | q = &i; 12 | p = new int; // Give me a valid location in memory and have p point to it. 13 | *p = 5; // Ok! 14 | (*p)++; // Ok! Derefrencing a pointer. 15 | 16 | 17 | int* &j = p; 18 | 19 | std::cout << "*p: " << *p << std::endl; 20 | std::cout << "*j: " << *j << std::endl; 21 | 22 | delete p; // I'm returning that valid location back! 23 | // (*p)++; // Not ok! Can crash! 24 | 25 | // We conditionally use more memory 26 | if (4 > 2) { 27 | p = new int; 28 | *p = 5; 29 | std::cout << "*p: " << *p << std::endl; 30 | } 31 | 32 | if (p != nullptr) { 33 | delete p; 34 | 35 | *p = 20; 36 | } 37 | 38 | p = new int(10); 39 | 40 | std::cout << "*p: " << *p << std::endl; 41 | 42 | delete p; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /src/uniform_initialization/initializer_list/explicit_constructor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | //----------------------------------------------------- 5 | // The compiler is allowed to make one implicit conversion to resolve the 6 | // parameters to a function. What this means is that the compiler can use 7 | // constructors callable with a single parameter to convert from one type to 8 | // another in order to get the right type for a parameter. 9 | 10 | class Test { 11 | public: 12 | explicit Test(int i) : x(i) {} 13 | void DoSomething() {} 14 | 15 | private: 16 | int x; 17 | }; 18 | 19 | void DoStuff(Test foo) { foo.DoSomething(); } 20 | 21 | int main(int argc, char const *argv[]) { 22 | // Uniform Initialization - Works! 23 | Test t1{5}; 24 | 25 | 26 | // = { } ignores explicit constructors - ERROR! :( 27 | // Test t2 = {5}; 28 | 29 | Test t3(5); 30 | 31 | // DoStuff(5); // Doesn't work because of explicit. 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /src/function/copy_if_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Person { 6 | std::string first_name; 7 | std::string last_name; 8 | int age; 9 | }; 10 | //----------------------------------------------------- 11 | int main() { 12 | std::vector from{1, 2, 3, 4, 5}; 13 | std::vector to; 14 | 15 | auto is_odd = [](const int& i) { return i % 2 == 1; }; 16 | 17 | // Copy from the "from" container into the "to" container, 18 | // only if is_odd returns true for that element 19 | std::copy_if(from.begin(), from.end(), std::back_inserter(to), is_odd); 20 | 21 | std::vector students; 22 | std::vector students_older_than_18; 23 | 24 | int min_age = 18; 25 | 26 | std::copy_if(students.begin(), students.end(), 27 | std::back_inserter(students_older_than_18), 28 | [min_age](const Person& s) { 29 | return s.age > min_age && s.last_name.size() > 10; 30 | }); 31 | } 32 | -------------------------------------------------------------------------------- /src/patterns/singleton/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | template 3 | class Singleton { 4 | private: 5 | static T* sInstance; 6 | 7 | protected: 8 | Singleton() {} 9 | 10 | public: 11 | static T& get() { 12 | if (sInstance) { 13 | std::cout << "Reusing the instance." << std::endl; 14 | return *sInstance; 15 | } else { 16 | std::cout << "Constructing the instance." << std::endl; 17 | sInstance = new T(); 18 | return *sInstance; 19 | } 20 | } 21 | }; 22 | 23 | // Initialize the static variable. 24 | template 25 | T* Singleton::sInstance = nullptr; 26 | 27 | class MyFileSystem : public Singleton { 28 | public: 29 | void Print() { std::cout << "This is my file system!" << std::endl; } 30 | }; 31 | 32 | int main(int argc, char const* argv[]) { 33 | MyFileSystem::get().Print(); 34 | 35 | MyFileSystem::get().Print(); 36 | MyFileSystem::get().Print(); 37 | MyFileSystem::get().Print(); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /src/priority_queue/main.cc: -------------------------------------------------------------------------------- 1 | // #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | { 8 | // max heap 9 | std::priority_queue q; 10 | 11 | q.push(5); 12 | q.push(150); 13 | q.push(1); 14 | 15 | q.push(42); 16 | q.push(3); 17 | q.push(2); 18 | 19 | // One by one extract items from max heap 20 | while (q.empty() == false) { 21 | std::cout << q.top() << std::endl; 22 | q.pop(); 23 | } 24 | } 25 | 26 | std::cout << "Output min heap" << std::endl; 27 | { 28 | // min heap 29 | std::priority_queue, std::greater > q; 30 | 31 | q.push(5); 32 | q.push(15); 33 | q.push(1); 34 | 35 | q.push(42); 36 | q.push(3); 37 | q.push(2); 38 | 39 | // One by one extract items from max heap 40 | while (q.empty() == false) { 41 | std::cout << q.top() << std::endl; 42 | q.pop(); 43 | } 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /src/cpp17/if_init.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(int argc, char const *argv[]) { 5 | std::map mymap; 6 | 7 | if (auto [iter, success] = mymap.insert(std::make_pair(1, "hi")); 8 | success == true) { 9 | // insert succeeded, and result is valid for this block 10 | std::cout << "iter->firsts: " << iter->first << std::endl; 11 | // ... 12 | } else { 13 | std::cout << "iter->firsts: " << iter->first << std::endl; 14 | } 15 | // iter, success are destroyed here 16 | return 0; 17 | } 18 | 19 | // // Generating data 20 | // std::vector data = {1, 2, 3, 4, 5, ..., 1'000'000'000}; // 1 000 000 000 21 | // items 22 | 23 | // //Usage or iterating data 24 | // for(auto e: data){ 25 | 26 | // } 27 | 28 | // // Coroutine 29 | // experimental::generator coRoutine() 30 | // { 31 | // for (int i { 0 }; i < 1000000000; ++i) { 32 | // co_yield i; 33 | // } 34 | // } 35 | 36 | // for(auto e: coRoutine() ){ 37 | 38 | // } -------------------------------------------------------------------------------- /src/function/lambda_event_class.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class Event { 6 | public: 7 | // Add a handler for this event 8 | void AddHandler(std::function handler) { 9 | mHandlers.emplace_back(handler); 10 | } 11 | // Loop over handlers and call each lambda 12 | void Trigger() { 13 | for (auto &f : mHandlers) { 14 | f(); 15 | } 16 | } 17 | 18 | private: 19 | std::vector> mHandlers; 20 | }; 21 | 22 | int main() { 23 | std::string name = "Sanjay"; 24 | 25 | Event myEvent; 26 | myEvent.AddHandler([]() { std::cout << "This is handler 1" << std::endl; }); 27 | myEvent.AddHandler( 28 | [&name]() { std::cout << "This is handler 2: " << name << std::endl; }); 29 | 30 | name = "Ari"; 31 | int x = 5; 32 | myEvent.AddHandler([&x]() { x++; }); 33 | 34 | std::cout << "x = " << x << std::endl; 35 | myEvent.Trigger(); 36 | std::cout << "x = " << x << std::endl; 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /src/function/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void PrintLine(int input) { std::cout << input << std::endl; } 4 | void PrintLine(char input) { std::cout << input << std::endl; } 5 | void PrintLine(float input) { std::cout << input << std::endl; } 6 | void PrintLine(double input) { std::cout << input << std::endl; } 7 | void PrintLine(long input) { std::cout << input << std::endl; } 8 | 9 | // Prints a string and adds a new line at the end. 10 | void PrintLine(std::string input) { std::cout << input << std::endl; } 11 | 12 | int x = 0; 13 | class MyClass { 14 | public: 15 | MyClass() { std::cout << "Constructor" << std::endl; } 16 | 17 | ~MyClass() { std::cout << "Destructor" << std::endl; } 18 | }; 19 | int main() { 20 | std::string text = "Hello world!"; 21 | PrintLine(text); 22 | PrintLine("Second hello world"); 23 | PrintLine(2); 24 | PrintLine('a'); 25 | MyClass c; 26 | for (int i = 0; i < 5; i++) { 27 | std::cout << "i: " << i << std::endl; 28 | } 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /src/lib/tree.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_H 2 | #define TREE_H 3 | 4 | struct TreeNode { 5 | int val; 6 | TreeNode *left; 7 | TreeNode *right; 8 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | TreeNode(int x, TreeNode *left_ptr, TreeNode *right_ptr) 10 | : val(x), left(left_ptr), right(right_ptr) {} 11 | }; 12 | 13 | class Tree { 14 | public: 15 | Tree() { root_ = nullptr; } 16 | bool empty(); 17 | int size(); 18 | void InOrder(TreeNode *start); 19 | void PreOrder(TreeNode *start); 20 | void PostOrder(TreeNode *start); 21 | TreeNode *Find(TreeNode *start, int val){ 22 | if(start == nullptr){ 23 | return nullptr; 24 | } 25 | if(start->val == val){ 26 | return start; 27 | } 28 | 29 | // Homework: complete the following two lines. 30 | Find(start->left, val); 31 | Find(start->right, val); 32 | 33 | return nullptr; 34 | 35 | } 36 | void CreateSampleTree1(); 37 | void CreateSampleTree2(); 38 | 39 | TreeNode *root_; 40 | }; 41 | 42 | #endif -------------------------------------------------------------------------------- /src/templates/non_member_functions_inside_templates.cc: -------------------------------------------------------------------------------- 1 | // Effective C++, Item 46 2 | 3 | #include 4 | template 5 | class Rational { 6 | public: 7 | Rational(const T& numerator = 0, // see Item 20 for why params 8 | const T& denominator = 1) {} // are now passed by reference 9 | const T numerator() const; // see Item 28 for why return 10 | const T denominator() const; // values are still passed by value, 11 | // Item 3 for why they're const 12 | 13 | friend const Rational operator*(const Rational& lhs, 14 | const Rational& rhs); 15 | }; 16 | template 17 | const Rational operator*(const Rational& lhs, const Rational& rhs) {} 18 | 19 | int main(int argc, char const* argv[]) { 20 | Rational oneHalf(1, 2); // this example is from Item 24, 21 | // except Rational is now a template 22 | Rational result = oneHalf * 2; // error! won't compile 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /src/dynamic_programming/lscs_table_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "src/lib/utility.h" 7 | // Given an integer array nums, find the contiguous subarray (containing at 8 | // least one number) which has the largest sum and return its sum. 9 | 10 | // Example: 11 | 12 | // Input: [-2,1,-3,4,-1,2,1,-5,4], 13 | // Output: 6 14 | // Explanation: [4,-1,2,1] has the largest sum = 6. 15 | 16 | // Finds LSCS at index i 17 | int MAX_LSCS(std::vector &input) { 18 | std::vector d(input.size()); 19 | d[0] = input[0]; 20 | for (int i = 1; i < input.size(); i++) { 21 | d[i] = std::max(d[i - 1] + input[i], input[i]); 22 | } 23 | auto max = std::max_element(d.begin(), d.end()); 24 | 25 | return *max; 26 | } 27 | 28 | // Driver function 29 | int main() { 30 | std::vector input = {-2, 1, -3, 4, -1, 2, 1, -5, 3}; 31 | 32 | auto max = MAX_LSCS(input); 33 | 34 | std::cout << "max: " << max << std::endl; 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /src/variables/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Prints a string and adds a new line at the end. 5 | void PrintLine(std::string text) { std::cout << text << std::endl; } 6 | 7 | int MakeDouble(int x) { return x * 2; } 8 | 9 | int main() { 10 | std::string hello = "Hello"; 11 | std::string world = " world"; 12 | 13 | int y = MakeDouble(6); 14 | auto z = MakeDouble(6); 15 | 16 | int i = 10; 17 | int j = i; 18 | 19 | i++; 20 | 21 | std::cout << "j: " << j << std::endl; 22 | std::cout << "i: " << i << std::endl; 23 | 24 | // int year = 2020; // Variable definition and Initialization 25 | 26 | int year; // Variable definition 27 | year = 2022; // Assignment 28 | 29 | std::string hello_world = hello + world + ';' + std::to_string(year); 30 | 31 | char x = 'A'; // single byte, usually from ASCII table 32 | 33 | std::string y = "this is a string"; 34 | 35 | std::cout << "x is: " << x << std::endl; 36 | 37 | PrintLine(hello_world); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /src/lib/graph/vector_v_vector_pair_e.cc: -------------------------------------------------------------------------------- 1 | #include "vector_v_vector_pair_e.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | bool Graph::IsEulerWalkable() { 10 | // Create a table to hold degree of each vertex 11 | std::vector degrees(v_.size()); 12 | 13 | // Iterate all edges 14 | for (auto e : e_) { 15 | degrees[e.first]++; 16 | degrees[e.second]++; 17 | } 18 | int countOdds = 0; 19 | 20 | // Iterate through degree table and count the number of odd ones 21 | for (auto d : degrees) { 22 | if (d % 2 == 1) { 23 | countOdds++; 24 | } 25 | } 26 | return (countOdds == 0 || countOdds == 2); 27 | } 28 | 29 | void Graph::PrintGraph() { 30 | for (const auto v : v_) { 31 | std::cout << "Vertex: " << v << ", Adjacents: {"; 32 | for (const auto e : e_) { 33 | if (e.first == v) { 34 | std::cout << e.second << ", "; 35 | } 36 | } 37 | std::cout << "}" << std::endl; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/graph/vector_v_vector_pair_e_test.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/graph/vector_v_vector_pair_e.h" 2 | 3 | #include 4 | 5 | #include "gtest/gtest.h" 6 | 7 | TEST(vector_v_vector_pair_e, euler_walkable1) { 8 | std::vector v = {0, 1, 2, 3}; 9 | std::vector> e = {{0, 1}, {1, 3}, {3, 2}, {2, 0}}; 10 | Graph g(v, e); 11 | EXPECT_EQ(true, g.IsEulerWalkable()); 12 | } 13 | 14 | TEST(vector_v_vector_pair_e, euler_walkable2) { 15 | std::vector v = {0, 1, 2, 3}; 16 | std::vector> e = {{0, 1}, {0, 2}, {0, 3}, 17 | {1, 2}, {1, 3}, {2, 3}}; 18 | Graph g(v, e); 19 | EXPECT_EQ(false, g.IsEulerWalkable()); 20 | } 21 | 22 | TEST(vector_v_vector_pair_e, euler_walkable3) { 23 | std::vector v = {0, 1, 2, 3}; 24 | std::vector> e = {{1, 3}, {1, 3}, {3, 0}, {3, 0}, 25 | {0, 2}, {2, 1}, {2, 3}}; 26 | Graph g(v, e); 27 | EXPECT_EQ(false, g.IsEulerWalkable()); 28 | } 29 | -------------------------------------------------------------------------------- /src/function/lambda_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | // lambda expression to print vector 5 | std::vector my_vector = {1, 2, 3, 4, 5, 6}; 6 | 7 | [](const std::vector &v) -> void { 8 | for (auto n : v) { 9 | std::cout << "n: " << n << std::endl; 10 | } 11 | }(my_vector); 12 | 13 | // Capturing everything by reference 14 | [&]() { 15 | for (auto n : my_vector) { 16 | std::cout << "n: " << n << std::endl; 17 | } 18 | }(); 19 | 20 | // Capturing my_vector by reference 21 | [&my_vector]() { 22 | for (auto n : my_vector) { 23 | std::cout << "n: " << n << std::endl; 24 | } 25 | }(); 26 | 27 | // Capturing all by value 28 | [=]() { 29 | for (auto n : my_vector) { 30 | std::cout << "n: " << n << std::endl; 31 | } 32 | }(); 33 | 34 | // Capturing all by value 35 | [=]() { 36 | for (auto n : my_vector) { 37 | std::cout << "n: " << n << std::endl; 38 | } 39 | }(); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /src/uniform_initialization/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | //----------------------------------------------------- 5 | class Test { 6 | public: 7 | private: 8 | int x = 5; // Works 9 | int y = {25}; // Works 10 | int z{125}; // Works 11 | }; 12 | //----------------------------------------------------- 13 | class Test2 { 14 | public: 15 | private: 16 | int x = 5; // Works 17 | int y = {25}; // Works 18 | int z{125}; // Works 19 | // int a(125); // ERROR! 20 | int a = 125; // Works 21 | }; 22 | 23 | int main() { 24 | // A novel aspect of uniform initialization is that it does not allow 25 | // narrowing from a less constrained to a more constrained type 26 | 27 | char c1{50}; // Fine, 50 can fit in a char 28 | // char c2{1234}; // ERROR - Can't narrow 1234 -> char 29 | char c3 = 1234; // Works 30 | 31 | int i1{10}; // Fine 32 | // int t2{10.0}; // ERROR - Can't narrow float -> int 33 | int t3 = 10.0; // Works 34 | } 35 | -------------------------------------------------------------------------------- /src/benchmark/output/insert_into_collection.csv: -------------------------------------------------------------------------------- 1 | name,iterations,real_time,cpu_time,time_unit,bytes_per_second,items_per_second,label,error_occurred,error_message 2 | "BM_InsertIntoVector/1024",6025,116791,116339,ns,,,,, 3 | "BM_InsertIntoVector/2048",1840,379149,378491,ns,,,,, 4 | "BM_InsertIntoVector/4096",480,1.36205e+06,1.35961e+06,ns,,,,, 5 | "BM_InsertIntoVector/8192",131,5.21744e+06,5.20318e+06,ns,,,,, 6 | "BM_InsertIntoVector/16384",33,2.09115e+07,2.08538e+07,ns,,,,, 7 | "BM_InsertIntoVector/32768",7,8.42043e+07,8.40841e+07,ns,,,,, 8 | "BM_InsertIntoVector/65536",2,3.3956e+08,3.39191e+08,ns,,,,, 9 | "BM_InsertIntoList/1024",1678,419052,418741,ns,,,,, 10 | "BM_InsertIntoList/2048",263,2.64554e+06,2.64434e+06,ns,,,,, 11 | "BM_InsertIntoList/4096",47,1.64807e+07,1.64137e+07,ns,,,,, 12 | "BM_InsertIntoList/8192",7,8.06457e+07,8.0579e+07,ns,,,,, 13 | "BM_InsertIntoList/16384",1,5.06616e+08,5.03807e+08,ns,,,,, 14 | "BM_InsertIntoList/32768",1,2.92866e+09,2.90961e+09,ns,,,,, 15 | "BM_InsertIntoList/65536",1,1.26058e+10,1.2577e+10,ns,,,,, 16 | -------------------------------------------------------------------------------- /src/graph/unordered_set_of_unordered_set.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/utility.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | struct Vertex { 10 | Vertex(int v, std::unordered_set a) : vertex_number(v), adjacents(a) {} 11 | // This function is used by unordered_set to compare 12 | bool operator==(const Vertex &rhs) const { 13 | return (this->vertex_number == rhs.vertex_number); 14 | } 15 | int vertex_number; 16 | std::unordered_set adjacents; 17 | }; 18 | 19 | struct VertextHashFunction { 20 | size_t operator()(const Vertex &a) const { return (a.vertex_number); } 21 | }; 22 | 23 | class Graph { 24 | public: 25 | Graph(std::unordered_set v) : v_(v) {} 26 | std::unordered_set v_; 27 | }; 28 | 29 | int main() { 30 | Graph v({Vertex(1, {2, 3}), Vertex(2, {1, 3, 4, 5}), Vertex(3, {1, 2, 4}), 31 | Vertex(4, {2, 4, 4})}); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /src/const/const_functions.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | class Rational { 5 | public: 6 | Rational(int n = 0, int d = 1) : numerator(n), denominator(d) {} 7 | 8 | int numerator; 9 | int denominator; 10 | 11 | void Print() { 12 | std::cout << "numerator: " << numerator << std::endl; 13 | std::cout << "denominator: " << denominator << std::endl; 14 | } 15 | 16 | private: 17 | }; 18 | 19 | const Rational operator*(const Rational &lhs, const Rational &rhs) { 20 | Rational result; 21 | result.numerator = lhs.numerator * rhs.numerator; 22 | result.denominator = lhs.denominator * rhs.denominator; 23 | 24 | return result; 25 | } 26 | 27 | int Add(int a, int b) { 28 | int result; 29 | result = a + b; 30 | return result; 31 | } 32 | int main() { 33 | { 34 | Rational a{1, 3}, b{2, 4}; 35 | Rational c = a * b; 36 | // a * b = c; // This compiles if operator* is not returning const! 37 | c.Print(); 38 | } 39 | // { Add(1, 2) = 4; } // ---> Doesn't compile! 40 | } -------------------------------------------------------------------------------- /src/memory_allocator/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | 3 | cc_binary( 4 | name = "main", 5 | srcs = ["main.cc"], 6 | ) 7 | 8 | cc_binary( 9 | name = "main_2", 10 | srcs = ["main_2.cc"], 11 | ) 12 | 13 | cc_binary( 14 | name = "main3", 15 | srcs = ["main3.cc"], 16 | ) 17 | 18 | cc_binary( 19 | name = "override_new_delete", 20 | srcs = ["override_new_delete.cc"], 21 | ) 22 | 23 | cc_binary( 24 | name = "global_new_delete", 25 | srcs = ["global_new_delete.cc"], 26 | ) 27 | 28 | cc_binary( 29 | name = "operator_new_delete", 30 | srcs = ["operator_new_delete.cc"], 31 | ) 32 | 33 | cc_binary( 34 | name = "stack_allocator_main", 35 | srcs = ["stack_allocator_main.cc"], 36 | ) 37 | 38 | cc_binary( 39 | name = "pool_allocator", 40 | srcs = ["pool_allocator.cc"], 41 | ) 42 | 43 | cc_binary( 44 | name = "override_new_delete2", 45 | srcs = ["override_new_delete2.cc"], 46 | ) 47 | 48 | cc_binary( 49 | name = "std_allocator1", 50 | srcs = ["std_allocator1.cc"], 51 | ) -------------------------------------------------------------------------------- /src/multi_threading/vector_of_threads_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/lib/utility.h" 9 | 10 | int main() { 11 | const int number_of_threads = 10; 12 | uint64_t number_of_elements = 1000 * 1000* 1000; 13 | uint64_t step = number_of_elements / number_of_threads; 14 | std::vector threads; 15 | std::vector partial_sums(number_of_threads); 16 | 17 | for (uint64_t i = 0; i < number_of_threads; i++) { 18 | threads.push_back(std::thread(AccumulateRange, std::ref(partial_sums[i]), 19 | i * step, (i + 1) * step)); 20 | } 21 | 22 | for (std::thread &t : threads) { 23 | if (t.joinable()) { 24 | t.join(); 25 | } 26 | } 27 | 28 | uint64_t total = 29 | std::accumulate(partial_sums.begin(), partial_sums.end(), uint64_t(0)); 30 | 31 | Print(partial_sums); 32 | std::cout << "total: " << total << std::endl; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /tests/dynamic_programming/lpss_test_memo.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/dynamic_programming/lpss.h" 2 | 3 | #include 4 | 5 | #include "gtest/gtest.h" 6 | 7 | TEST(LPSS_Memo, bbbab) { 8 | std::string in = "bbbab"; 9 | LPSS_Problem p(in); 10 | auto result = p.LPSS_Memo(0, in.size()-1); 11 | EXPECT_EQ(4, result); 12 | } 13 | 14 | TEST(LPSS_Memo, cbbd) { 15 | std::string in = "cbbd"; 16 | 17 | LPSS_Problem p(in); 18 | auto result = p.LPSS_Memo(0, in.size()-1); 19 | EXPECT_EQ(2, result); 20 | } 21 | 22 | TEST(LPSS_Memo, Empty) { 23 | std::string in = ""; 24 | LPSS_Problem p(in); 25 | auto result = p.LPSS_Memo(0, in.size()-1); 26 | EXPECT_EQ(0, result); 27 | } 28 | 29 | TEST(LPSS_Memo, Character) { 30 | std::string in = "Character"; 31 | LPSS_Problem p(in); 32 | auto result = p.LPSS_Memo(0, in.size()-1); 33 | EXPECT_EQ(3, result); 34 | } 35 | 36 | TEST(LPSS_Memo, ABBCDABBC) { 37 | std::string in = "ABBCDABBC"; 38 | 39 | LPSS_Problem p(in); 40 | auto result = p.LPSS_Memo(0, in.size()-1); 41 | EXPECT_EQ(5, result); 42 | } 43 | -------------------------------------------------------------------------------- /src/dynamic_programming/fib_memo_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // #include "src/lib/utility.h" 6 | 7 | // memo map works like a cache 8 | std::unordered_map memo; // O(1) (on average), Amortize O(1) 9 | 10 | // Fibonacci with memoization 11 | unsigned long Fibonacci_memo(unsigned long n) { 12 | if (n == 0 || n == 1) { 13 | return 1; 14 | } 15 | 16 | if (memo.count(n) > 0) { 17 | return memo[n]; 18 | } else { 19 | memo[n] = Fibonacci_memo(n - 1) + Fibonacci_memo(n - 2); 20 | } 21 | return memo[n]; 22 | } 23 | 24 | unsigned long Fibonacci(unsigned long n) { 25 | if (n == 0 || n == 1) { 26 | return 1; 27 | } 28 | 29 | return Fibonacci(n - 1) + Fibonacci(n - 2); 30 | } 31 | 32 | // Driver function 33 | int main() { 34 | unsigned long n = Fibonacci_memo(50); // O(n) 35 | 36 | unsigned long m = Fibonacci_memo(50); // O(1) 37 | std::cout << "n: " << n << std::endl; 38 | std::cout << "m: " << m << std::endl; 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /src/move/perfect_forwarding.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct A { 6 | A(int&& n) { std::cout << "rvalue overload, n=" << n << "\n"; } 7 | A(int& n) { std::cout << "lvalue overload, n=" << n << "\n"; } 8 | }; 9 | 10 | class B { 11 | public: 12 | template 13 | B(T1&& t1, T2&& t2, T3&& t3) 14 | : a1_{std::forward(t1)}, 15 | a2_{std::forward(t2)}, 16 | a3_{std::forward(t3)} {} 17 | 18 | private: 19 | A a1_, a2_, a3_; 20 | }; 21 | 22 | template 23 | std::unique_ptr make_unique1(U&& u) { 24 | return std::unique_ptr(new T(std::forward(u))); 25 | } 26 | 27 | template 28 | std::unique_ptr make_unique2(U&&... u) { 29 | return std::unique_ptr(new T(std::forward(u)...)); 30 | } 31 | 32 | int main() { 33 | auto p1 = make_unique1(2); // rvalue 34 | int i = 1; 35 | auto p2 = make_unique1(i); // lvalue 36 | 37 | std::cout << "B\n"; 38 | auto t = make_unique2(2, i, 3); 39 | } -------------------------------------------------------------------------------- /src/move/std_forward.cc: -------------------------------------------------------------------------------- 1 | // forward example 2 | #include // std::cout 3 | #include // std::forward 4 | 5 | // function with lvalue and rvalue reference overloads: 6 | void overloaded(const int& x) { std::cout << "[lvalue]"; } 7 | void overloaded(int&& x) { std::cout << "[rvalue]"; } 8 | 9 | // function template taking rvalue reference to deduced type: 10 | template 11 | void fn(T&& x) { 12 | overloaded(x); // always an lvalue 13 | overloaded(std::forward(x)); // rvalue if argument is rvalue 14 | 15 | // std::forward(x): 16 | // If arg is an lvalue reference, the function returns arg with its type 17 | // unchanged. 18 | // Otherwise, the function returns an rvalue reference (T&&) that refers to 19 | // arg that can be used to pass an rvalue. 20 | } 21 | 22 | int main() { 23 | int a; 24 | 25 | std::cout << "calling fn with lvalue: "; 26 | fn(a); 27 | std::cout << '\n'; 28 | 29 | std::cout << "calling fn with rvalue: "; 30 | fn(0); 31 | std::cout << '\n'; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /src/inheritance/private_inheritance.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | class Person { 4 | public: 5 | std::string name_; 6 | }; 7 | 8 | class Student : private Person {}; // inheritance is now private 9 | void eat(const Person& p); // anyone can eat 10 | void study(const Student& s); // only students study 11 | 12 | // in contrast to public inheritance, 13 | // compilers will generally not convert a derived class object (such as Student) 14 | // into a base class object (such as Person) if the inheritance relationship 15 | // between the classes is private. 16 | // The second rule is that members inherited from a private base class 17 | // become private members of the derived class, even if they were protected or 18 | // public in the base class. 19 | // Private inheritance means is-implemented-in-terms-of. 20 | int main(int argc, char const* argv[]) { 21 | Person p; // p is a Person 22 | Student s; // s is a Student 23 | eat(p); // fine, p is a Person 24 | eat(s); // error! a Student isn't a Person 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /src/class/brackets.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | class TextBlock { 4 | public: 5 | TextBlock(const std::string& s) : text(s) {} 6 | const char& operator[](std::size_t position) const // operator[] for 7 | { 8 | return text[position]; 9 | } // const objects 10 | char& operator[](std::size_t position) // operator[] for 11 | { 12 | return text[position]; 13 | } // non-const objects 14 | private: 15 | std::string text; 16 | }; 17 | 18 | int main(int argc, char const* argv[]) { 19 | TextBlock tb("Hello"); 20 | // calls non-const 21 | // TextBlock::operator[] 22 | std::cout << "tb[0]: " << tb[0] << std::endl; 23 | tb[0] = 'x'; 24 | std::cout << "tb[0]: " << tb[0] << std::endl; 25 | 26 | const TextBlock ctb("World"); 27 | // calls const TextBlock::operator[] 28 | std::cout << "ctb[0]: " << ctb[0] << std::endl; 29 | // ctb[0] = 'x';// compile error 30 | 31 | // char* p = &ctb[0]; // compile error 32 | std::cout << "ctb[0]: " << ctb[0] << std::endl; 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /src/control/while_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "src/lib/utility.h" 6 | 7 | int main() { 8 | std::string s = "Hello world"; 9 | for (auto c : s) { 10 | std::cout << "c: " << c << std::endl; 11 | s.push_back('!'); 12 | } 13 | 14 | int i, j; 15 | 16 | int *p; 17 | 18 | p = &i; 19 | 20 | p = &j; 21 | 22 | std::cout << *p << std::endl; 23 | 24 | std::vector my_vector; 25 | int i = 0; 26 | 27 | for (; i < 10;) { 28 | } 29 | 30 | while (i < 10) { 31 | my_vector.push_back(i++); 32 | } 33 | PrintVector(my_vector); 34 | 35 | // Do while 36 | my_vector.clear(); 37 | 38 | do { 39 | my_vector.push_back(i); 40 | } while (i < 10); 41 | 42 | PrintVector(my_vector); 43 | 44 | // Break and continue 45 | my_vector.clear(); 46 | i = 0; 47 | while (i < 10) { 48 | if (i == 2) { 49 | continue; 50 | } 51 | if (i == 5) { 52 | break; 53 | } 54 | my_vector.push_back(i++); 55 | } 56 | PrintVector(my_vector); 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /src/smart_pointer/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_binary") 2 | cc_binary( 3 | name = "main", 4 | srcs = ["main.cc"], 5 | 6 | ) 7 | 8 | cc_binary( 9 | name = "unique_ptr_main", 10 | srcs = ["unique_ptr_main.cc"], 11 | ) 12 | 13 | cc_binary( 14 | name = "shared_ptr_main", 15 | srcs = ["shared_ptr_main.cc"], 16 | ) 17 | 18 | cc_binary( 19 | name = "shared_ptr_main_cycle", 20 | srcs = ["shared_ptr_main_cycle.cc"], 21 | ) 22 | 23 | 24 | cc_binary( 25 | name = "stl_shared_cycle", 26 | srcs = ["stl_shared_cycle.cc"], 27 | ) 28 | 29 | 30 | 31 | cc_binary( 32 | name = "weak_ptr_main", 33 | srcs = ["weak_ptr_main.cc"], 34 | ) 35 | 36 | 37 | cc_binary( 38 | name = "stl_shared_weak_main", 39 | srcs = ["stl_shared_weak_main.cc"], 40 | ) 41 | cc_binary( 42 | name = "stl_unique_main", 43 | srcs = ["stl_unique_main.cc"], 44 | ) 45 | 46 | cc_binary( 47 | name = "stl_shared_ptr_issues", 48 | srcs = ["stl_shared_ptr_issues.cc"], 49 | ) 50 | 51 | 52 | cc_binary( 53 | name = "pimple", 54 | srcs = ["pimple.cc"], 55 | ) -------------------------------------------------------------------------------- /src/scope/namespace_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // namespaces 6 | #include 7 | 8 | using namespace std; // Generall, don't do this. 9 | 10 | // Global namespace 11 | int x = 0; 12 | 13 | void PrintSomething() { std::cout << "Printing example 0." << std::endl; } 14 | 15 | namespace ns1 { 16 | int x = 1; 17 | void PrintSomething() { std::cout << "Printing example 1." << std::endl; } 18 | 19 | } // namespace ns1 20 | 21 | namespace ns2 { 22 | int x = 2; 23 | void PrintSomething() { std::cout << "Printing example 2." << std::endl; } 24 | 25 | } // namespace ns2 26 | 27 | int main() { 28 | std::vector v; 29 | std::string s; 30 | 31 | int vector; 32 | int string; 33 | 34 | std::cout << "ns1::x: " << ns1::x << std::endl; 35 | std::cout << "ns2::x: " << ns2::x << std::endl; 36 | std::cout << "::x: " << ::x << std::endl; 37 | 38 | // ns1::yStruct.x = 10; 39 | // ns2::yStruct.x = 11; 40 | 41 | ns1::PrintSomething(); 42 | ns2::PrintSomething(); 43 | ::PrintSomething(); 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /src/decltype/find_deduced_type.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // Attempts to instantiate this template will elicit an error message, because 6 | // there’s no 7 | // template definition to instantiate. 8 | template // declaration only for TD; 9 | class TD; // TD == "Type Displayer" 10 | 11 | int main(int argc, char const* argv[]) { 12 | // TD td; 13 | // { 14 | // int x; 15 | // float y; 16 | // const int& cir = x; 17 | // auto xr = cir; 18 | 19 | // // To see the types for x and y, just try to instantiate 20 | // // TD with their types: 21 | // TD xType; // elicit errors containing 22 | // TD yType; // x's and y's types 23 | // TD cirType; // cir type 24 | // TD xrType; // xr type 25 | // } 26 | 27 | { 28 | int x; 29 | float y; 30 | std::cout << typeid(x).name() << '\n'; // display types for 31 | std::cout << typeid(y).name() << '\n'; // x and y 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /src/multi_threading/thread_functor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/lib/utility.h" 9 | 10 | int main() { 11 | const int number_of_threads = 10; 12 | uint64_t number_of_elements = 1000 * 1000 * 1000; 13 | uint64_t step = number_of_elements / number_of_threads; 14 | std::vector threads; 15 | std::vector partial_sums(number_of_threads); 16 | std::vector functors; 17 | 18 | for (int i = 0; i < number_of_threads; i++) { 19 | AccumulateFunctor *functor = new AccumulateFunctor(); 20 | threads.push_back( 21 | std::thread(std::ref(*functor), i * step, (i + 1) * step)); 22 | functors.push_back(functor); 23 | } 24 | 25 | for (std::thread &t : threads) { 26 | if (t.joinable()) { 27 | t.join(); 28 | } 29 | } 30 | 31 | int64_t total = 0; 32 | for (auto pf : functors) { 33 | total += pf->_sum; 34 | } 35 | std::cout << "total: " << total << std::endl; 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /src/templates/specialized.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | std::tuple ParseElem(const std::string& elem) {} 6 | 7 | template <> 8 | inline std::tuple ParseElem(const std::string& elem) { 9 | return std::make_tuple(std::stoi(elem)); 10 | } 11 | 12 | template <> 13 | inline std::tuple ParseElem(const std::string& elem) { 14 | return std::make_tuple(elem); 15 | } 16 | 17 | int main() { 18 | auto my_int = ParseElem("11"); 19 | auto my_string = ParseElem("r1"); 20 | 21 | std::cout << "std::get<0>(my_int): " << std::get<0>(my_int) << std::endl; 22 | std::cout << "std::get<0>(my_string): " << std::get<0>(my_string) 23 | << std::endl; 24 | 25 | return 0; 26 | } 27 | 28 | 29 | // "movei tx, 110" ======> "tx,110" ===>vector {"tx", "110"} ======================> tuple: ("tx", 110) 30 | 31 | // "add r1, r2, r3" ======> "r1, r2, r3" ==> vector {"r1", "r2", "r3"} ==============> tuple: ("r1", "r2", "r3") 32 | 33 | // ====ParseStr=> -------------------------------------------------------------------------------- /src/dynamic_programming/coin_memo_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | 8 | // memo map works like a cache 9 | std::unordered_map memo; 10 | 11 | int coinChange(std::vector &coins, int amount) { 12 | if (amount == 0) { 13 | return 0; 14 | } 15 | 16 | if (amount < 0) { 17 | return -1; 18 | } 19 | 20 | if (memo.count(amount) > 0) { 21 | return memo[amount]; 22 | } 23 | 24 | int min = INT_MAX; 25 | for (auto coin : coins) { 26 | int n = coinChange(coins, amount - coin); 27 | if (n >= 0) { 28 | min = std::min(n, min); 29 | } 30 | } 31 | 32 | if (min >= 0 && min < INT_MAX) { 33 | memo[amount] = 1 + min; 34 | } else { 35 | memo[amount] = -1; 36 | } 37 | 38 | return memo[amount]; 39 | } 40 | 41 | int main() { 42 | std::vector coins = {84, 457, 478, 309, 350, 349, 43 | 422, 469, 100, 432, 188}; 44 | int amount = 60993; 45 | int n = coinChange(coins, amount); 46 | std::cout << "n: " << n << std::endl; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /src/dynamic_programming/lscs_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "src/lib/utility.h" 7 | // Given an integer array nums, find the contiguous subarray (containing at 8 | // least one number) which has the largest sum and return its sum. 9 | 10 | // Example: 11 | 12 | // Input: [-2,1,-3,4,-1,2,1,-5,4], 13 | // Output: 6 14 | // Explanation: [4,-1,2,1] has the largest sum = 6. 15 | 16 | // Finds LSCS at index i 17 | int LSCS(std::vector &input, int i) { 18 | if (i == 0) { 19 | return input[0]; 20 | } else { 21 | return std::max(LSCS(input, i - 1) + input[i], input[i]); 22 | } 23 | } 24 | 25 | // Driver function 26 | int main() { 27 | std::vector input = {-2, 1, -3, 4, -1, 2, 1, -5, 3}; 28 | std::vector sum_vector; 29 | 30 | for (int i = 0; i < input.size(); i++) { 31 | sum_vector.push_back(LSCS(input, i)); 32 | } 33 | 34 | Print(sum_vector); 35 | auto max = std::max_element(sum_vector.begin(), sum_vector.end()); 36 | 37 | std::cout << "max: " << *max << std::endl; 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /src/inheritance/call_virtual_during_ctr copy.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | class Transaction { // base class for all 4 | public: // transactions 5 | Transaction() { logTransaction(); } 6 | virtual ~Transaction() { logTransaction(); } 7 | 8 | virtual void logTransaction() const { 9 | std::cout << "Transaction" << std::endl; 10 | } // make type-dependent 11 | // log entry 12 | }; 13 | 14 | class BuyTransaction : public Transaction { // derived class 15 | public: 16 | virtual void logTransaction() const { 17 | std::cout << "BuyTransaction" << std::endl; 18 | } // how to log trans- 19 | // actions of this type 20 | }; 21 | class SellTransaction : public Transaction { // derived class 22 | public: 23 | virtual void logTransaction() const { 24 | std::cout << "SellTransaction" << std::endl; 25 | } // how to log trans- 26 | // actions of this type 27 | }; 28 | int main(int argc, char const *argv[]) { 29 | // This will surprisingly call logTransaction from Transaction class not, 30 | // buyTransaction. 31 | BuyTransaction b; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /src/lib/dynamic_programming/dynamic_programming.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class DynamicProgramming { 10 | public: 11 | static int MinimumJumps(std::vector &input, int i); 12 | static int MinimumJumpsMemo(std::vector &input, int i); 13 | static int MinimumJumpsMemo_aux(std::unordered_map &memo, 14 | std::vector &input, int i); 15 | //----------------------------------------------------- 16 | // We have three coins: {1, 5, 10}. You can use each coin unlimited times. 17 | // For a given n, find out how many different ways you can build value n 18 | // using the given coins. 19 | // Example: 20 | // Input n = 2 21 | // Output: 1. Explanation: {1, 1} 22 | static int NumberOfCombinationSums(std::vector &input, int sum); 23 | 24 | static void NumberOfCombinationSums_aux( 25 | std::vector &input, int sum, int cur_sum, 26 | std::vector cur_combination, std::set> &combinations); 27 | }; -------------------------------------------------------------------------------- /src/modifiers/sizeof_examples_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | { 5 | std::cout << "sizeof(bool): " << sizeof(bool) << std::endl; 6 | } 7 | { 8 | struct { 9 | char mData[22]; 10 | char mSize; 11 | bool mIsSmallString; 12 | } Small; 13 | 14 | std::cout << "sizeof(Small): " << sizeof(Small) << std::endl; 15 | } 16 | { 17 | struct X { 18 | // vtable* ptr; ---> 8 bytes 19 | int a; //=============> 4 bytes, padding 20 | 21 | virtual void f(); 22 | virtual void g(); 23 | void h1(); 24 | void h2(); 25 | 26 | virtual ~X(); 27 | }; 28 | 29 | struct Y : X { 30 | double b; 31 | void f() override; 32 | virtual void i(); 33 | ~Y(); 34 | }; 35 | 36 | struct Z : Y { 37 | char c; 38 | char d; 39 | int e; 40 | void g() override; 41 | ~Z(); 42 | }; 43 | 44 | std::cout << "sizeof(X): " << sizeof(X) << std::endl; 45 | std::cout << "sizeof(Y): " << sizeof(Y) << std::endl; 46 | std::cout << "sizeof(Z): " << sizeof(Z) << std::endl; 47 | } 48 | } -------------------------------------------------------------------------------- /src/multi_threading/vector_of_threads_lambda_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/lib/utility.h" 9 | 10 | int main() { 11 | const int number_of_threads = 10; 12 | uint64_t number_of_elements = 1000 * 1000 * 1000; 13 | uint64_t step = number_of_elements / number_of_threads; 14 | std::vector threads; 15 | std::vector partial_sums(number_of_threads); 16 | 17 | for (uint64_t i = 0; i < number_of_threads; i++) { 18 | threads.push_back(std::thread([i, &partial_sums, step] { 19 | for (uint64_t j = i * step; j < (i + 1) * step; j++) { 20 | partial_sums[i] += j; 21 | } 22 | 23 | while(true) 24 | })); 25 | } 26 | 27 | for (std::thread &t : threads) { 28 | if (t.joinable()) { 29 | t.join(); 30 | } 31 | } 32 | 33 | uint64_t total = 34 | std::accumulate(partial_sums.begin(), partial_sums.end(), uint64_t(0)); 35 | Print(partial_sums); 36 | std::cout << "total: " << total << std::endl; 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /src/uniform_initialization/aggregate_initialization.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //----------------------------------------------------- 7 | struct Point { 8 | int x; 9 | int y; 10 | int z; 11 | }; 12 | 13 | struct Test2 { 14 | Point topLeft; 15 | Point botRight; 16 | }; 17 | 18 | int main() { 19 | Point t{50, -50, 25}; // Aggregate initialization 20 | 21 | // t.x = 50; 22 | // t.y = -50; 23 | 24 | Test2 t2{ 25 | {5, 10, 15}, // Top left 26 | {2, 4, 6}, // Bottom right 27 | }; 28 | 29 | // STL 30 | // Initialize a vector of even numbers 31 | std::vector v{2, 4, 6, 8, 10}; 32 | std::vector v2{2, 4, 6, 8, 10, 11}; 33 | std::vector v4{2, 4, 6, 8, 10, 11, 12}; 34 | 35 | // Initialize a list of odd numbers 36 | std::list l{1, 3, 5, 7, 9}; 37 | 38 | // Create a pair 39 | std::pair p{"Hello", 5}; 40 | 41 | // Vector of pairs 42 | std::vector> months{ 43 | {1, "January"}, {2, "February"}, {3, "March"}, 44 | // ... 45 | }; 46 | } -------------------------------------------------------------------------------- /cpp-template/README.md: -------------------------------------------------------------------------------- 1 | # C++ Template for Bazel and Google Test 2 | 3 | > Can be used in Visual Studio Code 4 | 5 | You can use this template for most of your C++ projects without the need for changing the BUILD files. 6 | 7 | ## Installation 8 | 9 | You can copy this using this command: 10 | 11 | ```bash 12 | git clone https://github.com/ourarash/cpp-template.git 13 | ``` 14 | 15 | ### Run main: 16 | 17 | You can run this using `blaze`: 18 | 19 | ```bash 20 | bazel run src/main:main 21 | ``` 22 | 23 | ### Run Tests: 24 | 25 | You can run unit tests using [`blaze`](installing-bazel): 26 | 27 | ```bash 28 | bazel test tests:tests 29 | ``` 30 | 31 | ## Directory Structure 32 | 33 | Directory Structure 34 | 35 | ## Installing Bazel 36 | 37 | This repo uses `Bazel` for building C++ files. 38 | You can install Bazel using this [link](https://docs.bazel.build/versions/master/install.html). 39 | 40 | ### Credit 41 | 42 | This repo was inspired by [this post](https://www.ratanparai.com/c++/writing-unit-tests-with-bazel/). 43 | -------------------------------------------------------------------------------- /src/class/constructor_implicit_default_deleted.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class Point { 6 | public: 7 | // No default constructor is defined. 8 | 9 | // PARAMETERIZED Constructor 10 | Point(int i, int j) { 11 | std::cout << "**PARAMETERIZED constructor." << std::endl; 12 | i_ = i; 13 | j_ = j; 14 | z_ = new int; 15 | } 16 | 17 | ~Point() { 18 | delete z_; // Deallocate z_ 19 | std::cout << "DESTRUCTOR." << std::endl; 20 | } 21 | 22 | // Getter or Accessor 23 | int GetI() const { return i_; }; 24 | int GetJ() const { return j_; }; 25 | 26 | void SetI(int i) { i_ = i; }; 27 | void SetJ(int j) { j_ = j; }; 28 | 29 | private: 30 | int i_; 31 | int j_; 32 | 33 | int *z_; 34 | }; 35 | 36 | std::ostream &operator<<(std::ostream &os, const Point &m) { 37 | return os << "( " << m.GetI() << ", " << m.GetJ() << " )"; 38 | } 39 | 40 | void MyFunction(Point param) { std::cout << "param: " << param << std::endl; } 41 | 42 | int main() { 43 | Point p1; //------------> doesn't compile! 44 | std::cout << "p1: " << p1 << std::endl; 45 | } -------------------------------------------------------------------------------- /src/memory_allocator/operator_new_delete.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct X { 4 | // X() { throw std::runtime_error(""); } 5 | X() { std::cout << "Constructor!" << std::endl; } 6 | ~X() { std::cout << "Destructor!" << std::endl; } 7 | // custom placement new 8 | static void* operator new(std::size_t sz, bool b) { 9 | std::cout << "custom placement new called, b = " << b << '\n'; 10 | std::cout << "sz: " << sz << std::endl; 11 | return ::operator new(sz); 12 | } 13 | // custom placement delete 14 | static void operator delete(void* ptr, bool b) { 15 | std::cout << "custom placement delete called, b = " << b << '\n'; 16 | ::operator delete(ptr); 17 | } 18 | // custom placement delete without extra parameter. 19 | static void operator delete(void* ptr) { 20 | std::cout << "custom placement delete with no extra parameters." << '\n'; 21 | ::operator delete(ptr); 22 | } 23 | }; 24 | int main() { 25 | try { 26 | X* p1 = new (true) X; 27 | delete p1; 28 | } catch (const std::exception& e) { 29 | std::cout << "e.what(): " << e.what() << std::endl; 30 | } 31 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A tour of C++ constructs using Bazel build system 2 | 3 | > Can be used in Visual Studio Code 4 | 5 | ## Installation 6 | 7 | You can copy this using this command: 8 | 9 | ```bash 10 | git clone https://github.com/ourarash/cpp_tour.git 11 | ``` 12 | 13 | ### Run main: 14 | 15 | You can run this using `bazel`: 16 | 17 | ```bash 18 | bazel run src/main:main 19 | ``` 20 | 21 | ### Run Tests: 22 | 23 | You can run unit tests using [`bazel`](installing-bazel): 24 | 25 | ```bash 26 | bazel test tests:tests 27 | ``` 28 | 29 | ## Installing Bazel 30 | 31 | This repo uses `Bazel` for building C++ files. 32 | You can install Bazel using this [link](https://docs.bazel.build/versions/master/install.html). 33 | 34 | ## Working with Bazel 35 | 36 | Here is a video that explains more about how to use Google Test with Bazel in Visual Studio Code: 37 | 38 |
39 | 40 | 41 | Bazel & Google Test in Visual Studio Code 42 | 43 |
44 | -------------------------------------------------------------------------------- /src/benchmark/variable_in_loop.cc: -------------------------------------------------------------------------------- 1 | /** 2 | * Benchmark code for comparison of inserting inside a vector vs list 3 | * Run: 4 | NAME_OF_TARGET=plus_equal 5 | bazel run src/benchmark/$NAME_OF_TARGET -c opt -- --benchmark_format=csv 6 | \ | tee run src/benchmark/output/$NAME_OF_TARGET.csv python3 7 | src/benchmark/plot.py src/benchmark/output/$NAME_OF_TARGET.csv 8 | */ 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "benchmark/benchmark.h" 16 | #include "src/lib/utility.h" 17 | 18 | class MyType { 19 | public: 20 | MyType() { std::cout << "constructor!" << std::endl; } 21 | 22 | void Print() { std::cout << "Print!" << std::endl; } 23 | }; 24 | 25 | void VariableInsideLoop() { 26 | for (int i = 0; i < 100; i++) { 27 | MyType m; 28 | m.Print(); 29 | } 30 | } 31 | 32 | void VariableOutsideLoop() { 33 | MyType m; 34 | for (int i = 0; i < 100; i++) { 35 | m.Print(); 36 | } 37 | } 38 | 39 | int main(int argc, char const *argv[]) { 40 | VariableInsideLoop(); 41 | // VariableOutsideLoop(); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /src/function/filter.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/lib/utility.h" 9 | 10 | //----------------------------------------------------- 11 | bool IsOdd(int i) { return ((i % 2) == 1); } 12 | //----------------------------------------------------- 13 | template 14 | T filter(const T& v, U f) { 15 | T ret; 16 | std::copy_if(v.begin(), // Start of range 17 | v.end(), // End of range 18 | std::back_inserter(ret), // Where to insert 19 | f); // Boolean unary function 20 | return ret; 21 | } 22 | //----------------------------------------------------- 23 | int main() { 24 | std::vector inputs = {0, 1, 2, 3, 4, 5, 6, 7, 8}; 25 | std::vector outputs1; 26 | 27 | std::copy_if(inputs.begin(), inputs.end(), std::back_inserter(outputs1), 28 | IsOdd); 29 | Print(outputs1); 30 | std::vector outputs2 = filter(inputs, IsOdd); 31 | Print(outputs2); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /src/smart_pointer/shared_ptr_costum_delete copy.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class Widget {}; 6 | 7 | auto loggingDel = [](Widget *pw) // custom deleter 8 | { // (as in Item 18) 9 | // makeLogEntry(pw); 10 | delete pw; 11 | }; 12 | std::unique_ptr< // deleter type is 13 | Widget, decltype(loggingDel) // part of ptr type 14 | > 15 | upw(new Widget, loggingDel); 16 | 17 | std::shared_ptr // deleter type is not 18 | spw(new Widget, loggingDel); // part of ptr type 19 | 20 | int main(int argc, char const *argv[]) { 21 | auto customDeleter1 = [](Widget *pw) {}; // custom deleters, 22 | auto customDeleter2 = [](Widget *pw) {}; // each with a 23 | // different type 24 | std::shared_ptr pw1(new Widget, customDeleter1); 25 | std::shared_ptr pw2(new Widget, customDeleter2); 26 | 27 | // Because pw1 and pw2 have the same type, they can be placed in a container 28 | // of objects 29 | // of that type 30 | std::vector> vpw{pw1, pw2}; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /src/templates/alias_template.cc: -------------------------------------------------------------------------------- 1 | // Effective modern C++, Item 9 2 | #include 3 | #include 4 | template 5 | class MyAlloc {}; 6 | class Widget {}; 7 | 8 | template // MyAllocList 9 | using MyAllocList = std::list>; // is synonym for 10 | // std::list> 11 | 12 | // Same thing with typedef 13 | template // MyAllocList::type 14 | struct MyAllocList2 { // is synonym for 15 | typedef std::list> type; // std::list> 17 | 18 | template 19 | class Widget2 { // Widget contains 20 | private: // a MyAllocList 21 | MyAllocList list; // as a data member 22 | // This one needs typename 23 | typename MyAllocList2::type list2; // as a data member 24 | }; 25 | 26 | int main(int argc, char const *argv[]) { 27 | MyAllocList lw; // client code 28 | MyAllocList2::type lw2; // client code 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /src/cpp17/memory_resources.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | // All memory_resources have an allocate and deallocate function 7 | 8 | // Use pool_options to configure the pool 9 | std::pmr::pool_options options; 10 | // Maximum of 1024 blocks 11 | options.max_blocks_per_chunk = 1024; 12 | // Largest block size required in bytes 13 | options.largest_required_pool_block = 16; 14 | 15 | // This is a non-thread safe pool allocator 16 | std::pmr::unsynchronized_pool_resource poolAlloc(options); 17 | // Allocate 12 bytes 18 | void* ptr = poolAlloc.allocate(12); 19 | // Deallocate the memory 20 | poolAlloc.deallocate(ptr, 12); 21 | 22 | // A monotonic_buffer_resource is like the "stack allocator" 23 | std::pmr::monotonic_buffer_resource stackAlloc; 24 | 25 | // Allocate 50 bytes 26 | void* ptr2 = stackAlloc.allocate(50); 27 | 28 | // Deallocate won't do anything in this case! 29 | stackAlloc.deallocate(ptr2, 50); 30 | 31 | // To release all the memory use the release function 32 | // (Also happens when the destructor is called) 33 | stackAlloc.release(); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /src/cpp14/other.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class SizeDelete { 7 | public: 8 | SizeDelete() {} 9 | void operator delete(void* p, std::size_t size) noexcept { 10 | std::cout << "size: " << size << std::endl; 11 | }; 12 | void operator delete[](void*, std::size_t) noexcept; 13 | 14 | int x; 15 | int y; 16 | }; 17 | int main() { 18 | { 19 | { // Declare this as a binary number 20 | int x = 0b1010; 21 | std::cout << "x: " << x << std::endl; 22 | 23 | // Add digit separators so numbers are easier to read 24 | int y = 300'000; 25 | std::cout << "y: " << y << std::endl; 26 | 27 | // Base 8 28 | int z = 01010; 29 | std::cout << "z: " << z << std::endl; 30 | } 31 | 32 | { 33 | long long decn = 1'000'000'000ll; 34 | long long hexn = 0xFFFF'FFFFll; 35 | long long octn = 00'23'00ll; 36 | long long binn = 0b1010'0011ll; 37 | 38 | std::cout << "octn: " << octn << std::endl; 39 | } 40 | 41 | { 42 | SizeDelete* p = new SizeDelete(); 43 | delete p; 44 | } 45 | } 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /src/move/copy_assignment.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | template 4 | class NamedObject { 5 | public: 6 | // this ctor no longer takes a const name, because nameValue 7 | // is now a reference-to-non-const string. The char* constructor 8 | // is gone, because we must have a string to refer to. 9 | NamedObject(std::string& name, const T& value) 10 | : nameValue(name), objectValue(value) {} 11 | // as above, assume no 12 | // operator= is declared 13 | // private: 14 | std::string& nameValue; // this is now a reference 15 | const T objectValue; // this is now const 16 | }; 17 | int main(int argc, char const* argv[]) { 18 | std::string newDog("Persephone"); 19 | std::string oldDog("Satch"); 20 | NamedObject p(newDog, 2); 21 | NamedObject s(oldDog, 36); 22 | 23 | std::cout << "p.nameValue: " << p.nameValue << std::endl; 24 | std::cout << "s.nameValue: " << s.nameValue << std::endl; 25 | 26 | newDog = "test"; 27 | std::cout << "p.nameValue: " << p.nameValue << std::endl; 28 | 29 | // p = s; // ---> Doesn't compile because the class has reference member 30 | // variables 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /src/typeid/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | /* 9 | * decltype() function introduced in C++ 11. 10 | * 11 | * This function detect the type of any variable 12 | * and provide same data type to the new variable. 13 | * 14 | * Ex: 15 | * string str; // type of str is string 16 | * decltype(str) name; // Creating a new variable name of string type 17 | */ 18 | 19 | // typeid() Example 20 | cout << "typeid(int) = " << typeid(5).name() << endl; 21 | cout << "typeid(double) = " << typeid((double)5.2).name() << endl; 22 | cout << "typeid(float) = " << typeid((float)5.3).name() << endl; 23 | cout << "typeid(string) = " << typeid("India").name() << endl; 24 | 25 | cout << "-----" << endl; // Line Divider 26 | 27 | // decltype example 28 | string value = "abc"; 29 | decltype(value) name = "xyz"; 30 | 31 | cout << "typeid(value) = " << typeid(value).name() << endl; 32 | cout << "typeid(name) = " << typeid(name).name() << endl; 33 | 34 | cout << "value = " << value << endl; 35 | cout << "name = " << name << endl; 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /tests/dynamic_programming/lpss_test.cc: -------------------------------------------------------------------------------- 1 | #include "src/lib/dynamic_programming/lpss.h" 2 | 3 | #include 4 | 5 | #include "gtest/gtest.h" 6 | 7 | TEST(LPSS, bbbab) { 8 | std::string in = "bbbab"; 9 | LPSS_Problem p(in); 10 | auto result = p.LPSS(0, in.size()-1); 11 | EXPECT_EQ(4, result); 12 | } 13 | 14 | TEST(LPSS, za12bb34a) { 15 | std::string in = "za12bb34a"; 16 | LPSS_Problem p(in); 17 | auto result = p.LPSS(0, in.size()-1); 18 | EXPECT_EQ(4, result); 19 | } 20 | 21 | TEST(LPSS, cbbd) { 22 | std::string in = "cbbd"; 23 | 24 | LPSS_Problem p(in); 25 | auto result = p.LPSS(0, in.size()-1); 26 | EXPECT_EQ(2, result); 27 | } 28 | 29 | TEST(LPSS, Empty) { 30 | std::string in = ""; 31 | LPSS_Problem p(in); 32 | auto result = p.LPSS(0, in.size()-1); 33 | EXPECT_EQ(0, result); 34 | } 35 | 36 | TEST(LPSS, Character) { 37 | std::string in = "Character"; 38 | LPSS_Problem p(in); 39 | auto result = p.LPSS(0, in.size()-1); 40 | EXPECT_EQ(3, result); 41 | } 42 | 43 | TEST(LPSS, ABBCDABBC) { 44 | std::string in = "ABBCDABBC"; 45 | 46 | LPSS_Problem p(in); 47 | auto result = p.LPSS(0, in.size()-1); 48 | EXPECT_EQ(5, result); 49 | } 50 | -------------------------------------------------------------------------------- /.bazelrc: -------------------------------------------------------------------------------- 1 | # Address sanitizer configuration. 2 | # ASAN_SYMBOLIZER_PATH=/usr/local/Cellar/llvm/13.0.0_1/bin/llvm-symbolizer 3 | 4 | # Address sanitizer config 5 | # Use with bazel run --config=asan or lsan 6 | build:asan --strip=never 7 | build:asan --copt -fsanitize=address 8 | build:asan --copt -DADDRESS_SANITIZER 9 | build:asan --copt -O0 10 | build:asan --copt -g 11 | build:asan --copt -fno-omit-frame-pointer 12 | build:asan --linkopt -fsanitize=address 13 | build:asan --sandbox_debug 14 | build:asan --spawn_strategy=standalone 15 | 16 | # undefined behavior sanitizer config 17 | build:ubsan --strip=never 18 | build:ubsan --copt -fsanitize=undefined 19 | build:ubsan --copt -DADDRESS_SANITIZER 20 | build:ubsan --copt -O0 21 | build:ubsan --copt -g 22 | build:ubsan --copt -fno-omit-frame-pointer 23 | build:ubsan --linkopt -fsanitize=undefined 24 | build:ubsan --sandbox_debug 25 | build:ubsan --spawn_strategy=standalone 26 | 27 | # Memory sanitizer config 28 | build:msan --strip=never 29 | build:msan --copt -fsanitize=memory 30 | build:msan --copt -O0 31 | build:msan --copt -g 32 | build:msan --linkopt -fsanitize=memory 33 | build:msan --sandbox_debug 34 | build:msan --spawn_strategy=standalone --------------------------------------------------------------------------------