├── .gitignore ├── README.md ├── addition ├── baidu │ └── clear-certain-number.cpp ├── kuaishou │ └── sleep_one_ms.cpp └── meixing │ └── bugs │ └── protected_inherite.cpp ├── common-bugs └── shared-ptr-circular-ref │ ├── bug-fix.cpp │ └── bug.cpp ├── design-pattern ├── abstract-factory │ ├── abstract_factory.cpp │ ├── abstract_factory.h │ └── main.cpp ├── adapter │ ├── main.cpp │ ├── play_adapter.cpp │ └── play_adapter.h ├── decoractor │ ├── decoractor.cpp │ ├── decoractor.h │ └── main.cpp ├── factory-method │ ├── factory_method.cpp │ ├── factory_method.h │ └── main.cpp └── simple-factory │ ├── main.cpp │ ├── simple_factory.cpp │ └── simple_factory.h ├── gdb-instance ├── deadlock │ ├── README.md │ └── main.cpp └── high-cpu │ ├── README.md │ └── main.cpp ├── grammar ├── lambda.cpp ├── move.cpp └── template │ ├── class_template.cpp │ └── function_template.cpp ├── hand-written ├── RAII │ └── main.cpp ├── class_default_func │ └── main.cpp ├── singleton │ ├── hungry.cpp │ ├── local-static-var-lazy.cpp │ └── lock-lazy.cpp ├── threadpool │ └── threadpool.cpp └── tiny_shared_ptr │ ├── tiny_shared_ptr.cpp │ └── tiny_shared_ptr.h ├── interview ├── get-asynchronous-res-multi-thread │ └── main.cpp ├── read-file-multi-threads │ ├── file1 │ ├── file2 │ ├── file3 │ └── main.cpp └── simple-asynchronous-tasks │ └── main.cpp ├── stl └── vector │ ├── push_emplace_back.cpp │ └── sort_range.cpp ├── valgrind ├── README.md ├── push_back_leak └── push_back_leak.cpp └── written-exam └── comma_input.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cpp-tips 2 | 3 | We recommend that you compile it under Linux using the `g++` tool. For multi-threaded services, `-pthread` needs to be added. 4 | 5 | ```bash 6 | g++ main.cpp -o main -std=c++11 -pthread 7 | ``` 8 | -------------------------------------------------------------------------------- /addition/baidu/clear-certain-number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | uint64_t clearBitsInRange(uint64_t num, int m, int n) { 7 | int bits = log10(num) + 1; 8 | uint64_t left_mask = pow(10, bits - m + 1); 9 | uint64_t right_mask = pow(10, bits - n); 10 | 11 | uint64_t left_num = num / left_mask; 12 | uint64_t right_num = num % right_mask; 13 | 14 | return left_mask * left_num + right_num; 15 | } 16 | 17 | int main() { 18 | uint64_t num = 123456789; 19 | int m = 3, n = 6; 20 | 21 | uint64_t res = clearBitsInRange(num, m, n); 22 | 23 | std::cout << "origin number: " << num << std::endl; 24 | std::cout << "final number: " << res << std::endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /addition/kuaishou/sleep_one_ms.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int count = 0; 8 | std::chrono::milliseconds duration(1); 9 | 10 | while (true) { 11 | count++; 12 | std::this_thread::sleep_for(duration); 13 | } 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /addition/meixing/bugs/protected_inherite.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Base { 6 | public: 7 | Base() {}; 8 | ~Base() {}; 9 | virtual void func() {}; 10 | void foo() {}; 11 | }; 12 | 13 | class MyClass : protected Base { 14 | public: 15 | MyClass() { 16 | _p = new int; 17 | } 18 | 19 | ~MyClass() { 20 | if (_p != nullptr) { 21 | delete _p; 22 | } 23 | } 24 | 25 | void func() {}; 26 | 27 | private: 28 | int* _p; 29 | static int _global; 30 | }; 31 | 32 | MyClass::_global = 10; // bug-1: modify member variable outside the class 33 | 34 | int main() { 35 | MyClass a, b; 36 | a.foo(); // bug-2: foo() is protected 37 | 38 | b = a; // bug-3: shallow copy 39 | Base* base = &a; // bug-4: cannot assign MyClass object to pointer to Base 40 | base->func(); 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /common-bugs/shared-ptr-circular-ref/bug-fix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class B; 7 | 8 | class A { 9 | public: 10 | std::shared_ptr ptr; // point to class B 11 | }; 12 | 13 | class B { 14 | public: 15 | std::weak_ptr weak_ptr; // point to class A (change to weak_ptr) 16 | }; 17 | 18 | int main() { 19 | std::shared_ptr a_ptr = std::make_shared(); 20 | std::shared_ptr b_ptr = std::make_shared(); 21 | 22 | a_ptr->ptr = b_ptr; 23 | b_ptr->weak_ptr = a_ptr; 24 | 25 | std::cout << "counter reference of A = " << a_ptr.use_count() << std::endl; // 1 26 | std::cout << "counter reference of B = " << b_ptr.use_count() << std::endl; // 2 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /common-bugs/shared-ptr-circular-ref/bug.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class B; 7 | 8 | class A { 9 | public: 10 | std::shared_ptr ptr; // point to class B 11 | }; 12 | 13 | class B { 14 | public: 15 | std::shared_ptr ptr; // point to class A 16 | }; 17 | 18 | int main() { 19 | std::shared_ptr a_ptr = std::make_shared(); 20 | std::shared_ptr b_ptr = std::make_shared(); 21 | 22 | a_ptr->ptr = b_ptr; 23 | b_ptr->ptr = a_ptr; // circular reference 24 | 25 | std::cout << "counter reference of A = " << a_ptr.use_count() << std::endl; // 2 26 | std::cout << "counter reference of B = " << b_ptr.use_count() << std::endl; // 2 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /design-pattern/abstract-factory/abstract_factory.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "abstract_factory.h" 4 | 5 | using namespace std; 6 | 7 | void SqlserverUser::insert(User* user) { 8 | std::cout << "Insert a record to Table User in SQL Server" << std::endl; 9 | } 10 | 11 | User* SqlserverUser::get_user(int id) { 12 | std::cout << "Get a record from Table User by ID in SQL Server" << std::endl; 13 | return nullptr; 14 | } 15 | 16 | void AccessUser::insert(User* user) { 17 | std::cout << "Insert a record to Table User in Access" << std::endl; 18 | } 19 | 20 | User* AccessUser::get_user(int id) { 21 | std::cout << "Get a record from Table User by ID in Access" << std::endl; 22 | return nullptr; 23 | } 24 | 25 | void SqlserverDepartment::insert(Department* dept) { 26 | std::cout << "Insert a record to Table Department in SQL Server" << std::endl; 27 | } 28 | 29 | Department* SqlserverDepartment::get_department(int id) { 30 | std::cout << "Get a record from Table Department by ID in SQL Server" << std::endl; 31 | return nullptr; 32 | } 33 | 34 | void AccessDepartment::insert(Department* dept) { 35 | std::cout << "Insert a record to Table Department in Access" << std::endl; 36 | } 37 | 38 | Department* AccessDepartment::get_department(int id) { 39 | std::cout << "Get a record from Table Department by ID in Access" << std::endl; 40 | return nullptr; 41 | } 42 | 43 | SqlserverUser* SqlserverFactory::create_user() { 44 | return new SqlserverUser(); 45 | } 46 | 47 | SqlserverDepartment* SqlserverFactory::create_department() { 48 | return new SqlserverDepartment(); 49 | } 50 | 51 | AccessUser* AccessFactory::create_user() { 52 | return new AccessUser(); 53 | } 54 | 55 | AccessDepartment* AccessFactory::create_department() { 56 | return new AccessDepartment(); 57 | } -------------------------------------------------------------------------------- /design-pattern/abstract-factory/abstract_factory.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class User { 6 | private: 7 | std::string _name; 8 | std::string _id; 9 | }; 10 | 11 | class Department { 12 | private: 13 | std::string _name; 14 | std::string _id; 15 | }; 16 | 17 | class IUser { 18 | public: 19 | virtual void insert(User* user) = 0; 20 | virtual User* get_user(int id) = 0; 21 | }; 22 | 23 | class IDepartment { 24 | public: 25 | virtual void insert(Department* dept) = 0; 26 | virtual Department* get_department(int id) = 0; 27 | }; 28 | 29 | class SqlserverUser : public IUser { 30 | void insert(User* user) override; 31 | User* get_user(int id) override; 32 | }; 33 | 34 | class SqlserverDepartment : public IDepartment { 35 | void insert(Department* dept) override; 36 | Department* get_department(int id) override; 37 | }; 38 | 39 | class AccessUser : public IUser { 40 | void insert(User* user) override; 41 | User* get_user(int id) override; 42 | }; 43 | 44 | class AccessDepartment : public IDepartment { 45 | void insert(Department* dept) override; 46 | Department* get_department(int id) override; 47 | }; 48 | 49 | class IFactory { 50 | public: 51 | virtual IUser* create_user() = 0; 52 | virtual IDepartment* create_department() = 0; 53 | }; 54 | 55 | class SqlserverFactory : public IFactory { 56 | SqlserverUser* create_user() override; 57 | SqlserverDepartment* create_department() override; 58 | }; 59 | 60 | class AccessFactory : public IFactory { 61 | AccessUser* create_user() override; 62 | AccessDepartment* create_department() override; 63 | }; -------------------------------------------------------------------------------- /design-pattern/abstract-factory/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "abstract_factory.h" 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | User* user = new User(); 9 | Department* dept = new Department(); 10 | 11 | IFactory* factory = new SqlserverFactory(); 12 | 13 | IUser* i_user = factory->create_user(); 14 | i_user->insert(user); 15 | i_user->get_user(10); 16 | 17 | IDepartment* i_dept = factory->create_department(); 18 | i_dept->insert(dept); 19 | i_dept->get_department(10); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /design-pattern/adapter/main.cpp: -------------------------------------------------------------------------------- 1 | #include "play_adapter.h" 2 | 3 | int main() { 4 | Player* james = new Forwards("James"); 5 | james->attack(); 6 | Player* curry = new Guards("Curry"); 7 | curry->attack(); 8 | 9 | Player* yao_ming = new Translator("Yao Ming"); 10 | yao_ming->attack(); 11 | yao_ming->defense(); 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /design-pattern/adapter/play_adapter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "play_adapter.h" 4 | 5 | void Forwards::attack() { 6 | std::cout << "Forwards " << _name << " attack" << std::endl; 7 | } 8 | 9 | void Forwards::defense() { 10 | std::cout << "Forwards " << _name << " defense" << std::endl; 11 | } 12 | 13 | void Center::attack() { 14 | std::cout << "Center " << _name << " attack" << std::endl; 15 | } 16 | 17 | void Center::defense() { 18 | std::cout << "Center " << _name << " defense" << std::endl; 19 | } 20 | 21 | void Guards::attack() { 22 | std::cout << "Guards " << _name << " attack" << std::endl; 23 | } 24 | 25 | void Guards::defense() { 26 | std::cout << "Guards " << _name << " defense" << std::endl; 27 | } 28 | 29 | void ForeignCenter::set_name(const std::string &name) { 30 | this->_name = name; 31 | } 32 | 33 | std::string ForeignCenter::get_name() { 34 | return this->_name; 35 | } 36 | 37 | void ForeignCenter::cn_attack() { 38 | std::cout << "CN Center " << _name << " attack" << std::endl; 39 | } 40 | 41 | void ForeignCenter::cn_defense() { 42 | std::cout << "CN Center " << _name << " defense" << std::endl; 43 | } 44 | 45 | Translator::Translator(const std::string& name) : Player(name) { 46 | _foreign_center.set_name(_name); 47 | } 48 | 49 | void Translator::attack() { 50 | _foreign_center.cn_attack(); 51 | } 52 | 53 | void Translator::defense() { 54 | _foreign_center.cn_defense(); 55 | } -------------------------------------------------------------------------------- /design-pattern/adapter/play_adapter.h: -------------------------------------------------------------------------------- 1 | #ifndef PLAY_ADAPTER_H 2 | #define PLAY_ADAPTER_H 3 | 4 | #include 5 | 6 | class Player { 7 | public: 8 | Player() = default; 9 | 10 | explicit Player(std::string name) : _name(name) {} 11 | 12 | virtual void attack() = 0; 13 | 14 | virtual void defense() = 0; 15 | 16 | protected: 17 | std::string _name; 18 | }; 19 | 20 | class Forwards : public Player { 21 | public: 22 | explicit Forwards(const std::string& name) : Player(name) {} 23 | 24 | void attack() override; 25 | 26 | void defense() override; 27 | }; 28 | 29 | class Center : public Player { 30 | public: 31 | explicit Center(const std::string& name) : Player(name) {} 32 | 33 | void attack() override; 34 | 35 | void defense() override; 36 | }; 37 | 38 | class Guards : public Player { 39 | public: 40 | explicit Guards(const std::string& name) : Player(name) {} 41 | 42 | void attack() override; 43 | 44 | void defense() override; 45 | }; 46 | 47 | class ForeignCenter { 48 | public: 49 | void set_name(const std::string& name); 50 | 51 | std::string get_name(); 52 | 53 | void cn_attack(); 54 | 55 | void cn_defense(); 56 | 57 | private: 58 | std::string _name; 59 | }; 60 | 61 | class Translator : public Player { 62 | public: 63 | explicit Translator(const std::string& name); 64 | 65 | void attack() override; 66 | 67 | void defense() override; 68 | 69 | private: 70 | ForeignCenter _foreign_center; 71 | }; 72 | 73 | #endif // PLAY_ADAPTER_H -------------------------------------------------------------------------------- /design-pattern/decoractor/decoractor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "decoractor.h" 4 | 5 | using namespace std; 6 | 7 | Person::Person(std::string name) { 8 | _name = name; 9 | } 10 | 11 | void Person::show() { 12 | std::cout << "Decoractor: " << _name << std::endl; 13 | } 14 | 15 | void Finery::decorate(Human* component) { 16 | _component = component; 17 | } 18 | 19 | void Finery::show() { 20 | _component->show(); 21 | } 22 | 23 | void Tshirts::show() { 24 | std::cout << "Tshirts "; 25 | Finery::show(); 26 | } 27 | 28 | void BigTrouser::show() { 29 | std::cout << "BigTrouser "; 30 | Finery::show(); 31 | } 32 | 33 | void Sneakers::show() { 34 | std::cout << "Sneakers "; 35 | Finery::show(); 36 | } 37 | 38 | void LeatherShoes::show() { 39 | std::cout << "LeatherShoes "; 40 | Finery::show(); 41 | } 42 | 43 | void Tie::show() { 44 | std::cout << "Tie "; 45 | Finery::show(); 46 | } 47 | 48 | void Suit::show() { 49 | std::cout << "Suit "; 50 | Finery::show(); 51 | } -------------------------------------------------------------------------------- /design-pattern/decoractor/decoractor.h: -------------------------------------------------------------------------------- 1 | #ifndef DECORACTOR_H 2 | #define DECORACTOR_H 3 | 4 | #include 5 | 6 | class Human { 7 | public: 8 | Human() {}; 9 | Human(std::string name) {}; 10 | virtual void show() = 0; 11 | 12 | protected: 13 | std::string _name; 14 | }; 15 | 16 | class Person : public Human { 17 | public: 18 | explicit Person(std::string name); 19 | void show() override; 20 | }; 21 | 22 | class Finery : public Human { 23 | public: 24 | Finery() {}; 25 | void decorate(Human* component); 26 | void show() override; 27 | 28 | protected: 29 | Human* _component; 30 | }; 31 | 32 | class Tshirts : public Finery { 33 | public: 34 | void show() override; 35 | }; 36 | 37 | class BigTrouser : public Finery { 38 | public: 39 | void show() override; 40 | }; 41 | 42 | class Sneakers : public Finery { 43 | public: 44 | void show() override; 45 | }; 46 | 47 | class LeatherShoes : public Finery { 48 | public: 49 | void show() override; 50 | }; 51 | 52 | class Tie : public Finery { 53 | public: 54 | void show() override; 55 | }; 56 | 57 | class Suit : public Finery { 58 | public: 59 | void show() override; 60 | }; 61 | 62 | #endif -------------------------------------------------------------------------------- /design-pattern/decoractor/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "decoractor.h" 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | try { 9 | Person* p = new Person("enbin"); 10 | 11 | std::cout << "First Instance:" << std::endl; 12 | Sneakers* s = new Sneakers(); 13 | BigTrouser* b = new BigTrouser(); 14 | Tshirts* t = new Tshirts(); 15 | s->decorate(p); 16 | b->decorate(s); 17 | t->decorate(b); 18 | t->show(); 19 | } catch (char* msg) { 20 | std::cerr << msg << std::endl; 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /design-pattern/factory-method/factory_method.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "factory_method.h" 4 | 5 | using namespace std; 6 | 7 | void LeiFeng::sweep() { 8 | std::cout << "sweep()" << std::endl; 9 | } 10 | 11 | void LeiFeng::wash() { 12 | std::cout << "wash()" << std::endl; 13 | } 14 | 15 | void LeiFeng::buy_rice() { 16 | std::cout << "buy_rice()" << std::endl; 17 | } -------------------------------------------------------------------------------- /design-pattern/factory-method/factory_method.h: -------------------------------------------------------------------------------- 1 | #ifndef FACTORY_METHOD_H 2 | #define FACTORY_METHOD_H 3 | 4 | class LeiFeng { 5 | public: 6 | LeiFeng() {}; 7 | void sweep(); 8 | void wash(); 9 | void buy_rice(); 10 | }; 11 | 12 | class ILeiFeng { 13 | public: 14 | ILeiFeng() {}; 15 | virtual LeiFeng* create_leifeng() const = 0; 16 | }; 17 | 18 | class Undergradute : public LeiFeng {}; 19 | 20 | class Volunteer : public LeiFeng {}; 21 | 22 | class UndergraduteLeiFeng : public ILeiFeng { 23 | public: 24 | UndergraduteLeiFeng() {}; 25 | LeiFeng* create_leifeng() const override { 26 | return new Undergradute(); 27 | } 28 | }; 29 | 30 | class VolunteerLeiFeng : public ILeiFeng { 31 | public: 32 | VolunteerLeiFeng() {}; 33 | LeiFeng* create_leifeng() const override { 34 | return new Volunteer(); 35 | } 36 | }; 37 | 38 | #endif -------------------------------------------------------------------------------- /design-pattern/factory-method/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "factory_method.h" 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | try { 9 | ILeiFeng* factory = new VolunteerLeiFeng(); 10 | LeiFeng* volunteer = factory->create_leifeng(); 11 | volunteer->sweep(); 12 | volunteer->wash(); 13 | volunteer->buy_rice(); 14 | } catch (char* msg) { 15 | std::cerr << msg << std::endl; 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /design-pattern/simple-factory/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "simple_factory.h" 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | try { 9 | std::cout << "Enter two numbers separated by a space:" << std::endl; 10 | std::string number_first, number_second; 11 | std::cin >> number_first >> number_second; 12 | 13 | std::cout << "Input operator (+ or -)" << std::endl; 14 | char operator_str; 15 | std::cin >> operator_str; 16 | 17 | Operation* op; 18 | op = OperationFactory::create_operation(operator_str); 19 | op->set_number_first(stod(number_first)); 20 | op->set_number_second(stod(number_second)); 21 | 22 | double res = op->get_result(); 23 | std::cout << "Result is " << res << std::endl; 24 | } catch (char* msg) { 25 | std::cerr << msg << std::endl; 26 | } 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /design-pattern/simple-factory/simple_factory.cpp: -------------------------------------------------------------------------------- 1 | #include "simple_factory.h" 2 | 3 | Operation* OperationFactory::create_operation(const char operation) { 4 | Operation* op; 5 | 6 | switch (operation) { 7 | case '+': 8 | op = new OperationAdd(); 9 | break; 10 | case '-': 11 | op = new OperationSub(); 12 | break; 13 | } 14 | 15 | return op; 16 | } -------------------------------------------------------------------------------- /design-pattern/simple-factory/simple_factory.h: -------------------------------------------------------------------------------- 1 | #ifndef SIMPLE_FACTORY_H 2 | #define SIMPLE_FACTORY_H 3 | 4 | class Operation { 5 | public: 6 | virtual double get_result() const = 0; 7 | 8 | void set_number_first(double number) { 9 | _number_first = number; 10 | } 11 | 12 | void set_number_second(double number) { 13 | _number_second = number; 14 | } 15 | 16 | protected: 17 | double _number_first; 18 | double _number_second; 19 | }; 20 | 21 | class OperationAdd : public Operation { 22 | public: 23 | double get_result() const override { 24 | return _number_first + _number_second; 25 | } 26 | }; 27 | 28 | class OperationSub : public Operation { 29 | public: 30 | double get_result() const override { 31 | return _number_first - _number_second; 32 | } 33 | }; 34 | 35 | class OperationFactory { 36 | public: 37 | static Operation* create_operation(const char operation); 38 | }; 39 | 40 | #endif -------------------------------------------------------------------------------- /gdb-instance/deadlock/README.md: -------------------------------------------------------------------------------- 1 | # deadlock 2 | 3 | Compile files 4 | 5 | ```main 6 | g++ main.cpp -o main -std=c++11 -lpthread 7 | ``` 8 | 9 | Running 10 | 11 | ```bash 12 | gdb ./main 13 | ``` 14 | 15 | `set pagination off` help us close the pagination. `set scheduler-locking on` start scheduler locks to prevent gdb debugging interfering with multi-threaded execution. 16 | 17 | ```bash 18 | set pagination off 19 | set scheduler-locking on 20 | ``` 21 | 22 | Start gdb debugging and it will stop at the point where the mutex lock is acquired. 23 | 24 | ```bash 25 | run 26 | ``` 27 | 28 | Show all thread information 29 | 30 | ```bash 31 | info threads 32 | ``` 33 | 34 | Enter thread #2 and use `bt` to view the stack information. You will observe that the `futex_wait` function is waiting to acquire a lock. Enter thread #3 and you will observe a similar result. This shows that the two threads have been waiting for each other's mutex locks. 35 | 36 | ```bash 37 | thread 2 38 | bt 39 | ``` 40 | 41 | Exit gdb 42 | 43 | ```bash 44 | quit 45 | ``` -------------------------------------------------------------------------------- /gdb-instance/deadlock/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | std::mutex mutex1; 8 | std::mutex mutex2; 9 | 10 | void thread1_func() { 11 | std::unique_lock lock1(mutex1); 12 | std::cout << "Thread 1: acquire mutex1" << std::endl; 13 | 14 | std::this_thread::sleep_for(std::chrono::seconds(1)); 15 | 16 | std::unique_lock lock2(mutex2); 17 | std::cout << "Thread 2: acquire mutex2" << std::endl; // dead lock 18 | 19 | lock2.unlock(); 20 | lock1.unlock(); 21 | } 22 | 23 | void thread2_func() { 24 | std::unique_lock lock2(mutex2); 25 | std::cout << "Thread 2: acquire mutex1" << std::endl; 26 | 27 | std::this_thread::sleep_for(std::chrono::seconds(1)); 28 | 29 | std::unique_lock lock1(mutex1); 30 | std::cout << "Thread 1: acquire mutex1" << std::endl; 31 | 32 | lock1.unlock(); 33 | lock2.unlock(); 34 | } 35 | 36 | int main() { 37 | std::thread thread1(thread1_func); 38 | std::thread thread2(thread2_func); 39 | 40 | thread1.join(); 41 | thread2.join(); 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /gdb-instance/high-cpu/README.md: -------------------------------------------------------------------------------- 1 | # high-cpu 2 | 3 | Compile files 4 | 5 | ```bash 6 | g++ main.cpp -o main -std=c++11 -lpthread 7 | ``` 8 | 9 | Running 10 | 11 | ```bash 12 | ./main 13 | ``` 14 | 15 | Start gdb and load the executable file 16 | 17 | ```bash 18 | gdb ./main $(pidof main) 19 | ``` 20 | 21 | Show all thread information 22 | 23 | ```bash 24 | info threads 25 | ``` 26 | 27 | Switch to thread #2 28 | 29 | ```bash 30 | thread 2 31 | ``` 32 | 33 | View stack information for thread #2. You will see that function `compute_task()` has been running the computation task 34 | 35 | ```bash 36 | bt 37 | ``` -------------------------------------------------------------------------------- /gdb-instance/high-cpu/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void compute_task() { 8 | while (1) { 9 | for (int i = 0; i < 10000000; i++) { 10 | double x = i * i * i; 11 | } 12 | } 13 | } 14 | 15 | int main() { 16 | std::vector threads; 17 | 18 | for (int i = 0; i < 2; i++) { 19 | threads.emplace_back(compute_task); 20 | } 21 | 22 | for (auto& thread : threads) { 23 | thread.join(); 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /grammar/lambda.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | std::vector nums {1, 2, 3, 4, 5, 6, 7}; 10 | int x = 5; 11 | 12 | // delete all numbers less than 5 in 'nums' 13 | nums.erase(std::remove_if(nums.begin(), nums.end(), [x](int n) { return n < x; }), nums.end()); 14 | 15 | for (auto num : nums) { 16 | std::cout << num << std::endl; 17 | } 18 | 19 | auto func1 = [](int i) { return i + 4; }; 20 | std::cout << "func1: " << func1(6) << std::endl; 21 | 22 | std::function func2 = [](int i) { return i + 4; }; 23 | std::cout << "func2: " << func2(6) << std::endl; 24 | } 25 | -------------------------------------------------------------------------------- /grammar/move.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void get_size(std::vector&& vec) { 8 | std::cout << "numbers size = " << vec.size() << std::endl; 9 | } 10 | 11 | int main() { 12 | std::vector numbers = {1, 2, 3, 4, 5}; 13 | get_size(std::move(numbers)); 14 | 15 | numbers.clear(); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /grammar/template/class_template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | template 6 | class Blob { 7 | public: 8 | typedef T value_type; 9 | typedef typename std::vector::size_type size_type; 10 | 11 | Blob() : _data(std::make_shared>()) {} 12 | Blob(std::initializer_list init_list) : _data(std::make_shared>(init_list)) {} 13 | 14 | size_type size() const { 15 | return _data->size(); 16 | } 17 | 18 | bool empty() const { 19 | return _data->empty(); 20 | } 21 | 22 | void push_back(const T& t) { 23 | _data->push_back(t); 24 | } 25 | 26 | void push_back(T&& t) { 27 | _data->push_back(std::move(t)); 28 | } 29 | 30 | void pop_back() { 31 | check(0, "pop_back on empty Blob"); 32 | _data->pop_back(); 33 | } 34 | 35 | T& back() { 36 | check(0, "back on empty Blob"); 37 | return _data->back(); 38 | } 39 | 40 | T& operator[](size_type i) { 41 | check(0, "subscript out of range"); 42 | return (*_data)[i]; 43 | } 44 | 45 | private: 46 | std::shared_ptr> _data; 47 | 48 | void check(size_type i, const std::string& msg) const { 49 | if (i >= _data->size()) { 50 | throw std::out_of_range(msg); 51 | } 52 | } 53 | }; 54 | 55 | int main() { 56 | Blob b; 57 | Blob b_init = {0, 1, 2, 3, 4}; 58 | 59 | b_init.push_back(5); 60 | std::cout << b_init.size() << std::endl; 61 | std::cout << b_init[2] << std::endl; 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /grammar/template/function_template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | int compare_func(const T& val1, const T& val2) { 6 | return val1 <= val2 ? (val1 == val2 ? 0 : -1) : 1; 7 | } 8 | 9 | int main() { 10 | std::cout << compare_func(2, 1) << std::endl; 11 | 12 | std::vector vec1 = {1, 2, 3}, vec2 = {4, 5, 6}; 13 | std::cout << compare_func(vec1, vec2) << std::endl; 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /hand-written/RAII/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Resource { 7 | public: 8 | Resource() { 9 | std::cout << "Resource()" << std::endl; 10 | } 11 | 12 | ~Resource() { 13 | std::cout << "~Resource()" << std::endl; 14 | } 15 | 16 | void work_func() { 17 | std::cout << "work_func()" << std::endl; 18 | } 19 | }; 20 | 21 | class ResourceManager { 22 | public: 23 | ResourceManager() : _resource_ptr(std::make_unique()) { // C++14 24 | std::cout << "ResourceManager()" << std::endl; 25 | } 26 | 27 | void work_with_resource() { 28 | _resource_ptr->work_func(); 29 | } 30 | 31 | private: 32 | std::unique_ptr _resource_ptr; 33 | }; 34 | 35 | int main() { 36 | ResourceManager manager; 37 | manager.work_with_resource(); 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /hand-written/class_default_func/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Basic { 6 | public: 7 | Basic(size_t* data) : _data(new size_t(0)) { 8 | 9 | } 10 | 11 | Basic(const Basic& other) : _data(other._data) { 12 | 13 | } 14 | 15 | Basic& operator=(const Basic& other) { 16 | if (this != &other) { 17 | delete _data; 18 | _data = new size_t(*other._data); 19 | } 20 | 21 | return *this; 22 | } 23 | 24 | Basic(Basic&& other) { 25 | _data = other._data; 26 | other._data = nullptr; 27 | } 28 | 29 | Basic& operator=(Basic&& other) { 30 | if (this != &other) { 31 | delete _data; 32 | _data = other._data; 33 | other._data = nullptr; 34 | } 35 | 36 | return *this; 37 | } 38 | 39 | ~Basic() { 40 | if (_data) { 41 | delete _data; 42 | _data = nullptr; 43 | } 44 | } 45 | 46 | private: 47 | size_t* _data; 48 | }; -------------------------------------------------------------------------------- /hand-written/singleton/hungry.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Singleton { 10 | public: 11 | static Singleton* get_instance(); 12 | void print(); 13 | 14 | private: 15 | friend class AutoRelease; 16 | Singleton(); 17 | ~Singleton(); 18 | Singleton(const Singleton& signal) = delete; 19 | const Singleton& operator=(const Singleton& signal) = delete; 20 | 21 | private: 22 | static Singleton *g_Instance; 23 | 24 | private: 25 | class AutoRelease { 26 | public: 27 | AutoRelease() { 28 | std::cout << "AutoRelease()" << std::endl; 29 | } 30 | ~AutoRelease() { 31 | if (g_Instance) { 32 | delete g_Instance; 33 | g_Instance = nullptr; 34 | } 35 | std::cout << "~AutoRelease()" << std::endl; 36 | } 37 | }; 38 | 39 | private: 40 | static AutoRelease m_At; 41 | }; 42 | 43 | /* create an instance when code runs */ 44 | Singleton *Singleton::g_Instance = new Singleton; 45 | Singleton::AutoRelease Singleton::m_At; 46 | 47 | Singleton* Singleton::get_instance() { 48 | return g_Instance; 49 | } 50 | 51 | void Singleton::print() { 52 | std::cout << "addr: " << this << std::endl; 53 | } 54 | 55 | Singleton::Singleton() { 56 | std::cout << "Singleton()" << std::endl; 57 | } 58 | 59 | Singleton::~Singleton() { 60 | std::cout << "~Singleton()" << std::endl; 61 | } 62 | 63 | // operator func 64 | void test() { 65 | Singleton::get_instance()->print(); 66 | } 67 | 68 | int main() { 69 | // create three threads 70 | thread t1(test); 71 | thread t2(test); 72 | thread t3(test); 73 | 74 | // threads joined 75 | t1.join(); 76 | t2.join(); 77 | t3.join(); 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /hand-written/singleton/local-static-var-lazy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Singleton { 10 | public: 11 | static Singleton& get_instance(); 12 | void print(); 13 | 14 | private: 15 | Singleton(); 16 | ~Singleton(); 17 | Singleton(const Singleton& signal) = delete; 18 | const Singleton& operator=(const Singleton& signal) = delete; 19 | }; 20 | 21 | Singleton& Singleton::get_instance() { 22 | static Singleton singal; // static var 23 | return singal; 24 | } 25 | 26 | void Singleton::print() { 27 | std::cout << "addr: " << this << std::endl; 28 | } 29 | 30 | Singleton::Singleton() { 31 | std::cout << "Singleton()" << std::endl; 32 | } 33 | 34 | Singleton::~Singleton() { 35 | std::cout << "~Singleton()" << std::endl; 36 | } 37 | 38 | // operator func 39 | void test() { 40 | Singleton::get_instance().print(); 41 | } 42 | 43 | int main() { 44 | // create three threads 45 | thread t1(test); 46 | thread t2(test); 47 | thread t3(test); 48 | 49 | // threads joined 50 | t1.join(); 51 | t2.join(); 52 | t3.join(); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /hand-written/singleton/lock-lazy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Singleton { 10 | public: 11 | static Singleton* get_instance(); 12 | void print(); 13 | 14 | private: 15 | friend class AutoRelease; 16 | Singleton(); 17 | ~Singleton(); 18 | Singleton(const Singleton& signal) = delete; 19 | const Singleton& operator=(const Singleton& signal) = delete; 20 | 21 | private: 22 | static Singleton *m_Instance; 23 | static std::mutex m_Mutex; 24 | 25 | private: 26 | class AutoRelease { 27 | public: 28 | AutoRelease() { 29 | std::cout << "AutoRelease()" << std::endl; 30 | } 31 | ~AutoRelease() { 32 | std::unique_lock lock(m_Mutex); 33 | if (m_Instance) { 34 | delete m_Instance; 35 | m_Instance = nullptr; 36 | } 37 | std::cout << "~AutoRelease()" << std::endl; 38 | } 39 | }; 40 | 41 | private: 42 | static AutoRelease m_At; 43 | }; 44 | 45 | Singleton *Singleton::m_Instance = nullptr; 46 | std::mutex Singleton::m_Mutex; 47 | Singleton::AutoRelease Singleton::m_At; 48 | 49 | Singleton* Singleton::get_instance() { 50 | if (m_Instance == nullptr) { 51 | std::unique_lock lock(m_Mutex); 52 | if (m_Instance == nullptr) { 53 | m_Instance = new Singleton; 54 | } 55 | } 56 | 57 | return m_Instance; 58 | } 59 | 60 | void Singleton::print() { 61 | std::cout << "addr: " << this << std::endl; 62 | } 63 | 64 | Singleton::Singleton() { 65 | std::cout << "Singleton()" << std::endl; 66 | } 67 | 68 | Singleton::~Singleton() { 69 | std::cout << "~Singleton()" << std::endl; 70 | } 71 | 72 | // operator func 73 | void test() { 74 | Singleton::get_instance()->print(); 75 | } 76 | 77 | int main() { 78 | // create three threads 79 | thread t1(test); 80 | thread t2(test); 81 | thread t3(test); 82 | 83 | // threads joined 84 | t1.join(); 85 | t2.join(); 86 | t3.join(); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /hand-written/threadpool/threadpool.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | class ThreadPool { 13 | public: 14 | ThreadPool(size_t numThreads) : stop(false) { 15 | for (size_t i = 0; i < numThreads; ++i) { 16 | threads.emplace_back([this] { 17 | while (true) { 18 | std::function task; 19 | { 20 | std::unique_lock lock(queueMutex); // lock 21 | condition.wait(lock, [this] { return stop || !tasks.empty(); }); 22 | if (stop && tasks.empty()) { 23 | return; 24 | } 25 | task = std::move(tasks.front()); 26 | tasks.pop(); 27 | } 28 | task(); 29 | } 30 | }); 31 | } 32 | } 33 | 34 | template 35 | void enqueue(F&& f, Args&&... args) { 36 | { 37 | std::unique_lock lock(queueMutex); 38 | tasks.emplace([f, args...] { f(args...); }); 39 | } 40 | condition.notify_one(); // wake up thread 41 | } 42 | 43 | ~ThreadPool() { 44 | { 45 | std::unique_lock lock(queueMutex); 46 | stop = true; 47 | } 48 | condition.notify_all(); 49 | for (std::thread& thread : threads) { 50 | thread.join(); 51 | } 52 | } 53 | 54 | private: 55 | std::vector threads; 56 | std::queue> tasks; 57 | std::mutex queueMutex; 58 | std::condition_variable condition; 59 | bool stop; 60 | }; 61 | 62 | void printNumber(int num) { 63 | sleep(5); // ssimulate operational implementation 64 | std::cout << num << std::endl; 65 | } 66 | 67 | int main() { 68 | ThreadPool pool(4); 69 | 70 | // submit tasks to thread pool 71 | for (int i = 0; i < 10; ++i) { 72 | pool.enqueue(printNumber, i); 73 | } 74 | 75 | // wait for all tasks to be completed 76 | std::this_thread::sleep_for(std::chrono::seconds(1)); 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /hand-written/tiny_shared_ptr/tiny_shared_ptr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "tiny_shared_ptr.h" 4 | 5 | using namespace std; 6 | 7 | class Test { 8 | public: 9 | Test() { 10 | std::cout << "Test()" << std::endl; 11 | } 12 | 13 | ~Test() { 14 | std::cout << "~Test()" << std::endl; 15 | } 16 | 17 | void test_opreation() { 18 | std::cout << "test_opreation()" << std::endl; 19 | } 20 | }; 21 | 22 | int main() { 23 | tiny_shared_ptr ptr(new Test); 24 | tiny_shared_ptr ptr_copy = ptr; 25 | 26 | std::cout << "ptr count = " << ptr.use_count() << std::endl; 27 | std::cout << "ptr_copy count = " << ptr_copy.use_count() << std::endl; 28 | 29 | ptr->test_opreation(); 30 | 31 | tiny_shared_ptr ptr_empty; 32 | std::cout << "ptr_empty count = " << ptr_empty.use_count() << std::endl; 33 | 34 | { 35 | tiny_shared_ptr ptr_local(new Test); 36 | std::cout << "ptr_local count = " << ptr_local.use_count() << std::endl; 37 | } 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /hand-written/tiny_shared_ptr/tiny_shared_ptr.h: -------------------------------------------------------------------------------- 1 | template 2 | class tiny_shared_ptr { 3 | public: 4 | tiny_shared_ptr(T* ptr = nullptr) : _ptr(ptr), _count(new size_t(1)) { 5 | if (_ptr == nullptr) { 6 | *_count = 0; 7 | } 8 | } 9 | 10 | tiny_shared_ptr(const tiny_shared_ptr& other) : _ptr(other._ptr), _count(other._count) { 11 | if (_ptr) { 12 | (*_count)++; 13 | } 14 | } 15 | 16 | tiny_shared_ptr& operator=(const tiny_shared_ptr& other) { 17 | if (this != other) { 18 | release_source(); 19 | _ptr = other._ptr; 20 | _count = other._count; 21 | if (_ptr) { 22 | (*_count)++; 23 | } 24 | } 25 | 26 | return *this; 27 | } 28 | 29 | T* operator->() { 30 | return _ptr; 31 | } 32 | 33 | int use_count() const { 34 | return (_count ? *_count : 0); 35 | } 36 | 37 | ~tiny_shared_ptr() { 38 | release_source(); 39 | } 40 | 41 | private: 42 | void release_source() { 43 | if (_count) { 44 | (*_count)--; 45 | if (*_count == 0) { 46 | delete _ptr; 47 | delete _count; 48 | } 49 | } 50 | } 51 | 52 | T* _ptr; 53 | size_t* _count; 54 | }; -------------------------------------------------------------------------------- /interview/get-asynchronous-res-multi-thread/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void worker_func(std::promise& promise) { 9 | int res = 10; 10 | std::this_thread::sleep_for(std::chrono::seconds(5)); // work operation 11 | promise.set_value(res); 12 | } 13 | 14 | int main() { 15 | std::promise promise; 16 | std::future future = promise.get_future(); // bind 17 | 18 | std::thread thread_worker(worker_func, std::ref(promise)); // get future by promise 19 | 20 | int res = future.get(); 21 | std::cout << "future result = " << res << std::endl; 22 | 23 | thread_worker.join(); 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /interview/read-file-multi-threads/file1: -------------------------------------------------------------------------------- 1 | 1 2 | 3 3 | 5 4 | 7 5 | -------------------------------------------------------------------------------- /interview/read-file-multi-threads/file2: -------------------------------------------------------------------------------- 1 | 2 2 | 4 3 | 6 4 | 8 5 | -------------------------------------------------------------------------------- /interview/read-file-multi-threads/file3: -------------------------------------------------------------------------------- 1 | 9 2 | 10 3 | -------------------------------------------------------------------------------- /interview/read-file-multi-threads/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | static int sum = 0; 10 | std::mutex mtx; 11 | 12 | void process_file(const std::string& filename) { 13 | int file_sum = 0; 14 | 15 | std::ifstream file(filename); 16 | if (file.is_open()) { 17 | int num = 0; 18 | while (file >> num) { 19 | file_sum += num; 20 | } 21 | file.close(); 22 | } else { 23 | std::cout << "Failed to open file " << filename << std::endl; 24 | } 25 | 26 | std::lock_guard lock(mtx); 27 | sum += file_sum; 28 | } 29 | 30 | int main() { 31 | std::vector filelist = {"file1", "file2", "file3"}; 32 | std::vector threads; 33 | 34 | for (const std::string& filename : filelist) { 35 | threads.emplace_back(process_file, filename); 36 | } 37 | 38 | for (std::thread& t : threads) { 39 | t.join(); 40 | } 41 | 42 | std::cout << "sum = " << sum << std::endl; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /interview/simple-asynchronous-tasks/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int get_sum(int a, int b) { 8 | std::this_thread::sleep_for(std::chrono::seconds(2)); // sleep two seconds 9 | 10 | return a + b; 11 | } 12 | 13 | int main() { 14 | // creat asynchronous task 15 | std::future future_res = std::async(std::launch::async, get_sum, 2, 3); 16 | std::cout << "Doing some work..." << std::endl; 17 | 18 | int res = future_res.get(); // get asynchronous task result 19 | std::cout << "sum = " << res < 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Test { 7 | public: 8 | Test(const std::string& str) : m_data(str) { 9 | std::cout << "Test(): " << m_data << std::endl; 10 | } 11 | 12 | Test(const Test& other) : m_data(other.m_data) { 13 | std::cout << "Copy Test(): " << m_data << std::endl; 14 | } 15 | 16 | private: 17 | std::string m_data; 18 | }; 19 | 20 | int main() { 21 | std::vector vec; 22 | vec.reserve(5); 23 | 24 | Test t1("t1"); 25 | vec.push_back(t1); 26 | 27 | vec.emplace_back("t2"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /stl/vector/sort_range.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class MyClass { 8 | public: 9 | MyClass(int val) : _val(val) {} 10 | 11 | bool operator<(const MyClass& other) const { 12 | return _val < other._val; 13 | } 14 | 15 | public: 16 | int _val; 17 | }; 18 | 19 | int main() { 20 | std::vector vec; 21 | 22 | vec.push_back(MyClass(10)); 23 | vec.push_back(MyClass(5)); 24 | vec.push_back(MyClass(15)); 25 | vec.push_back(MyClass(10)); 26 | 27 | std::sort(vec.begin(), vec.end()); 28 | 29 | for (const auto& obj : vec) { 30 | std::cout << obj._val << std::endl; 31 | } 32 | 33 | int target = 10; 34 | auto range = std::equal_range(vec.begin(), vec.end(), MyClass(target)); 35 | if (range.first != range.second) { 36 | std::cout << "Found " << target << " in range [" << (*range.first)._val << ", " << (*(range.second - 1))._val << "]" << std::endl; 37 | } else { 38 | std::cout << "Not Found" << std::endl; 39 | } 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /valgrind/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EnbinYang/cpp-tips/ca512684c0f5515a90cd894328958773b58c3687/valgrind/README.md -------------------------------------------------------------------------------- /valgrind/push_back_leak: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EnbinYang/cpp-tips/ca512684c0f5515a90cd894328958773b58c3687/valgrind/push_back_leak -------------------------------------------------------------------------------- /valgrind/push_back_leak.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | std::vector* vec = new std::vector; 7 | 8 | for (int i = 0; i < 1000; i++) { 9 | vec->push_back(i); 10 | } 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /written-exam/comma_input.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | std::string input; 9 | std::getline(std::cin, input); 10 | 11 | std::vector nums; 12 | 13 | std::istringstream iss(input); 14 | std::string token; 15 | while (std::getline(iss, token, ',')) { 16 | nums.push_back(std::stoi(token)); 17 | } 18 | 19 | for (auto num : nums) { 20 | std::cout << num << std::endl; 21 | } 22 | 23 | return 0; 24 | } --------------------------------------------------------------------------------