├── stack ├── todo.txt ├── README.md ├── size.md ├── empty.md ├── top.md ├── push.md ├── pop.md └── emplace.md ├── queue ├── todo.txt ├── README.md ├── empty.md ├── size.md ├── back.md ├── push.md ├── front.md ├── emplace.md ├── pop.md └── queue.md ├── vector ├── todo.txt ├── pop_back.md ├── begin.md ├── push_back.md ├── swap.md ├── end.md ├── emplace_back.md ├── emplace.md ├── rbegin.md ├── sort.md ├── at.md ├── rend.md ├── reserve.md ├── capacity.md ├── size.md ├── back.md ├── front.md ├── crbegin.md ├── data.md ├── insert.md ├── shrink_to_fit.md ├── max_size.md ├── crend.md ├── empty.md ├── cbegin.md └── clear.md ├── set ├── todo.txt ├── insert.md ├── end.md ├── empty.md ├── begin.md ├── cend.md ├── clear.md ├── cbegin.md ├── size.md ├── rend.md ├── rbegin.md ├── equal_range.md ├── lower_bound.md ├── swap.md ├── crbegin.md ├── emplace_hint.md ├── max_size.md ├── find.md └── count.md ├── list ├── todo.txt ├── emplace_back.md ├── begin.md ├── clear.md ├── rbegin.md ├── rend.md ├── sort.md ├── assign.md ├── cend.md ├── back.md ├── merge.md ├── crend.md ├── size.md ├── cbegin.md ├── crbegin.md ├── front.md ├── push_back.md ├── max_size.md ├── emplace.md ├── empty.md ├── swap.md ├── splice.md ├── remove.md ├── end.md └── insert.md ├── logo ├── stl.png ├── logo_short.png ├── new_logo_1.jpg └── new_logo_2.jpg ├── map ├── todo.txt ├── empty.cpp ├── max_size.md ├── end.md ├── rend.md ├── find.md ├── begin.md ├── count.md ├── insert.md ├── contains.md ├── size.md ├── equal_range.md ├── clear.md ├── rbegin.md ├── cend.md └── empty.md ├── unordered_map ├── todo.txt ├── bucket.md ├── README.md └── size.md ├── .github ├── workflows │ └── main.yml ├── ISSUE_TEMPLATE │ ├── suggestion-feedback.md │ └── bug_report.md ├── pull_request_template.md └── FUNDING.yml ├── priority_queue ├── README.md ├── size.md ├── top.md ├── push.md └── pop.md ├── algorithm ├── min.md ├── max.md ├── max_element.md ├── min_element.md ├── swap.md ├── reverse.md ├── find.md ├── count_if.md ├── stable_sort.md ├── count.md ├── for_each.md ├── for_each_n.md ├── all_of.md ├── any_of.md ├── todo.txt ├── copy_n.md ├── is_sorted.md ├── adjacent_find.md ├── copy.md ├── remove.md ├── find_if.md ├── find_if_not.md ├── sort.md ├── fill.md ├── is_permutation.md ├── iota.md ├── minmax.md ├── none_of.md ├── replace.md ├── pop_heap.md ├── lower_bound.md ├── nth_element.md ├── transform.md ├── unique_copy.md ├── unique.md ├── generate.md ├── generate_n.md ├── iter_swap.md ├── search.md ├── copy_if.md ├── remove_if.md ├── shuffle.md ├── find_first_of.md ├── adjacent_difference.md ├── replace_if.md ├── move.md ├── transform_reduce.md ├── reverse_copy.md ├── equal_range.md ├── copy_backward.md ├── partial_sort.md ├── equal.md ├── is_heap.md ├── stable_partition.md ├── is_sorted_until.md ├── merge.md ├── minimax_element.md ├── minmax_element.md ├── sort_heap.md ├── clamp.md ├── accumulate.md ├── find_end.md └── make_heap.md ├── snippets ├── list │ ├── sample.cpp │ ├── Makefile │ ├── max_size.cpp │ ├── reverse.cpp │ ├── swap.cpp │ ├── emplace.cpp │ └── pop_back.cpp ├── map │ ├── sample.cpp │ ├── Makefile │ ├── begin_and_end.cpp │ ├── emplace.cpp │ ├── contains.cpp │ ├── insert.cpp │ └── size.cpp ├── queue │ ├── sample.cpp │ ├── Makefile │ ├── empty.cpp │ ├── size.cpp │ ├── back.cpp │ ├── front.cpp │ ├── push.cpp │ ├── emplace.cpp │ └── pop.cpp ├── stack │ ├── sample.cpp │ ├── Makefile │ ├── push.cpp │ ├── pop.cpp │ └── size.cpp ├── vector │ ├── sample.cpp │ ├── Makefile │ ├── at.cpp │ ├── begin.cpp │ ├── front.cpp │ ├── back.cpp │ ├── empty.cpp │ ├── end.cpp │ ├── rbegin.cpp │ ├── rend.cpp │ ├── data.cpp │ ├── max_size.cpp │ ├── reserve.cpp │ ├── crbegin.cpp │ ├── shrink_to_fit.cpp │ ├── get_allocator.cpp │ ├── sort.cpp │ ├── crend.cpp │ ├── cbegin.cpp │ ├── emplace_back.cpp │ ├── emplace.cpp │ └── capacity.cpp ├── algorithm │ ├── sample.cpp │ ├── stable_sort.cpp │ ├── max.cpp │ ├── min.cpp │ ├── Makefile │ ├── max_element.cpp │ ├── min_element.cpp │ ├── swap.cpp │ ├── partial_sort.cpp │ ├── make_heap.cpp │ ├── minmax_element.cpp │ ├── is_sorted.cpp │ ├── pop_heap.cpp │ ├── to_string.cpp │ ├── copy.cpp │ ├── copy_n.cpp │ ├── remove.cpp │ ├── lower_bound.cpp │ ├── minmax.cpp │ ├── sort_heap.cpp │ ├── replace.cpp │ ├── __gcd.cpp │ ├── unique_copy.cpp │ ├── remove_if.cpp │ ├── copy_if.cpp │ ├── fill.cpp │ ├── count.cpp │ ├── fill_n.cpp │ ├── copy_backward.cpp │ ├── search.cpp │ ├── generate_n.md │ ├── equal_range.cpp │ ├── random_shuffle.cpp │ ├── replace_if.cpp │ ├── equal.cpp │ ├── merge.cpp │ ├── nth_element.cpp │ ├── find_end.cpp │ ├── transform.cpp │ └── remove_copy.cpp ├── unordered_map │ ├── Makefile │ ├── bucket.cpp │ └── count.cpp └── set │ ├── Makefile │ ├── rbegin.cpp │ ├── crbegin.cpp │ └── swap.cpp ├── .gitignore └── .travis.yml /stack/todo.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /queue/todo.txt: -------------------------------------------------------------------------------- 1 | ~queue -------------------------------------------------------------------------------- /vector/todo.txt: -------------------------------------------------------------------------------- 1 | crend 2 | ~vector 3 | -------------------------------------------------------------------------------- /set/todo.txt: -------------------------------------------------------------------------------- 1 | crbegin 2 | crend 3 | extract 4 | merge 5 | -------------------------------------------------------------------------------- /list/todo.txt: -------------------------------------------------------------------------------- 1 | flip 2 | ~list 3 | pop_back 4 | push_back 5 | get_allocator -------------------------------------------------------------------------------- /logo/stl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Phonbopit/30-seconds-of-cpp/master/logo/stl.png -------------------------------------------------------------------------------- /logo/logo_short.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Phonbopit/30-seconds-of-cpp/master/logo/logo_short.png -------------------------------------------------------------------------------- /logo/new_logo_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Phonbopit/30-seconds-of-cpp/master/logo/new_logo_1.jpg -------------------------------------------------------------------------------- /logo/new_logo_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Phonbopit/30-seconds-of-cpp/master/logo/new_logo_2.jpg -------------------------------------------------------------------------------- /map/todo.txt: -------------------------------------------------------------------------------- 1 | insert_or_assign 2 | try_emplace 3 | extract 4 | merge 5 | equal_range 6 | key_comp 7 | value_comp 8 | -------------------------------------------------------------------------------- /unordered_map/todo.txt: -------------------------------------------------------------------------------- 1 | count 2 | clear 3 | insert_or_assign 4 | emplace 5 | emplace_hint 6 | try_emplace 7 | swap 8 | extract 9 | merge 10 | at 11 | find 12 | contains 13 | equal_range 14 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: on-merge 2 | on: [push] 3 | jobs: 4 | my-job: 5 | name: Automate update_readme.py 6 | runs-on: ubuntu-18.04 7 | steps: 8 | - name: Update Readme 9 | run: python update_readme.py 10 | shell: python 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/suggestion-feedback.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Suggestion/Feedback 3 | about: Use this if you want to suggest anything or give feedback 4 | title: Suggestion/Feedback 5 | labels: feedback, suggestion 6 | assignees: '' 7 | 8 | --- 9 | 10 | 11 | -------------------------------------------------------------------------------- /priority_queue/README.md: -------------------------------------------------------------------------------- 1 | # `` 2 | :heavy_check_mark: [priority_queue](priority_queue.md) 3 | :heavy_check_mark: [empty](empty.md) 4 | :heavy_check_mark: [top](top.md) 5 | :heavy_check_mark: [pop](pop.md) 6 | :heavy_check_mark: [push](push.md) 7 | :heavy_check_mark: [size](size.md) 8 | -------------------------------------------------------------------------------- /stack/README.md: -------------------------------------------------------------------------------- 1 | # `` 2 | :heavy_check_mark: [emplace](emplace.md) 3 | :heavy_check_mark: [empty](empty.md) 4 | :heavy_check_mark: [pop](pop.md) 5 | :heavy_check_mark: [push](push.md) 6 | :heavy_check_mark: [size](size.md) 7 | :heavy_check_mark: [swap](swap.md) 8 | :heavy_check_mark: [top](top.md) 9 | -------------------------------------------------------------------------------- /algorithm/min.md: -------------------------------------------------------------------------------- 1 | # min 2 | 3 | **Description** : Compares two values, returning the smallest. 4 | 5 | **Example**: 6 | ```cpp 7 | int a = 7, b = 3; 8 | auto smallest = std::min(a, b); 9 | // prints 3 10 | std::cout << smallest << " "; 11 | ``` 12 | **[See Sample code](../snippets/algorithm/min.cpp)** 13 | **[Run Code](https://rextester.com/RCHUQA23545)** -------------------------------------------------------------------------------- /snippets/list/sample.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : this must be your name ;) 5 | Date : Date format dd/mm/yyyy 6 | Time : Time format hh:mm 7 | Description : description should be small & one liner. 8 | */ 9 | -------------------------------------------------------------------------------- /snippets/map/sample.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : this must be your name ;) 5 | Date : Date format dd/mm/yyyy 6 | Time : Time format hh:mm 7 | Description : description should be small & one liner. 8 | */ 9 | -------------------------------------------------------------------------------- /snippets/queue/sample.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : this must be your name ;) 5 | Date : Date format dd/mm/yyyy 6 | Time : Time format hh:mm 7 | Description : description should be small & one liner. 8 | */ 9 | -------------------------------------------------------------------------------- /snippets/stack/sample.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : this must be your name ;) 5 | Date : Date format dd/mm/yyyy 6 | Time : Time format hh:mm 7 | Description : description should be small & one liner. 8 | */ 9 | -------------------------------------------------------------------------------- /snippets/vector/sample.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : this must be your name ;) 5 | Date : Date format dd/mm/yyyy 6 | Time : Time format hh:mm 7 | Description : description should be small & one liner. 8 | */ 9 | -------------------------------------------------------------------------------- /snippets/algorithm/sample.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : this must be your name ;) 5 | Date : Date format dd/mm/yyyy 6 | Time : Time format hh:mm 7 | Description : description should be small & one liner. 8 | */ 9 | -------------------------------------------------------------------------------- /algorithm/max.md: -------------------------------------------------------------------------------- 1 | # max 2 | 3 | **Description** : Compares two values, returning the greatest. 4 | 5 | **Example**: 6 | ```cpp 7 | int a = 7, b = 3; 8 | auto greatest = std::max(a, b); 9 | // prints 7 10 | std::cout << greatest << " "; 11 | ``` 12 | **[See Sample code](../snippets/algorithm/max.cpp)** 13 | **[Run Code](https://rextester.com/RCHUQA23545)** 14 | -------------------------------------------------------------------------------- /snippets/algorithm/stable_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | //sample array 5 | int ar[] = {1,1,3,5,4,9,4}; 6 | 7 | //sorting the array 8 | std::stable_sort(ar,ar+7); 9 | 10 | //array after sorting 11 | for(int i = 0;i < 7;i++){ 12 | std::cout << ar[i] << " "; 13 | } 14 | //outputs 1 1 3 4 4 5 9 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 2 | This PR... 3 | 4 | 5 | Fixes # 6 | 7 | ## Changes 8 | 9 | - 10 | 11 | ## Checklist 12 | - [ ] I have read CONTRIBUTING guidelines. 13 | - [ ] This is a typo fix. 14 | - [ ] I am not updating any `todo.txt` files. 15 | -------------------------------------------------------------------------------- /snippets/unordered_map/Makefile: -------------------------------------------------------------------------------- 1 | SRC_PATH=. 2 | OUT_PATH=./bin 3 | SRC_FILES:=$(wildcard ${SRC_PATH}/*.cpp) 4 | SRC_FILES:= $(filter-out ${SRC_PATH}/sample.cpp, $(SRC_FILES)) 5 | SRC_BIN=$(SRC_FILES:${SRC_PATH}/%.cpp=${OUT_PATH}/%) 6 | 7 | CXX=g++ 8 | 9 | all: $(SRC_BIN) 10 | 11 | ${OUT_PATH}/%: ${SRC_PATH}/%.cpp 12 | mkdir -p ${OUT_PATH} 13 | ${CXX} -std=c++17 $^ -o $@ 14 | clean: 15 | @rm -rf ${OUT_PATH} 16 | -------------------------------------------------------------------------------- /unordered_map/bucket.md: -------------------------------------------------------------------------------- 1 | # bucket() 2 | 3 | **Description** : 4 | - Returns the bucket number where the element with the key k is located in the map. 5 | 6 | **Example**: 7 | ```cpp 8 | // stores the bucket number of the key k 9 | int number = mymap.bucket(it->first); 10 | 11 | ``` 12 | **[See Sample code](../snippets/unordered_map/bucket.cpp)** 13 | **[Run Code](https://rextester.com/PMCFAI46947)** -------------------------------------------------------------------------------- /vector/pop_back.md: -------------------------------------------------------------------------------- 1 | # pop_back 2 | 3 | **Description** : Remove elements of a vector from the end 4 | 5 | **Example** : 6 | ```cpp 7 | //Declare the vector 8 | std::vector vector1; 9 | 10 | //Function to pop_back values from the vector 11 | vector1.pop_back(); 12 | 13 | ``` 14 | **[See Sample code](../snippets/vector/pop_back.cpp)** 15 | **[Run Code](https://rextester.com/PZCE91278)** 16 | -------------------------------------------------------------------------------- /snippets/algorithm/max.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 23:00 5 | Description : Compares two values, returning the greatest. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() { 12 | int a = 7, b = 3; 13 | auto greatest = std::max(a, b); 14 | // prints 7 15 | std::cout << greatest << " "; 16 | } -------------------------------------------------------------------------------- /snippets/algorithm/min.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 23:00 5 | Description : Compares two values, returning the smallest. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() { 12 | int a = 7, b = 3; 13 | auto smallest = std::min(a, b); 14 | // print 3 15 | std::cout << smallest << " "; 16 | } -------------------------------------------------------------------------------- /snippets/list/Makefile: -------------------------------------------------------------------------------- 1 | SRC_PATH=. 2 | OUT_PATH=./bin 3 | SRC_FILES:=$(wildcard ${SRC_PATH}/*.cpp) 4 | SRC_FILES:= $(filter-out ${SRC_PATH}/sample.cpp, $(SRC_FILES)) 5 | SRC_BIN=$(SRC_FILES:${SRC_PATH}/%.cpp=${OUT_PATH}/%) 6 | 7 | CXX=g++ 8 | 9 | all: $(SRC_BIN) 10 | 11 | ${OUT_PATH}/%: ${SRC_PATH}/%.cpp 12 | mkdir -p ${OUT_PATH} 13 | ${CXX} -std=c++17 $^ -o $@ 14 | clean: 15 | @rm -rf ${OUT_PATH} 16 | 17 | 18 | -------------------------------------------------------------------------------- /snippets/map/Makefile: -------------------------------------------------------------------------------- 1 | SRC_PATH=. 2 | OUT_PATH=./bin 3 | SRC_FILES:=$(wildcard ${SRC_PATH}/*.cpp) 4 | SRC_FILES:= $(filter-out ${SRC_PATH}/sample.cpp, $(SRC_FILES)) 5 | SRC_BIN=$(SRC_FILES:${SRC_PATH}/%.cpp=${OUT_PATH}/%) 6 | 7 | CXX=g++ 8 | 9 | all: $(SRC_BIN) 10 | 11 | ${OUT_PATH}/%: ${SRC_PATH}/%.cpp 12 | mkdir -p ${OUT_PATH} 13 | ${CXX} -std=c++17 $^ -o $@ 14 | clean: 15 | @rm -rf ${OUT_PATH} 16 | 17 | 18 | -------------------------------------------------------------------------------- /snippets/queue/Makefile: -------------------------------------------------------------------------------- 1 | SRC_PATH=. 2 | OUT_PATH=./bin 3 | SRC_FILES:=$(wildcard ${SRC_PATH}/*.cpp) 4 | SRC_FILES:= $(filter-out ${SRC_PATH}/sample.cpp, $(SRC_FILES)) 5 | SRC_BIN=$(SRC_FILES:${SRC_PATH}/%.cpp=${OUT_PATH}/%) 6 | 7 | CXX=g++ 8 | 9 | all: $(SRC_BIN) 10 | 11 | ${OUT_PATH}/%: ${SRC_PATH}/%.cpp 12 | mkdir -p ${OUT_PATH} 13 | ${CXX} -std=c++17 $^ -o $@ 14 | clean: 15 | @rm -rf ${OUT_PATH} 16 | 17 | 18 | -------------------------------------------------------------------------------- /snippets/set/Makefile: -------------------------------------------------------------------------------- 1 | SRC_PATH=. 2 | OUT_PATH=./bin 3 | SRC_FILES:=$(wildcard ${SRC_PATH}/*.cpp) 4 | SRC_FILES:= $(filter-out ${SRC_PATH}/sample.cpp, $(SRC_FILES)) 5 | SRC_BIN=$(SRC_FILES:${SRC_PATH}/%.cpp=${OUT_PATH}/%) 6 | 7 | CXX=g++ 8 | 9 | all: $(SRC_BIN) 10 | 11 | ${OUT_PATH}/%: ${SRC_PATH}/%.cpp 12 | mkdir -p ${OUT_PATH} 13 | ${CXX} -std=c++17 $^ -o $@ 14 | clean: 15 | @rm -rf ${OUT_PATH} 16 | 17 | 18 | -------------------------------------------------------------------------------- /snippets/stack/Makefile: -------------------------------------------------------------------------------- 1 | SRC_PATH=. 2 | OUT_PATH=./bin 3 | SRC_FILES:=$(wildcard ${SRC_PATH}/*.cpp) 4 | SRC_FILES:= $(filter-out ${SRC_PATH}/sample.cpp, $(SRC_FILES)) 5 | SRC_BIN=$(SRC_FILES:${SRC_PATH}/%.cpp=${OUT_PATH}/%) 6 | 7 | CXX=g++ 8 | 9 | all: $(SRC_BIN) 10 | 11 | ${OUT_PATH}/%: ${SRC_PATH}/%.cpp 12 | mkdir -p ${OUT_PATH} 13 | ${CXX} -std=c++17 $^ -o $@ 14 | clean: 15 | @rm -rf ${OUT_PATH} 16 | 17 | 18 | -------------------------------------------------------------------------------- /snippets/vector/Makefile: -------------------------------------------------------------------------------- 1 | SRC_PATH=. 2 | OUT_PATH=./bin 3 | SRC_FILES:=$(wildcard ${SRC_PATH}/*.cpp) 4 | SRC_FILES:= $(filter-out ${SRC_PATH}/sample.cpp, $(SRC_FILES)) 5 | SRC_BIN=$(SRC_FILES:${SRC_PATH}/%.cpp=${OUT_PATH}/%) 6 | 7 | CXX=g++ 8 | 9 | all: $(SRC_BIN) 10 | 11 | ${OUT_PATH}/%: ${SRC_PATH}/%.cpp 12 | mkdir -p ${OUT_PATH} 13 | ${CXX} -std=c++17 $^ -o $@ 14 | clean: 15 | @rm -rf ${OUT_PATH} 16 | 17 | 18 | -------------------------------------------------------------------------------- /snippets/algorithm/Makefile: -------------------------------------------------------------------------------- 1 | SRC_PATH=. 2 | OUT_PATH=./bin 3 | SRC_FILES:=$(wildcard ${SRC_PATH}/*.cpp) 4 | SRC_FILES:= $(filter-out ${SRC_PATH}/sample.cpp, $(SRC_FILES)) 5 | SRC_BIN=$(SRC_FILES:${SRC_PATH}/%.cpp=${OUT_PATH}/%) 6 | 7 | CXX=g++ 8 | 9 | all: $(SRC_BIN) 10 | 11 | ${OUT_PATH}/%: ${SRC_PATH}/%.cpp 12 | mkdir -p ${OUT_PATH} 13 | ${CXX} -std=c++17 $^ -o $@ 14 | clean: 15 | @rm -rf ${OUT_PATH} 16 | 17 | 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /priority_queue/size.md: -------------------------------------------------------------------------------- 1 | # size 2 | 3 | **Description** : simply returns the size of the priority_queue. 4 | 5 | **Example**: 6 | ``` cpp 7 | 8 | std::priority_queue my; 9 | 10 | my.push(2); 11 | my.push(10); 12 | my.push(4); 13 | 14 | // returns the first value of the queue(largest) 15 | std::cout << my.size(); 16 | ``` 17 | 18 | **[Run Code](https://rextester.com/QJOPJ75245)** 19 | 20 | 21 | -------------------------------------------------------------------------------- /queue/README.md: -------------------------------------------------------------------------------- 1 | # `` 2 | :heavy_check_mark: [back](back.md) 3 | :heavy_check_mark: [emplace](emplace.md) 4 | :heavy_check_mark: [empty](empty.md) 5 | :heavy_check_mark: [front](front.md) 6 | :heavy_check_mark: [pop](pop.md) 7 | :heavy_check_mark: [push](push.md) 8 | :heavy_check_mark: [queue](queue.md) 9 | :heavy_check_mark: [size](size.md) 10 | :heavy_check_mark: [swap](swap.md) 11 | :x: ~queue 12 | -------------------------------------------------------------------------------- /vector/begin.md: -------------------------------------------------------------------------------- 1 | # begin 2 | 3 | **Description** : 4 | - Returns an iterator pointing to the first element in the vector. 5 | 6 | **Example**: 7 | ```cpp 8 | // Iterate over myVector using iterators 9 | for (auto it = myVector.begin(); it != myVector.end(); it++) { 10 | std::cout << *it << " "; 11 | } 12 | ``` 13 | **[See Sample code](../snippets/vector/begin.cpp)** 14 | **[Run Code](https://rextester.com/TNG69681)** 15 | -------------------------------------------------------------------------------- /vector/push_back.md: -------------------------------------------------------------------------------- 1 | # push_back 2 | 3 | **Description** : Add new elements to the vector 4 | 5 | **Example**: 6 | ```cpp 7 | //Declare the vector 8 | std::vector vector1; 9 | 10 | //Function to push_back values to the vector 11 | vector1.push_back(1); 12 | vector1.push_back(2); 13 | 14 | ``` 15 | **[See Sample code](../snippets/vector/push_back.cpp)** 16 | **[Run Code](https://rextester.com/JBQCG9959)** -------------------------------------------------------------------------------- /vector/swap.md: -------------------------------------------------------------------------------- 1 | # swap 2 | 3 | **Description** : Swaps the contents of two vectors 4 | 5 | **Example**: 6 | ```cpp 7 | //Intializing the vectors 8 | std::vector vector1 = {1,2,3}; 9 | std::vector vector2 = {4,5,6,6}; 10 | 11 | //Function to swap the vectors 12 | vector1.swap(vector2); 13 | 14 | ``` 15 | **[See Sample code](../snippets/vector/swap.cpp)** 16 | **[Run Code](https://rextester.com/PNR78595)** -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: bhupesh 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | custom: # Replace with a single custom sponsorship URL 9 | -------------------------------------------------------------------------------- /vector/end.md: -------------------------------------------------------------------------------- 1 | # end 2 | 3 | **Description** : 4 | - Returns an iterator referring to the past-the-end (next to last element) element in the vector container. 5 | 6 | **Example**: 7 | ```cpp 8 | // Iterate over myVector using iterators 9 | for (auto it = myVector.begin(); it != myVector.end(); it++) { 10 | std::cout << *it << " "; 11 | } 12 | ``` 13 | **[See Sample code](../snippets/vector/end.cpp)** 14 | **[Run Code](https://rextester.com/TNG69681)** 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | sudo: required 3 | script: 4 | - sudo unlink /usr/bin/gcc && sudo ln -s /usr/bin/gcc-5 /usr/bin/gcc 5 | - sudo unlink /usr/bin/g++ && sudo ln -s /usr/bin/g++-5 /usr/bin/g++ 6 | - gcc --version 7 | - cd snippets/vector && make && cd ../stack && make && cd ../queue && make && cd ../list && make && cd ../algorithm && make 8 | 9 | addons: 10 | apt: 11 | sources: 12 | - ubuntu-toolchain-r-test 13 | packages: 14 | - gcc-5 15 | - g++-5 -------------------------------------------------------------------------------- /list/emplace_back.md: -------------------------------------------------------------------------------- 1 | # emplace_back 2 | 3 | **Description** : emplace_back() function is used to add an element to the end of the list 4 | 5 | **Example**: 6 | ```cpp 7 | std::list mylist; 8 | 9 | mylist.emplace_back(1); 10 | mylist.emplace_back(2); 11 | mylist.emplace_back(3); 12 | 13 | std::cout << "mylist contains: "; 14 | for (auto& x: mylist) 15 | std::cout << x << " "; 16 | 17 | ``` 18 | **[Run Code](https://rextester.com/DKWI57315)** 19 | -------------------------------------------------------------------------------- /map/empty.cpp: -------------------------------------------------------------------------------- 1 | // Demonstrates empty() 2 | #include 3 | #include 4 | 5 | int main() { 6 | // declaration of map container 7 | std::map mymap; 8 | mymap['a'] = 1; 9 | mymap['b'] = 2; 10 | mymap['c'] = 3; 11 | 12 | // using empty() to check map is empty or not 13 | if(mymap.empty()==true) 14 | printf("my map is empty\n"); 15 | else 16 | printf("my map is not empty\n"); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /list/begin.md: -------------------------------------------------------------------------------- 1 | # begin 2 | 3 | **Description** : begin() function is used to return an iterator pointing to the first element of the list container. 4 | 5 | **Example**: 6 | ```cpp 7 | // declaration of list container 8 | std::list mylist{ 1, 2, 3, 4, 5 }; 9 | 10 | // using begin() to print list 11 | for (auto it = mylist.begin(); it != mylist.end(); ++it) 12 | std::cout << ' ' << *it; 13 | 14 | ``` 15 | **[Run Code](https://rextester.com/OLYXB49722)** 16 | -------------------------------------------------------------------------------- /list/clear.md: -------------------------------------------------------------------------------- 1 | # clear 2 | 3 | **Description** : clear() function is used to remove all the elements of the list container, thus making it size 0. 4 | 5 | **Example**: 6 | ```cpp 7 | 8 | std::list mylist{ 1, 2, 3, 4, 5 }; 9 | 10 | mylist.clear(); 11 | // List becomes empty 12 | 13 | // Printing the list 14 | for (auto value : mylist) { 15 | std::cout << ' ' << value; 16 | } 17 | 18 | ``` 19 | **[Run Code](https://rextester.com/IKAFZ38505)** 20 | -------------------------------------------------------------------------------- /set/insert.md: -------------------------------------------------------------------------------- 1 | # insert 2 | 3 | **Description :** 4 | This method is used to insert elements in std::set. 5 | 6 | **Example :** 7 | ```cpp 8 | //Run Code To Demonstrate use of set.size() 9 | #include 10 | #include 11 | 12 | int main(){ 13 | // Create a set object holding integers 14 | std::set mySet {1,2,3,4,-5}; 15 | 16 | // insert element in set 17 | mySet.insert(7); 18 | 19 | return 0; 20 | } 21 | 22 | ``` 23 | 24 | **[Run Code](https://ideone.com/gG9ZgB)** -------------------------------------------------------------------------------- /list/rbegin.md: -------------------------------------------------------------------------------- 1 | # rbegin 2 | 3 | **Description :** list::rbegin() is an inbuilt function in C++ STL that returns a reverse iterator which points to the last element of the list. 4 | 5 | **Example** : 6 | ```cpp 7 | 8 | std::list lis = { 105, 207, 309, 401, 503 }; 9 | 10 | std::cout << "The list in reverse order: "; 11 | 12 | for (auto it = lis.rbegin(); it != lis.rend(); ++it) 13 | std::cout << *it << " "; 14 | 15 | ``` 16 | **[Run Code](https://rextester.com/FJQN20855)** -------------------------------------------------------------------------------- /set/end.md: -------------------------------------------------------------------------------- 1 | # end 2 | 3 | **Description :** 4 | This method returns an iterator pointing to the last element in the set. 5 | 6 | **Example :** 7 | ```cpp 8 | //Run Code To Demonstrate use of set.end() 9 | #include 10 | #include 11 | 12 | int main(){ 13 | // Create a set object holding integers 14 | std::set mySet {1,2,3,4,-5}; 15 | 16 | std::cout << *(mySet.end()) < 10 | #include 11 | 12 | int main(){ 13 | // Create a set object holding integers 14 | std::set mySet {1,2,3,4,-5}; 15 | 16 | std::cout << mySet.empty() <` 2 | :heavy_check_mark: [count](count.md) 3 | :heavy_check_mark: [erase](erase.md) 4 | :heavy_check_mark: [find](find.md) 5 | :heavy_check_mark: [insert](insert.md) 6 | :heavy_check_mark: [size](size.md) 7 | :x: at 8 | :x: clear 9 | :x: contains 10 | :x: count 11 | :x: emplace 12 | :x: emplace_hint 13 | :x: equal_range 14 | :x: extract 15 | :x: find 16 | :x: insert_or_assign 17 | :x: merge 18 | :x: swap 19 | :x: try_emplace 20 | -------------------------------------------------------------------------------- /vector/emplace_back.md: -------------------------------------------------------------------------------- 1 | # emplace_back 2 | 3 | **Description**: Constructs and places an element at the end of the vector 4 | 5 | **Example**: 6 | ```cpp 7 | //creating a vector of 3 elements 8 | std::vector vec{10, 20, 30}; 9 | 10 | //function to construct and insert a element at the end of the vector 11 | vec.emplace_back(40); 12 | vec.emplace_back(50); 13 | ``` 14 | 15 | **[See Sample Code](../snippets/vector/emplace_back.cpp)** 16 | **[Run Code](https://rextester.com/PIGRO40314)** -------------------------------------------------------------------------------- /list/rend.md: -------------------------------------------------------------------------------- 1 | # rend 2 | 3 | **Description :** list::rend() is an inbuilt function in C++ STL that returns a reverse iterator which points to the position before the beginning of the list. 4 | 5 | **Example** : 6 | ```cpp 7 | 8 | std::list lis = { 109, 206, 303, 401, 506 }; 9 | 10 | std::cout << "The list in reverse order: "; 11 | 12 | for (auto it = lis.rbegin(); it != lis.rend(); ++it) 13 | std::cout << *it << " "; 14 | 15 | ``` 16 | **[Run Code](https://rextester.com/OWUY16070)** -------------------------------------------------------------------------------- /queue/empty.md: -------------------------------------------------------------------------------- 1 | # empty 2 | 3 | **Description** : empty() function is used to check if the queue container is empty or not. This function returns true, if the queue is empty or false, otherwise. 4 | 5 | **Example**: 6 | ```cpp 7 | // Empty queue 8 | std::queue myqueue; 9 | 10 | if (myqueue.empty()){ 11 | std::cout << "My queue is empty"; 12 | } 13 | else{ 14 | std::cout << "My queue is not empty"; 15 | } 16 | ``` 17 | **[Run Code](https://rextester.com/LONM40957)** 18 | -------------------------------------------------------------------------------- /set/begin.md: -------------------------------------------------------------------------------- 1 | # begin 2 | 3 | **Description :** 4 | This method returns an iterator pointing to the first element in the set. 5 | 6 | **Example :** 7 | ```cpp 8 | //Run Code To Demonstrate use of set.begin() 9 | #include 10 | #include 11 | 12 | int main(){ 13 | // Create a set object holding integers 14 | std::set mySet {1,2,3,4,-5}; 15 | 16 | std::cout << *(mySet.begin()) < vec = {3, -1, 2, 10, 4}; 8 | 9 | auto greatestIt = std::max_element(vec.begin(), vec.end()); 10 | // prints 10 11 | std::cout << "The greatest element is " << *greatestIt << '\n'; 12 | 13 | ``` 14 | **[See Sample code](../snippets/algorithm/max_element.cpp)** 15 | **[Run Code](https://rextester.com/MYCU8700)** -------------------------------------------------------------------------------- /algorithm/min_element.md: -------------------------------------------------------------------------------- 1 | # min_element 2 | 3 | **Description** : Returns an iterator to the smallest element in a range defined by `[first, last)`. 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector vec = {3, -1, 2, 10, 4}; 8 | 9 | auto smallestIt = std::min_element(vec.begin(), vec.end()); 10 | // prints -1 11 | std::cout << "The smallest element is " << *smallestIt << '\n'; 12 | 13 | ``` 14 | **[See Sample code](../snippets/algorithm/min_element.cpp)** 15 | **[Run Code](https://rextester.com/ZVBA46979)** -------------------------------------------------------------------------------- /algorithm/swap.md: -------------------------------------------------------------------------------- 1 | # swap 2 | 3 | **Description** : 4 | - Exchange values of two objects 5 | 6 | **Example** 7 | ```cpp 8 | //Declare two example objects (same type) 9 | int a = 60, b = 50; 10 | 11 | //a:60 and b:50 12 | std::cout<<"Before: "<<"a = "< 9 | #include 10 | 11 | using namespace std; 12 | 13 | int main(){ 14 | // Creating a list 15 | std::list demoList; 16 | 17 | // checking the max size of the list 18 | std::cout << demoList.max_size(); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /vector/emplace.md: -------------------------------------------------------------------------------- 1 | # emplace 2 | 3 | **Description**: Places an element in the vector at the specified position 4 | 5 | **Example**: 6 | ```cpp 7 | //creating a vector of 5 elements 8 | std::vector vector1{10, 20, 30, 40, 50}; 9 | 10 | //function to add an element at the beginning and the end 11 | vector1.emplace(vector1.begin(), -10); 12 | vector1.emplace(vector1.end(), 60); 13 | ``` 14 | 15 | **[See Sample Code](../snippets/vector/emplace.cpp)** 16 | **[Run Code](https://rextester.com/CDL77568)** 17 | -------------------------------------------------------------------------------- /algorithm/reverse.md: -------------------------------------------------------------------------------- 1 | # reverse 2 | 3 | **Description :** It reverses the order of the elements in the range [first, last) of any container. 4 | 5 | **Example** : 6 | ```cpp 7 | int main() { 8 | int i; 9 | std::vector v1{1,2,3,4}; 10 | 11 | std::reverse(v1.begin(),v1.end()); 12 | 13 | for (auto value : v1) { 14 | std::cout << value << " "; 15 | } 16 | return 0; 17 | } 18 | ``` 19 | **[Run Code](https://rextester.com/NXC57566)** 20 | -------------------------------------------------------------------------------- /queue/size.md: -------------------------------------------------------------------------------- 1 | # size 2 | 3 | **Description** : size() function is used to return the size of the queue or the number of elements in the queue. 4 | 5 | **Example**: 6 | ```cpp 7 | std::queue myqueue; 8 | myqueue.push(4); 9 | myqueue.push(5); 10 | myqueue.push(9); 11 | myqueue.push(1); 12 | myqueue.push(3); 13 | 14 | // Queue becomes 4, 5, 9, 1, 3 15 | 16 | // Print the size of myqueue 17 | std::cout << myqueue.size(); 18 | ``` 19 | **[Run Code](https://rextester.com/ANYAP12901)** 20 | -------------------------------------------------------------------------------- /snippets/algorithm/max_element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 23:00 5 | Description : Retrieve the greatest value from a range. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() { 13 | std::vector vec = {3, -1, 2, 10, 4}; 14 | 15 | auto greatestIt = std::max_element(vec.begin(), vec.end()); 16 | // prints 10 17 | std::cout << "The greatest element is " << *greatestIt << '\n'; 18 | } -------------------------------------------------------------------------------- /snippets/algorithm/min_element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 23:00 5 | Description : Retrieve the smallest value from a range. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() { 13 | std::vector vec = {3, -1, 2, 10, 4}; 14 | 15 | auto smallestIt = std::min_element(vec.begin(), vec.end()); 16 | // prints -1 17 | std::cout << "The smallest element is " << *smallestIt << '\n'; 18 | } -------------------------------------------------------------------------------- /snippets/algorithm/swap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : duongoku 3 | Date : 10/1/2019 4 | Time : 21:25 5 | Description : Exchange values of two objects 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main(){ 12 | //Declare two example objects (same type) 13 | int a = 60, b = 50; 14 | 15 | //a:60 and b:50 16 | std::cout << "Before : a = " << a << " and b = " << b << "\n"; 17 | 18 | std::swap(a,b); 19 | 20 | //a:50 and b:60, after swap 21 | std::cout << "After : a = " << a << " and b = " << b; 22 | } -------------------------------------------------------------------------------- /algorithm/find.md: -------------------------------------------------------------------------------- 1 | # find 2 | 3 | **Description** : Returns the first element in the range [first, last) that satisfies specific criteria(searches for an element equal to *value*). 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 }; 8 | 9 | int searchme = 4; 10 | if(std::find(std::begin(v), std::end(v), searchme) != std::end(v)){ 11 | std::cout <<"\n v contains 4"; 12 | } 13 | else 14 | std::cout<<"No match !!"; 15 | ``` 16 | **[Run Code](https://rextester.com/FIVI88863)** -------------------------------------------------------------------------------- /list/sort.md: -------------------------------------------------------------------------------- 1 | # sort 2 | 3 | **Description :** sort() function is used to sort the elements of the container by changing their positions. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | // list declaration of integer type 9 | std::list mylist{ 1, 5, 3, 2, 4 }; 10 | 11 | // sort function 12 | mylist.sort(); 13 | 14 | // printing the list after sort 15 | for (auto value : mylist) { 16 | std::cout << value << " "; 17 | } 18 | 19 | ``` 20 | **[Run Code](https://rextester.com/BNVRW86064)** 21 | 22 | -------------------------------------------------------------------------------- /stack/size.md: -------------------------------------------------------------------------------- 1 | # size 2 | 3 | **Description** : size() function is used to return the size of the satck or the number of elements in the stack. 4 | 5 | **Example**: 6 | ```cpp 7 | // Empty stack 8 | std::stack mystack; 9 | mystack.push(4); 10 | mystack.push(5); 11 | mystack.push(9); 12 | mystack.push(1); 13 | mystack.push(3); 14 | 15 | // Stack from top to bottom becomes 3, 1, 9, 5, 4 16 | 17 | // Print the size of mystack (5) 18 | std::cout << mystack.size(); 19 | ``` 20 | **[Run Code](https://rextester.com/OEZV66856)** 21 | -------------------------------------------------------------------------------- /snippets/list/reverse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Shameer Ahmad 3 | Date : 25/10/2019 4 | Time : 05:26 5 | Description : This function is used to reverse the content of a list. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | 12 | int main(){ 13 | // initializing a list 14 | std::list mylist{1, 2, 3, 4}; 15 | 16 | //calling the reverse function 17 | mylist.reverse(); 18 | // printing values of the reversed list 19 | for (auto value : mylist){ 20 | std::cout << ' ' << value; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /list/assign.md: -------------------------------------------------------------------------------- 1 | # assign 2 | 3 | **Description** :The list::assign() is a built-in function in C++ STL which is used to assign values to a list. 4 | 5 | **Example**: 6 | ```cpp 7 | // Initialization of list 8 | std::list demo_list; 9 | 10 | // Assigning the value 100, 5 times 11 | // to the list, list_demo. 12 | demo_list.assign(5, 100); 13 | 14 | // Displaying the list 15 | for (int itr : demo_list) { 16 | std::cout << itr << " "; 17 | } 18 | 19 | ``` 20 | **[Run Code](https://rextester.com/UMFNZM11115)** 21 | -------------------------------------------------------------------------------- /list/cend.md: -------------------------------------------------------------------------------- 1 | # cend 2 | 3 | **Description** : The list::cend() is a built-in function in C++ STL which returns a constant random access iterator which points to the end of the list. 4 | 5 | **Example**: 6 | ```cpp 7 | // declaration of list 8 | std::list lis = { 100, 200, 300, 400, 500 }; 9 | 10 | // printing list elements 11 | std::cout << "List: " << std::endl; 12 | 13 | for (auto it = lis.cbegin(); it != lis.cend(); ++it) 14 | std::cout << *it << " "; 15 | 16 | ``` 17 | **[Run Code](https://rextester.com/QTRH25197)** 18 | -------------------------------------------------------------------------------- /snippets/vector/at.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Ankush Kamboj 3 | Date : 11/07/2019 4 | Time : 20:00 5 | Description : Returns a reference to the element at position _n_ in the vector. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() { 12 | // create a vector of 5 integers 13 | std::vector myVector{1, 2, 3, 4, 5}; 14 | 15 | // display the vector contents using std::vector::at 16 | for (int i = 0 ; i < 5; i++) { 17 | std::cout << myVector.at(i) << " "; 18 | } 19 | std::cout << std::endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /list/back.md: -------------------------------------------------------------------------------- 1 | # back 2 | 3 | **Description** : The list::back() function in C++ STL returns a direct reference to the last element in the list container. 4 | 5 | **Example**: 6 | ```cpp 7 | // Initialization of list 8 | std::list demo_list; 9 | 10 | // Adding elements to the list 11 | demo_list.push_back(10); 12 | demo_list.push_back(20); 13 | demo_list.push_back(30); 14 | 15 | // prints the last element of demo_list 16 | std::cout << demo_list.back(); 17 | 18 | ``` 19 | **[Run Code](https://rextester.com/TMRQJ2841)** 20 | -------------------------------------------------------------------------------- /set/clear.md: -------------------------------------------------------------------------------- 1 | # clear 2 | 3 | **Description :** 4 | This method is used to remove all the elements from the set and make its size zero 5 | 6 | **Example :** 7 | ```cpp 8 | //Run Code To Demonstrate use of set.clear() 9 | #include 10 | #include 11 | 12 | int main(){ 13 | // Create a set object holding integers 14 | std::set myset {1,2,3,4,5}; 15 | myset.clear(); 16 | std::cout << "Size of my set is : " << myset.size() << std::endl; 17 | 18 | return 0; 19 | } 20 | 21 | ``` 22 | **[Run Code](https://ideone.com/14Ccn)** 23 | 24 | -------------------------------------------------------------------------------- /stack/empty.md: -------------------------------------------------------------------------------- 1 | # empty 2 | 3 | **Description** : empty() function is used to check if the stack container is empty or not. 4 | 5 | **Example**: 6 | ```cpp 7 | // Empty stack 8 | std::stack mystack; 9 | //pushing elements using push() 10 | mystack.push(0); 11 | mystack.push(1); 12 | mystack.push(2); 13 | 14 | while (!mystack.empty()) { 15 | //deleting elements using pop() 16 | std::cout << ' ' << mystack.top(); 17 | mystack.pop(); 18 | } 19 | ``` 20 | **[Run Code](https://rextester.com/PQL30958)** 21 | -------------------------------------------------------------------------------- /snippets/algorithm/partial_sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : David Bezhanishvili 3 | Date : 14/06/2020 4 | Time : 18:25 5 | Description : move n smallest elements to the beginning of an array 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int main() 12 | { 13 | int n=3; //move 3 smallest elements to the beginning 14 | std::vector v = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; 15 | 16 | std::partial_sort(v.begin(), v.begin() + n, v.end()); 17 | for (int a : v) { 18 | std::cout << a << " "; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /snippets/map/begin_and_end.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Manav Sethi 3 | Date : 26/06/2019 4 | Time : 21:52 5 | Description : demonstration of begin and end function in map 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main () 12 | { 13 | std::map mymap; 14 | 15 | mymap['b'] = 100; 16 | mymap['a'] = 200; 17 | mymap['c'] = 300; 18 | 19 | // show content: 20 | for (std::map::iterator it=mymap.begin(); it!=mymap.end(); ++it) 21 | std::cout << it->first << " => " << it->second << '\n'; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /stack/top.md: -------------------------------------------------------------------------------- 1 | # top 2 | 3 | **Description** : top() function is used to reference the top(or the newest) element of the stack. 4 | 5 | **Example**: 6 | ```cpp 7 | // Empty stack 8 | std::stack mystack; 9 | //pushing elements using push() 10 | mystack.push(0); 11 | mystack.push(1); 12 | mystack.push(2); 13 | 14 | while (!mystack.empty()) { 15 | //deleting elements using pop() 16 | std::cout << ' ' << mystack.top(); 17 | mystack.pop(); 18 | } 19 | ``` 20 | **[Run Code](https://rextester.com/OMMI96105)** 21 | -------------------------------------------------------------------------------- /algorithm/count_if.md: -------------------------------------------------------------------------------- 1 | # count_if 2 | 3 | **Description** : Returns the number of elements in the range `[first, last)` satisfying specific criteria(counts the elements that are equal to *value*). 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 }; 8 | 9 | // use a lambda expression to count elements divisible by 3. 10 | int num_items3 = std::count_if(v.begin(), v.end(), [](int i){return i % 3 == 0;}); 11 | std::cout << "number divisible by three: " << num_items3 << '\n'; 12 | ``` 13 | **[Run Code](https://rextester.com/XYVYW31080)** -------------------------------------------------------------------------------- /algorithm/stable_sort.md: -------------------------------------------------------------------------------- 1 | # stable_sort 2 | 3 | **Description :** This function sorts the array.stable_sort sorts the elements maintaining their order as in the original array unline sort. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | //sample array 9 | int ar[] = {1,1,3,5,4,9,4}; 10 | //sorting the array 11 | std::stable_sort(ar,ar+7); 12 | //array after sorting 13 | for(int i = 0;i < 7;i++){ 14 | std::cout << ar[i] << " "; 15 | } 16 | //outputs 1 1 3 4 4 5 9 17 | return 0; 18 | ``` 19 | **[Run Code](https://rextester.com/QOJM63982)** 20 | -------------------------------------------------------------------------------- /snippets/vector/begin.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Ankush Kamboj 3 | Date : 11/07/2019 4 | Time : 20:00 5 | Description : Returns an iterator pointing to the first element in the vector. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() { 12 | // create a vector of 5 integers 13 | std::vector myVector{1, 2, 3, 4, 5}; 14 | 15 | // display the vector contents using iterators 16 | for (auto it = myVector.begin(); it != myVector.end(); it++) { 17 | std::cout << *it << " "; 18 | } 19 | std::cout << std::endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /vector/rbegin.md: -------------------------------------------------------------------------------- 1 | # rbegin 2 | 3 | **Description** : 4 | - Returns a reverse iterator pointing to the last element in the vector (i.e., its reverse beginning). 5 | - Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container. 6 | 7 | **Example**: 8 | ```cpp 9 | // Iterate backwards over myVector using reverse iterators 10 | for (auto it = myVector.rbegin(); it != myVector.rend(); it++) { 11 | std::cout << *it << " "; 12 | } 13 | ``` 14 | **[See Sample code](../snippets/vector/rbegin.cpp)** 15 | **[Run Code](https://rextester.com/XGWC54800)** -------------------------------------------------------------------------------- /algorithm/count.md: -------------------------------------------------------------------------------- 1 | # count 2 | 3 | **Description :** : Returns the number of elements in the range `[first, last)` satisfying specific criteria(counts the elements that are equal to *value*). 4 | 5 | **Example** : 6 | ```cpp 7 | std::vector v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 }; 8 | 9 | // determine how many integers in a std::vector match a target value. 10 | int target1 = 3; 11 | int num_items1 = std::count(v.begin(), v.end(), target1); 12 | std::cout << "number: " << target1 << " count: " << num_items1 << '\n'; 13 | ``` 14 | **[Run Code](https://rextester.com/PSP35316)** -------------------------------------------------------------------------------- /list/merge.md: -------------------------------------------------------------------------------- 1 | # merge 2 | 3 | **Description :** The list::merge() is an inbuilt function in C++ STL which merges two sorted lists into one. 4 | 5 | **Example** : 6 | ```cpp 7 | // declaring the lists 8 | // initially sorted 9 | std::list list1 = { 10, 20, 30 }; 10 | std::list list2 = { 40, 50, 60 }; 11 | 12 | // merge operation 13 | list2.merge(list1); 14 | 15 | std::cout << "List: "; 16 | for (auto value : list2) { 17 | std::cout << value << " "; 18 | } 19 | 20 | ``` 21 | **[Run Code](https://rextester.com/VXG26617)** 22 | -------------------------------------------------------------------------------- /vector/sort.md: -------------------------------------------------------------------------------- 1 | # sort 2 | 3 | **Description** : Puts the vector in increasing or decreasing order 4 | **Example**: 5 | ```cpp 6 | //Declare the vector 7 | std::vector v {-1,0,4,-5,12,2,10,-11,3,5}; 8 | 9 | //Function to sort values in increasing order in the vector 10 | 11 | std::sort(v.begin(), v.end()); 12 | 13 | //Function to sort values in decreasing order in the vector 14 | 15 | std::sort(v.begin(), v.end(), std::greater()); 16 | 17 | ``` 18 | **[See Sample code](../snippets/vector/sort.cpp)** 19 | **[Run Code](https://rextester.com/FFTEBY25900)** -------------------------------------------------------------------------------- /snippets/algorithm/make_heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(){ 6 | //sample vector 7 | std::vector v1 = {2,5,8,7,4,5}; 8 | 9 | //printing first element 10 | std::cout << "First element before making heap : " << v1.front() << std::endl; 11 | //outputs 2 12 | 13 | //transforming vector into heap with make_heap() 14 | make_heap(v1.begin(),v1.end()); 15 | //printing first element 16 | std::cout << "First element in heap : " << v1.front(); 17 | //outputs 8 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /snippets/vector/front.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Jayshree Aishwarya 3 | Date : 18/10/2019 4 | Time : 00:04 5 | Description : Used to fetch the first element of a vector 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() { 12 | // A vector storing rainfall(in inches) of a week 13 | std::vector vRainfall{1.1, 2.02, 3.5, 4.9, 5.0, 3.7, 0.9}; 14 | 15 | // displaying the first value by front() 16 | std::cout << "The Rainfall on day 1 -" << std::endl; 17 | std::cout << vRainfall.front(); 18 | std::cout << std::endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /set/cbegin.md: -------------------------------------------------------------------------------- 1 | # cbegin 2 | 3 | **Description :** 4 | This method is used to return a const_iterator pointing to the first element in the container. 5 | 6 | **Example :** 7 | ```cpp 8 | //Run Code To Demonstrate use of set.cbegin() 9 | #include 10 | #include 11 | 12 | int main () 13 | { 14 | std::set myset = {50,20,60,10,25}; 15 | 16 | std::cout << "myset contains:"; 17 | for (auto it=myset.cbegin(); it != myset.cend(); ++it) 18 | std::cout << ' ' << *it; 19 | 20 | std::cout << '\n'; 21 | 22 | return 0; 23 | } 24 | 25 | ``` 26 | **[Run Code](https://rextester.com/YVMS29814)** -------------------------------------------------------------------------------- /snippets/vector/back.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Jayshree Aishwarya 3 | Date : 18/10/2019 4 | Time : 00:19 5 | Description : Used to fetch the last element of a vector 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() { 12 | // A vector storing rainfall(in inches) of a week 13 | std::vector vRainfall{1.1, 2.02, 3.5, 4.9, 5.0, 3.7, 0.9}; 14 | 15 | // displaying the last value of a vector by back() 16 | std::cout << "The Rainfall on day 7 -" << std::endl; 17 | std::cout << vRainfall.back(); 18 | std::cout << std::endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /vector/at.md: -------------------------------------------------------------------------------- 1 | # at 2 | 3 | **Description** : 4 | - Returns a reference to the element at position _n_ in the vector. 5 | 6 | - If the position is not present in the vector, it throws exception of type _out_of_range_ 7 | 8 | **Example**: 9 | ```cpp 10 | // Create a vector of 5 integers 11 | std::vector myVector{1, 2, 3, 4, 5}; 12 | 13 | // Display the contents of vector using std::vector::at. 14 | for (int i = 0; i < 5; i++) { 15 | std::cout << myVector.at(i) << " "; 16 | } 17 | ``` 18 | **[See Sample code](../snippets/vector/at.cpp)** 19 | **[Run Code](https://rextester.com/ZGMP1944)** -------------------------------------------------------------------------------- /list/crend.md: -------------------------------------------------------------------------------- 1 | # crend 2 | 3 | **Description :** The list::crend() is a built-in function in C++ STL that returns a constant reverse iterator which points to the theoretical element preceding the first element in the list i.e. the reverse end of the list. 4 | 5 | **Example** : 6 | ```cpp 7 | // declaration of the list 8 | std::list lis = { 27, 46, 65, 84, 30, 22 }; 9 | 10 | std::cout << "List: " << std::endl; 11 | 12 | for (auto it = lis.crbegin(); it != lis.crend(); ++it) 13 | std::cout << *it << " "; 14 | 15 | ``` 16 | **[Run Code](https://rextester.com/MUBAJ40037)** 17 | -------------------------------------------------------------------------------- /snippets/vector/empty.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Drashti Modasara 3 | Date : 12/10/2019 4 | Time : 15:17 5 | Description : Returns 1 if the vector is empty and 0 otherwise. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() { 12 | std::vector v1; 13 | v1.push_back(2); 14 | v1.push_back(4); 15 | // display the vector contents using std::vector::empty 16 | std::cout<< "When vector is not empty : v1.empty() = "<< v1.empty()< 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | stack demostack; //Initializing an empty stack. 14 | demostack.push(2); 15 | demostack.push(3); 16 | demostack.push(4); 17 | //We've added these three numbers to the stack. 18 | // Printing stack content. 19 | while (!demostack.empty()) { 20 | cout << demostack.top() << endl; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /snippets/vector/end.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Ankush Kamboj 3 | Date : 11/07/2019 4 | Time : 20:00 5 | Description : Returns an iterator referring to the past-the-end (next to last element) element in the vector container. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() { 12 | // create a vector of 5 integers 13 | std::vector myVector{1, 2, 3, 4, 5}; 14 | 15 | // display the vector contents using iterator 16 | for (auto it = myVector.begin(); it != myVector.end(); it++) { 17 | std::cout << *it << " "; 18 | } 19 | std::cout << std::endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /algorithm/for_each.md: -------------------------------------------------------------------------------- 1 | # for_each 2 | 3 | **Description :** Applies the given Function Object f to each element of the container in range [first, last) 4 | 5 | **Example :** 6 | ```cpp 7 | class Display { 8 | public: 9 | void operator() (int i){ 10 | std::cout << i << " "; 11 | } 12 | }; 13 | 14 | std::vector a{3,2,3,9}; 15 | // Applying the function object Display to each element of a 16 | std::for_each(a.begin(), a.end(), Display()); // Will output, 3 2 3 9 17 | 18 | ``` 19 | **[Run Code](https://rextester.com/BNHGL9456)** 20 | -------------------------------------------------------------------------------- /algorithm/for_each_n.md: -------------------------------------------------------------------------------- 1 | # for_each_n 2 | 3 | **Description :** Applies the given Function Object f to each element of the container in range [first, last) 4 | 5 | **Example :** 6 | ```cpp 7 | class Display { 8 | public: 9 | void operator() (int i){ 10 | std::cout << i << " "; 11 | } 12 | }; 13 | 14 | std::vector a{3,2,3,9}; 15 | // Applying the function object Display to each element of a 16 | std::for_each_n(a.begin(), 2, Display()); // Will output, "3 2 " 17 | 18 | ``` 19 | **[Run Code](https://rextester.com/CTXL35674)** 20 | -------------------------------------------------------------------------------- /snippets/vector/rbegin.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Rok Krhlikar 3 | Date : 07/10/2019 4 | Time : 19:30 5 | Description : Returns a reverse iterator pointing to the last element in the vector (i.e., its reverse beginning). 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() { 12 | // create a vector of 5 integers 13 | std::vector myVector{1, 2, 3, 4, 5}; 14 | 15 | // display the vector contents using reverse iterators 16 | for (auto it = myVector.rbegin(); it != myVector.rend(); it++) { 17 | std::cout << *it << " "; 18 | } 19 | std::cout << std::endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /set/size.md: -------------------------------------------------------------------------------- 1 | # size 2 | 3 | **Description :** 4 | This method is used to return the size of set. The return type is size_t (quite similar to unsigned integer), which overflows when a bigger number is subtracted from it. 5 | 6 | **Example :** 7 | ```cpp 8 | //Run Code To Demonstrate use of set.size() 9 | #include 10 | #include 11 | 12 | int main(){ 13 | // Create a set object holding integers 14 | std::set mySet {1,2,3,4,-5}; 15 | 16 | std::cout << "Size of my set is : " << mySet.size() << std::endl; 17 | 18 | return 0; 19 | } 20 | 21 | ``` 22 | 23 | **[Run Code](https://ideone.com/guEojH)** -------------------------------------------------------------------------------- /vector/rend.md: -------------------------------------------------------------------------------- 1 | # rend 2 | 3 | **Description** : 4 | - Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (which is considered its reverse end). 5 | - Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container. 6 | 7 | **Example**: 8 | ```cpp 9 | // Iterate backwards over myVector using reverse iterators 10 | for (auto it = myVector.rbegin(); it != myVector.rend(); it++) { 11 | std::cout << *it << " "; 12 | } 13 | ``` 14 | **[See Sample code](../snippets/vector/rend.cpp)** 15 | **[Run Code](https://rextester.com/XGWC54800)** 16 | -------------------------------------------------------------------------------- /algorithm/all_of.md: -------------------------------------------------------------------------------- 1 | # all_of 2 | 3 | **Description :** This function operates on whole range of array elements and checks for a given property on every element and returns true when each element in range satisfies specified property, else returns false. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | int main() { 9 | 10 | std::vector v{10, 2, 4, 6}; 11 | 12 | if (std::all_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; })) { 13 | std::cout << "All numbers are even\n"; 14 | } 15 | else{ 16 | std::cout << "All numbers are not even\n"; 17 | } 18 | } 19 | ``` 20 | **[Run Code](https://rextester.com/NBPE75660)** 21 | -------------------------------------------------------------------------------- /algorithm/any_of.md: -------------------------------------------------------------------------------- 1 | # any_of 2 | 3 | **Description :** This function operates on whole range of array elements and checks for a given property on every element and returns true when atleast one element in range satisfies specified property, else returns false. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | int main() { 9 | 10 | std::vector v{1, 3, 5, 7, 2}; 11 | 12 | if (std::any_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; })) { 13 | std::cout << "A number is even\n"; 14 | } 15 | else{ 16 | std::cout << "No number is even\n"; 17 | } 18 | } 19 | ``` 20 | **[Run Code](https://rextester.com/WDB57875)** 21 | -------------------------------------------------------------------------------- /priority_queue/top.md: -------------------------------------------------------------------------------- 1 | # top 2 | 3 | **Description** : top() function is used to reference the first element (ie. the largest element) of the queue. 4 | Note that the next element may not be the second largest so inorder to get the second largest remove the top element and then find the new top element. 5 | 6 | **Example**: 7 | ``` cpp 8 | 9 | std::priority_queue my; 10 | 11 | my.push(2); 12 | my.push(10); 13 | my.push(4); 14 | 15 | // returns the first value of the queue(largest) i.e 10 16 | std::cout << my.top(); 17 | 18 | 19 | ``` 20 | 21 | **[Run Code](https://rextester.com/VUXYQ63057)** 22 | 23 | 24 | -------------------------------------------------------------------------------- /vector/reserve.md: -------------------------------------------------------------------------------- 1 | # reserve 2 | 3 | **Description** : 4 | - Helps specify the minimum size of a vector 5 | - Useful to know how many elements the vector will ultimately hold 6 | 7 | **Example**: 8 | ```cpp 9 | //create an empty vector 10 | std::vector myvector; 11 | 12 | //reserve a size of 'atleast' 5 elements 13 | myvector.reserve(5); 14 | 15 | //inserting 5 elements 16 | for(int i=0; i<5; i++) 17 | { 18 | myvector.push_back(i); 19 | } 20 | ``` 21 | 22 | **[See sample code](..snippets/vector/reserve.cpp)** 23 | **[Run Code](https://rextester.com/AMLWGW9232)** 24 | 25 | -------------------------------------------------------------------------------- /algorithm/todo.txt: -------------------------------------------------------------------------------- 1 | find_end 2 | move_backward 3 | generate_n 4 | swap_ranges 5 | rotate_copy 6 | unique_copy 7 | is_partitioned 8 | partition_copy 9 | stable_partition 10 | partition_point 11 | is_sorted_until 12 | partial_sort 13 | partial_sort_copy 14 | inplace_merge 15 | set_symmetric_difference 16 | is_heap_until 17 | sort_heap 18 | minimax 19 | sample 20 | destroy 21 | uninitialized_copy 22 | uninitialized_fill 23 | uninitialized_move 24 | uninitialized_default_construct 25 | uninitialized_value_construct 26 | partial_sum 27 | inclusive_scan 28 | exclusive_scan 29 | reduce 30 | transform_inclusive_scan 31 | transform_exclusive_scan 32 | inner_product 33 | -------------------------------------------------------------------------------- /queue/back.md: -------------------------------------------------------------------------------- 1 | # back 2 | 3 | **Description** : back() function is used to reference the last element (i.e the newest element) of the queue. This function can be used to fetch the last element of a queue. 4 | 5 | **Example**: 6 | ```cpp 7 | // Empty queue 8 | std::queue myqueue; 9 | 10 | // Add elements to queue using push() 11 | myqueue.push(2); 12 | myqueue.push(5); 13 | myqueue.push(4); 14 | myqueue.push(1); 15 | 16 | // Queue becomes 2, 5, 4, 1 17 | 18 | // Print the last/newest element in the queue 19 | std::cout << myqueue.back(); 20 | ``` 21 | **[Run Code](https://rextester.com/MJAK44967)** 22 | -------------------------------------------------------------------------------- /queue/push.md: -------------------------------------------------------------------------------- 1 | # push 2 | 3 | **Description** : push() function is used to insert an element at the back of the queue. The element is added to the queue container and the size of the queue is increased by 1. 4 | 5 | **Example**: 6 | ```cpp 7 | // Empty queue 8 | std::queue myqueue; 9 | 10 | // pushing elements into queue using push() 11 | myqueue.push(0); 12 | myqueue.push(1); 13 | myqueue.push(2); 14 | 15 | // print contents of queue 16 | while (!myqueue.empty()) { 17 | std::cout << ' ' << myqueue.front(); 18 | myqueue.pop(); 19 | } 20 | ``` 21 | **[Run Code](https://rextester.com/OEC31098)** 22 | -------------------------------------------------------------------------------- /snippets/algorithm/is_sorted.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abhay Singh Yadav 3 | Date : Date format 21/10/2019 4 | Time : Time format 21:20 5 | Description : Finding the first unsorted element in the range [first, last). 6 | It returns an iterator to the first unsorted element in the range. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main(){ 13 | 14 | int array[] = { 0,10, 22, 43, 44, 17, 10, 20, 89,67 }; 15 | int *ptr; 16 | ptr = std::is_sorted_until(array, array + 10); 17 | 18 | std::cout <<"The first unsorted element is " << *ptr; 19 | std::cout<<"\n"; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /snippets/stack/pop.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Bhupesh Varshey 3 | Date : 13/11/2019 4 | Time : 01:47 PM 5 | Description : Demonstrating how pop() works in stack 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | stack mystack; //Initializing an empty stack. 14 | mystack.push(2); 15 | mystack.push(3); 16 | // Printing stack content. 17 | while (!mystack.empty()) { 18 | cout << mystack.top() << endl; 19 | } 20 | mystack.pop(); 21 | // Stack after pop() 22 | while (!mystack.empty()) { 23 | cout << mystack.top() << endl; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stack/push.md: -------------------------------------------------------------------------------- 1 | # push 2 | 3 | **Description** : push() function is used to insert an element at the top of the stack. The element is added to the stack container and the size of the stack is increased by 1. 4 | 5 | **Example**: 6 | ```cpp 7 | // Empty stack 8 | std::stack mystack; 9 | //pushing elements using push() 10 | mystack.push(0); 11 | mystack.push(1); 12 | mystack.push(2); 13 | 14 | while (!mystack.empty()) { 15 | //deleting elements using pop() 16 | std::cout << ' ' << mystack.top(); 17 | mystack.pop(); 18 | } 19 | ``` 20 | **[Run Code](https://rextester.com/JRY48091)** 21 | 22 | -------------------------------------------------------------------------------- /vector/capacity.md: -------------------------------------------------------------------------------- 1 | # capacity 2 | 3 | **Description** : 4 | - Returns the storage space currently allocated to vector 5 | 6 | -The size returned is greater than or equal to vector size, the extra space is to accommodate expansion without the need to relocate vector each time 7 | 8 | **Example** 9 | ```cpp 10 | //Declare the vector 11 | std::vector myvector{1,2,3,4,5}; 12 | 13 | //when capacity is exhausted, vector automatically expands 14 | std::cout<<"The maximum capacity of the vector is: "<< myvector.capacity(); 15 | 16 | ``` 17 | **[See Sample code](../snippets/vector/capacity.cpp)** 18 | **[Run Code](https://rextester.com/ZVN24902)** 19 | -------------------------------------------------------------------------------- /map/max_size.md: -------------------------------------------------------------------------------- 1 | # max_size 2 | 3 | **Description :** It returns an integer value which is equal to the maximum number of elements map container can hold. It accepts no parameters. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | // C++ program to Demonstrate map::max_size() function 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int main() { 14 | 15 | // initialize container 16 | std::map mp; 17 | 18 | // print the maximum size of map 19 | std::cout << "The max size of map is " << mp.max_size() ; 20 | 21 | return 0; 22 | } 23 | ``` 24 | 25 | **[Run Code](https://rextester.com/ZPP8806)** 26 | -------------------------------------------------------------------------------- /queue/front.md: -------------------------------------------------------------------------------- 1 | # front 2 | 3 | **Description** : front() function is used to reference the first element (ie. the oldest element) of the queue. This function can be used to fetch the first element of a queue. 4 | 5 | **Example**: 6 | ```cpp 7 | // Empty queue 8 | std::queue myqueue; 9 | 10 | // Add elements to queue using push() 11 | myqueue.push(2); 12 | myqueue.push(5); 13 | myqueue.push(4); 14 | myqueue.push(1); 15 | 16 | // Queue becomes 2, 5, 4, 1 17 | 18 | // Print the first/oldest element in the queue 19 | std::cout << myqueue.front(); 20 | ``` 21 | **[Run Code](https://rextester.com/NHSYM95891)** 22 | -------------------------------------------------------------------------------- /snippets/vector/rend.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Rok Krhlikar 3 | Date : 07/10/2019 4 | Time : 19:30 5 | Description : Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (which is considered its reverse end). 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() { 12 | // create a vector of 5 integers 13 | std::vector myVector{1, 2, 3, 4, 5}; 14 | 15 | // display the vector contents using reverse iterators 16 | for (auto it = myVector.rbegin(); it != myVector.rend(); it++) { 17 | std::cout << *it << " "; 18 | } 19 | std::cout << std::endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /vector/size.md: -------------------------------------------------------------------------------- 1 | # size 2 | 3 | **Description** : The list::vector() is a built-in function in C++ STL which is used to find the number of elements present in a vector container. 4 | 5 | **Example**: 6 | ```cpp 7 | // Creating a vector 8 | std::vector example; 9 | 10 | // Adding elements to the vector 11 | example.push_back(10); 12 | example.push_back(20); 13 | example.push_back(30); 14 | example.push_back(40); 15 | 16 | // getting size of the list 17 | int size = example.size(); 18 | 19 | std::cout << "The vector contains " << size << " elements"; 20 | ``` 21 | **[Run Code]( https://rextester.com/FLGXD19090)** -------------------------------------------------------------------------------- /algorithm/copy_n.md: -------------------------------------------------------------------------------- 1 | # copy_n 2 | 3 | **Description** : Copies n elements starting in the ranged passed, to another range beginning at passed argument. 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector origin {1, 2, 3}; 8 | std::vector destination; 9 | 10 | // Will copy 2 values starting at origin.begin, to destination 11 | std::copy_n(origin.begin(), 2, std::back_inserter(destination)); 12 | 13 | // destination is now {1, 2} 14 | for (auto value : destination) { 15 | std::cout << value << " "; 16 | } 17 | ``` 18 | **[See Sample code](../snippets/algorithm/copy_n.cpp)** 19 | **[Run Code](https://rextester.com/GXR34835)** -------------------------------------------------------------------------------- /algorithm/is_sorted.md: -------------------------------------------------------------------------------- 1 | # is_sorted 2 | 3 | **Description :** The C++ function std::is_sorted checks if the elements in range `[first, last]` are sorted in ascending order. If the elements are sorted in ascending order, the function returns true, else returns false. 4 | 5 | **Example :** 6 | 7 | ```cpp 8 | int main(){ 9 | 10 | std::vector v = {1, 5, 12, 17, 21, 35}; 11 | 12 | if(is_sorted(v.begin(), v.end())){ 13 | std::cout << "Vector is sorted\n"; 14 | } 15 | else{ 16 | std::cout << "Vector is not sorted\n"; 17 | } 18 | 19 | return 0; 20 | } 21 | ``` 22 | 23 | **[Run Code](https://rextester.com/EDDQ23470)** 24 | -------------------------------------------------------------------------------- /list/size.md: -------------------------------------------------------------------------------- 1 | # size 2 | 3 | **Description :** The list::size() is a built-in function in C++ STL which is used to find the number of elements present in a list container. 4 | 5 | **Example** : 6 | ```cpp 7 | // Creating a list 8 | std::list demoList; 9 | 10 | // Add elements to the List 11 | demoList.push_back(10); 12 | demoList.push_back(20); 13 | demoList.push_back(30); 14 | demoList.push_back(40); 15 | 16 | // getting size of the list 17 | int size = demoList.size(); 18 | 19 | std::cout << "The list contains " << size << " elements"; 20 | 21 | ``` 22 | **[Run Code](https://rextester.com/MJW36296)** 23 | -------------------------------------------------------------------------------- /snippets/algorithm/pop_heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(){ 6 | //sample vector 7 | std::vector v1 = {2,5,8,7,4,5}; 8 | 9 | //converting vector into heap 10 | make_heap(v1.begin(),v1.end()); 11 | //printing first element 12 | std::cout << "first element of the heap: " << v1.front() << std::endl; 13 | //outputs 8 14 | 15 | pop_heap(v1.begin(),v1.end()); 16 | //pops the maximum element in the heap 17 | //printing frist element 18 | std::cout << "first element of the heap after using pop_heap: " << v1.front() << std::endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /snippets/algorithm/to_string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Nishanth Sanjeev 3 | Date : 07/10/2019 4 | Time : 22:57 5 | Description : A simple function that converts a numerical value to a string. 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | // An example of converting an integer value to a string. 13 | std::string str1 = std::to_string(3456); 14 | 15 | // An example of converting a float value to a string. 16 | std::string str2 = std::to_string(12.19); 17 | 18 | // Printing the strings 19 | std::cout << str1 << endl; 20 | std::cout << str2 << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /list/cbegin.md: -------------------------------------------------------------------------------- 1 | # cbegin 2 | 3 | **Description** :he list::cbegin() is a built-in function in C++ STL which returns a constant random access iterator which points to the beginning of the list. 4 | 5 | **Example**: 6 | ```cpp 7 | // declaration of list 8 | std::list lis = { 15, 26, 37, 48, 59 }; 9 | 10 | // Prints the first element 11 | std::cout << "The first element is: " << *lis.cbegin(); 12 | 13 | // printing list elements 14 | std::cout << "\nList: "; 15 | 16 | for (auto it = lis.cbegin(); it != lis.end(); ++it) 17 | std::cout << *it << " "; 18 | 19 | ``` 20 | **[Run Code](https://rextester.com/NEDW55378)** 21 | -------------------------------------------------------------------------------- /list/crbegin.md: -------------------------------------------------------------------------------- 1 | # crbegin 2 | 3 | **Description** : The list::crbegin() is a built-in function in c++ STL that returns a constant reverse iterator which points to the last element of the list i.e reversed beginning of container. 4 | 5 | **Example**: 6 | ```cpp 7 | // declaration of the list 8 | std::list lis = { 109, 207, 305, 403, 501 }; 9 | 10 | // prints the last element 11 | std::cout << "The last element is: " << *lis.crbegin(); 12 | std::cout << "\nList: "; 13 | 14 | for (auto it = lis.crbegin(); it != lis.crend(); ++it) 15 | std::cout << *it << " "; 16 | 17 | ``` 18 | **[Run Code](https://rextester.com/XVFTEG19372)** 19 | -------------------------------------------------------------------------------- /list/front.md: -------------------------------------------------------------------------------- 1 | # front 2 | 3 | **Description** :The list::front() is a built-in function in C++ STL which is used to return a reference to the first element in a list container. 4 | 5 | **Example**: 6 | ```cpp 7 | // Creating a list 8 | std::list demoList; 9 | 10 | // Add elements to the List 11 | demoList.push_back(10); 12 | demoList.push_back(20); 13 | demoList.push_back(30); 14 | demoList.push_back(40); 15 | 16 | // get the first element using front() 17 | int ele = demoList.front(); 18 | 19 | // Print the first element 20 | std::cout << ele; 21 | 22 | ``` 23 | **[Run Code](https://rextester.com/SKCG67239)** -------------------------------------------------------------------------------- /priority_queue/push.md: -------------------------------------------------------------------------------- 1 | # push 2 | 3 | **Description** : inserts a element into the queue and then reorders it to contain the largest among the elements in the queue. 4 | 5 | **Example**: 6 | ``` cpp 7 | 8 | std::priority_queue my; 9 | 10 | my.push(2); 11 | my.push(10); 12 | my.push(4); 13 | 14 | // returns the first value of the queue(largest) i.e 10 15 | std::cout << my.top(); 16 | 17 | // 20 is larger than 10 so inserting it,20 becomes the largest 18 | my.push(20); 19 | 20 | // prints the largest value i.e 20 21 | std::cout << my.top(); 22 | 23 | ``` 24 | 25 | **[Run Code](https://rextester.com/XWWB77826)** 26 | 27 | 28 | -------------------------------------------------------------------------------- /algorithm/adjacent_find.md: -------------------------------------------------------------------------------- 1 | # adjacent_find 2 | 3 | **Description :** Binary function which returns first adjacent element pairs based on certain condition (as third argument). Default condition checks equality. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | std::vector v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 }; 9 | 10 | // Binary function which returns first adjacent element pairs based on certain condition (as third argument) . 11 | // Default condition checks equality. 12 | auto i = std::adjacent_find (v.begin(), v.end()); 13 | std::cout << "The first pair of repeated elements are: " << *i <<'\n'; 14 | ``` 15 | **[Run Code](https://rextester.com/HBWU15163)** -------------------------------------------------------------------------------- /algorithm/copy.md: -------------------------------------------------------------------------------- 1 | # copy 2 | 3 | **Description** : Copies the elements in the range, defined by `[first, last)`, to another range beginning at passed argument. 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector origin {1, 2, 3}; 8 | std::vector destination; 9 | 10 | // Will copy from origin [begin, end), to destination 11 | std::copy(origin.begin(), origin.end(), std::back_inserter(destination)); 12 | 13 | // destination is now {1, 2, 3} 14 | for (auto value : destination) { 15 | std::cout << value << " "; 16 | } 17 | ``` 18 | **[See Sample code](../snippets/algorithm/copy.cpp)** 19 | **[Run Code](https://rextester.com/DPG88459)** -------------------------------------------------------------------------------- /snippets/queue/empty.cpp: -------------------------------------------------------------------------------- 1 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 2 | /* 3 | Author : Timothy Itodo 4 | Date : 30/05/2019 5 | Time : 01:00 PM 6 | Description : empty() function is used to check if the queue container is empty or not. This function returns true, if the queue is empty or false, otherwise. 7 | */ 8 | #include 9 | #include 10 | 11 | int main(){ 12 | // Empty queue 13 | std::queue myqueue; 14 | 15 | if(myqueue.empty()){ 16 | std::cout << "My queue is empty"; 17 | } 18 | else{ 19 | std::cout << "My queue is not empty"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /snippets/stack/size.cpp: -------------------------------------------------------------------------------- 1 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 2 | /* 3 | Author : Kevin Harvell 4 | Date : 01/06/2019 5 | Time : 11:30 AM 6 | Description : size() function is used to return the size of the stack or the number of elements in the stack. 7 | */ 8 | #include 9 | #include 10 | 11 | int main() { 12 | std::stack mystack; 13 | mystack.push(4); 14 | mystack.push(5); 15 | mystack.push(9); 16 | mystack.push(1); 17 | mystack.push(3); 18 | 19 | // Stack from top to bottom becomes 3, 1, 9, 5, 4 20 | 21 | // Print the size of mystack (5) 22 | std::cout << mystack.size(); 23 | } -------------------------------------------------------------------------------- /set/rend.md: -------------------------------------------------------------------------------- 1 | # rend 2 | 3 | **Description :** 4 | This method returns size of the set of numbers until the end is encountered. 5 | 6 | **Example :** 7 | ```cpp 8 | 9 | #include 10 | using namespace std; 11 | int main() 12 | { 13 | 14 | int arr[] = { 14, 12, 15, 11, 10 }; 15 | 16 | // initializes the set from an array 17 | set s(arr, arr + 5); 18 | 19 | set::reverse_iterator rit; 20 | 21 | // prints all elements in reverse order 22 | for (rit = s.rbegin(); rit != s.rend(); rit++) 23 | cout << *rit << " "; 24 | 25 | return 0; 26 | } 27 | 28 | ``` 29 | 30 | **[Run Code](https://ideone.com/z0iUbJ)** -------------------------------------------------------------------------------- /stack/pop.md: -------------------------------------------------------------------------------- 1 | # pop 2 | 3 | **Description** : pop() function is used to remove an element from the top of the stack(newest element in the stack). The element is removed to the stack container and the size of the stack is decreased by 1. 4 | 5 | **Example**: 6 | ```cpp 7 | // Empty stack 8 | std::stack mystack; 9 | //pushing elements using push() 10 | mystack.push(0); 11 | mystack.push(1); 12 | mystack.push(2); 13 | 14 | while (!mystack.empty()) { 15 | //deleting elements using pop() 16 | std::cout << ' ' << mystack.top(); 17 | mystack.pop(); 18 | } 19 | ``` 20 | **[Run Code](https://rextester.com/MIKHGL76695)** 21 | -------------------------------------------------------------------------------- /vector/back.md: -------------------------------------------------------------------------------- 1 | # back 2 | 3 | **Description :** 4 | 5 | Returns a reference to the last element in the vector 6 | 7 | **Example** : 8 | 9 | ```cpp 10 | // Initialize int vector 11 | std::vector myVector = {1, 2, 3, 4, 5}; 12 | 13 | // Save reference to the last element 14 | int& lastEle = myVector.back(); 15 | 16 | // Last element is 5 17 | std::cout << myVector.back() << " "; 18 | 19 | // Change the last element by changing the reference variable lastEle 20 | lastEle = 10; 21 | 22 | // Last element is 10 23 | std::cout << myVector.back() << " "; 24 | 25 | ``` 26 | **[Run Code](https://rextester.com/RQEY20725)** 27 | -------------------------------------------------------------------------------- /snippets/algorithm/copy.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 02:00 5 | Description : Copies the elements in one range to another range. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | std::vector origin {1, 2, 3}; 14 | std::vector destination; 15 | 16 | // Will copy from origin [begin, end), to destination 17 | std::copy(origin.begin(), origin.end(), std::back_inserter(destination)); 18 | 19 | // destination is now {1, 2, 3} 20 | for (auto value : destination) { 21 | std::cout << value << " "; 22 | } 23 | } -------------------------------------------------------------------------------- /vector/front.md: -------------------------------------------------------------------------------- 1 | # front 2 | 3 | **Description** :The vector::front() is a built-in function in C++ STL which is used to return a reference to the first element in a vector container. 4 | 5 | **Example**: 6 | ```cpp 7 | // Creating a vector 8 | std::vector example; 9 | 10 | // Add elements to the List 11 | example.push_back(10); 12 | example.push_back(20); 13 | example.push_back(30); 14 | example.push_back(40); 15 | 16 | // get the first element using front() 17 | int & ele = example.front(); 18 | 19 | // Print the first element 20 | std::cout << ele; 21 | 22 | ``` 23 | **[Run Code](https://rextester.com/WASUB54049)** 24 | -------------------------------------------------------------------------------- /snippets/algorithm/copy_n.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 02:00 5 | Description : Copies n elements starting at a position to another range. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | std::vector origin {1, 2, 3}; 14 | std::vector destination; 15 | 16 | // Will copy 2 values starting at origin.begin, to destination 17 | std::copy_n(origin.begin(), 2, std::back_inserter(destination)); 18 | 19 | // destination is now {1, 2} 20 | for (auto value : destination) { 21 | std::cout << value << " "; 22 | } 23 | } -------------------------------------------------------------------------------- /snippets/algorithm/remove.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 09/09/2019 4 | Time : Time format 23:00 5 | Description : Removes elements from vector that satisfies a criteria. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | std::vector v {3, 5, 3, 1, 2, 3}; 14 | 15 | // remove all elements that are 3 16 | auto newEndIt = std::remove(v.begin(), v.end(), 3); 17 | 18 | // Erase elements from [newEndIt, v.end()] 19 | v.erase(newEndIt, v.end()); 20 | 21 | // v is now {5, 1, 2} 22 | for (auto value : v) { 23 | std::cout << value << " "; 24 | } 25 | } -------------------------------------------------------------------------------- /set/rbegin.md: -------------------------------------------------------------------------------- 1 | # rbegin 2 | 3 | **Description :** 4 | This method returns an iterator pointing to the last element in the set. 5 | 6 | **Example :** 7 | ```cpp 8 | 9 | #include 10 | #include 11 | 12 | int main() { 13 | // Create a set object holding integers 14 | std::set s{ 14, 12, 15, 11, 10 }; 15 | 16 | std::set::reverse_iterator rit = s.rbegin(); 17 | 18 | // prints all elements in reverse order 19 | for (; rit != s.rend(); ++rit) { 20 | std::cout << *rit < https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 2 | /* 3 | Author : Timothy Itodo 4 | Date : 30/05/2019 5 | Time : 01:00 PM 6 | Description : size() function is used to return the size of the queue or the number of elements in the queue. 7 | */ 8 | #include 9 | #include 10 | 11 | int main(){ 12 | std::queue myqueue; 13 | myqueue.push(4); 14 | myqueue.push(5); 15 | myqueue.push(9); 16 | myqueue.push(1); 17 | myqueue.push(3); 18 | 19 | // Queue becomes 4, 5, 9, 1, 3 20 | 21 | // Print the size of myqueue 22 | std::cout << myqueue.size(); 23 | } 24 | -------------------------------------------------------------------------------- /vector/crbegin.md: -------------------------------------------------------------------------------- 1 | # crbegin 2 | 3 | **Description** : The function returns an const_reverse_iterator pointing to the last element in the container (i.e., its reverse beginning). The iterator points to past-the-end element of the vector. 4 | 5 | **Example**: 6 | ```cpp 7 | // Demonstrates cbegin() 8 | #include 9 | #include 10 | 11 | int main () 12 | { 13 | std::vector myvector = {1,2,3,4,5}; 14 | 15 | std::cout << "myvector backwards:"; 16 | for (auto rit = myvector.crbegin(); rit != myvector.crend(); ++rit) 17 | std::cout << ' ' << *rit; 18 | std::cout << '\n'; 19 | 20 | return 0; 21 | } 22 | ``` 23 | **[Run Code](https://rextester.com/HOY41620)** 24 | 25 | -------------------------------------------------------------------------------- /algorithm/remove.md: -------------------------------------------------------------------------------- 1 | # remove 2 | 3 | **Description** : Removes elementes from range `[first, last)` that matches a value and returns a new past-the-end iterator for the new end of range. 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector v {3, 5, 3, 1, 2, 3}; 8 | 9 | // removes all elements that are 3 10 | auto newEndIt = std::remove(v.begin(), v.end(), 3); 11 | 12 | // Erase elements from [newEndIt, v.end()] 13 | v.erase(newEndIt, v.end()); 14 | 15 | // v is now {5, 1, 2} 16 | for (auto value : v) { 17 | std::cout << value << " "; 18 | } 19 | ``` 20 | **[See Sample code](../snippets/algorithm/remove.cpp)** 21 | **[Run Code](https://rextester.com/WCZY78870)** -------------------------------------------------------------------------------- /vector/data.md: -------------------------------------------------------------------------------- 1 | # data 2 | 3 | **Description** : The function returns a pointer to the first element in the array which is used internally by the vector. It doesn't accept any parameters. 4 | 5 | 6 | **Example**: 7 | 8 | ```cpp 9 | int main(){ 10 | 11 | //vector initialisation 12 | std::vector v = { 1, 2, 3, 4, 5 }; 13 | 14 | //memory pointer pointing to the first element 15 | int* pos = v.data(); 16 | 17 | // prints the vector 18 | std::cout << "The vector elements are: "; 19 | for (int i = 0; i < v.size(); ++i) 20 | std::cout << *pos++ << " "; 21 | 22 | return 0; 23 | } 24 | ``` 25 | **[Run Code](https://rextester.com/DHEP56251)** 26 | -------------------------------------------------------------------------------- /algorithm/find_if.md: -------------------------------------------------------------------------------- 1 | # find_if 2 | 3 | **Description** : Returns the first element in the range [first, last) that satisfies specific criteria(searches for an element for which predicate/condition p returns *true*). 4 | 5 | **Example**: 6 | ```cpp 7 | bool IsOdd (int i) { 8 | return ((i%2)==1); 9 | } 10 | 11 | int main(){ 12 | std::vector v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 }; 13 | if(std::find_if(std::begin(v), std::end(v), IsOdd) != std::end(v)){ 14 | std::cout <<"\n Odd Value Found"; 15 | } 16 | else 17 | std::cout<<"No match !!"; 18 | return 0; 19 | } 20 | ``` 21 | **[Run Code](https://rextester.com/ANC42820)** 22 | -------------------------------------------------------------------------------- /algorithm/find_if_not.md: -------------------------------------------------------------------------------- 1 | # find_if_not 2 | 3 | **Description** : Returns the first element in the range [first, last) that satisfies specific criteria(searches for an element for which predicate q returns *false*). 4 | 5 | **Example**: 6 | ```cpp 7 | bool IsOdd (int i) { 8 | return ((i%2)==1); 9 | } 10 | 11 | int main(){ 12 | std::vector v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 }; 13 | if(std::find_if_not(std::begin(v), std::end(v), IsOdd) != std::end(v)){ 14 | std::cout <<"\n First Even Value"; 15 | } 16 | else 17 | std::cout<<"No match !!"; 18 | return 0; 19 | } 20 | ``` 21 | **[Run Code](https://rextester.com/TZCBJG77851)** 22 | -------------------------------------------------------------------------------- /algorithm/sort.md: -------------------------------------------------------------------------------- 1 | # sort 2 | 3 | **Description :** This function is implemented as Quick-sort. The complexity of it is O(N*log(N)). 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | void show(int a[]) { 9 | for(int i = 0; i < 10; ++i) 10 | std::cout << a[i] << " "; 11 | } 12 | 13 | int main() { 14 | 15 | int a[10]= {1, 5, 8, 9, 6, 7, 3, 4, 2, 0}; 16 | std::cout << "\n The array before sorting is : "; 17 | show(a); 18 | 19 | std::sort(a, a+10); 20 | 21 | std::cout << "\n\n The array after sorting is : "; 22 | show(a); 23 | 24 | return 0; 25 | } 26 | ``` 27 | **[Run Code](https://rextester.com/JJZQO18587)** 28 | -------------------------------------------------------------------------------- /queue/emplace.md: -------------------------------------------------------------------------------- 1 | # emplace 2 | 3 | **Description** : emplace() function is used to add a new element at the end of the queue, after its current last element. The element is added to the queue container and the size of the queue is increased by 1. 4 | 5 | **Example**: 6 | ```cpp 7 | // Empty queue 8 | std::queue myqueue; 9 | 10 | // adding elements into queue using emplace() 11 | myqueue.emplace(0); 12 | myqueue.emplace(1); 13 | myqueue.emplace(2); 14 | 15 | // print contents of queue 16 | while (!myqueue.empty()) { 17 | std::cout << ' ' << myqueue.front(); 18 | myqueue.pop(); 19 | } 20 | ``` 21 | **[Run Code](https://rextester.com/RZID79506)** 22 | -------------------------------------------------------------------------------- /set/equal_range.md: -------------------------------------------------------------------------------- 1 | # equal_range 2 | 3 | **Description :** 4 | This method is used to know if value exits an set. If a value is element of set the method return 1, and otherwise return 0. 5 | 6 | **Example :** 7 | ```cpp 8 | //Run Code To Demonstrate use of set.equal_range() 9 | #include 10 | #include 11 | 12 | int main () 13 | { 14 | std::set myset = {1, 2, 3, 4}; // myset: 1 2 3 4 15 | 16 | std::pair::const_iterator,std::set::const_iterator> range = myset.equal_range(2); 17 | 18 | std::cout << "The range is from " << *range.first << " to " << *range.second << std::endl; 19 | 20 | return 0; 21 | } 22 | ``` 23 | 24 | **[Run Code](https://ideone.com/gof6oy)** -------------------------------------------------------------------------------- /snippets/vector/data.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Yashwi Shah 3 | Date : 10/15/2019 4 | Time : 12:40 5 | Description : The function returns a pointer to the first element in the array which is used internally by the vector. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() 12 | { 13 | // vector initialisation 14 | std::vector v = { 1, 2, 3, 4, 5 }; 15 | 16 | // memory pointer pointing to the first element 17 | int* pos = v.data(); 18 | 19 | // prints the vector 20 | std::cout << "The vector elements are: "; 21 | for (int i = 0; i < v.size(); ++i){ 22 | std::cout << *pos++ << " "; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /snippets/vector/max_size.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Y. Sai Sriram 3 | Date : 29/09/2019 4 | Time : 08:43 5 | Description : Returns the maximum number of elements that a vector can hold. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main(){ 12 | //create two vectors, one empty and one having 6 elements 13 | std::vector vector1{10, 20, 30, 40, 50, 60}; 14 | std::vector vector2; 15 | 16 | //notice how both vectors' max_size function return the same value 17 | std::cout << "The maximum size of vector1 is: " << vector1.max_size() << std::endl; 18 | std::cout << "The maximum size of vector2 is: " << vector2.max_size() << std::endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /algorithm/fill.md: -------------------------------------------------------------------------------- 1 | # fill 2 | 3 | **Description :** This function assigns the value ‘val’ to all the elements in the range [begin, end), where ‘begin’ is the initial position and ‘end’ is the last position. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | int main () { 9 | std::vector myvector (8); // myvector: 0 0 0 0 0 0 0 0 10 | 11 | std::fill (myvector.begin(),myvector.begin()+4,5); // myvector: 5 5 5 5 0 0 0 0 12 | std::fill (myvector.begin()+3,myvector.end()-2,8); // myvector: 5 5 5 8 8 8 0 0 13 | 14 | for(int i = 0; i<10; i++) 15 | cout< 10 | #include 11 | 12 | int main() { 13 | // declaration of map container 14 | std::map mymap; 15 | mymap['a'] = 1; 16 | mymap['b'] = 2; 17 | mymap['c'] = 3; 18 | 19 | // using end() to print map 20 | for (auto it = mymap.begin(); it != mymap.end(); ++it) 21 | std::cout << it->first << " = " 22 | << it->second << '\n'; 23 | return 0; 24 | } 25 | ``` 26 | **[Run Code](https://rextester.com/AUMIKR77967)** 27 | -------------------------------------------------------------------------------- /map/rend.md: -------------------------------------------------------------------------------- 1 | # rend 2 | **Description** This function returns a reverse iterator pointing ot the element right before the first key value pair in the map 3 | 4 | **Example** : 5 | ```cpp 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | map mymap; 12 | mymap.insert(make_pair('a',1)); 13 | mymap.insert(make_pair('b',3)); 14 | mymap.insert(make_pair('c',5)); 15 | 16 | 17 | for(auto i = mymap.rbegin(); i != mymap.rend();i++){ 18 | cout<< i -> first << " = " << i -> second << endl; 19 | } 20 | 21 | 22 | return 0; 23 | } 24 | ``` 25 | 26 | [Run Code](https://rextester.com/DHB11056) 27 | -------------------------------------------------------------------------------- /stack/emplace.md: -------------------------------------------------------------------------------- 1 | # emplace 2 | 3 | **Description** : The emplace() member inserts an element at the top 4 | of the stack and constructs the element in place from the arguments 5 | provided. This can be more efficient than push in certain situations 6 | and avoid temporaries. emplace() effectively forwards its arguments to 7 | a constructor that is placed at the top of the stack. 8 | 9 | **Example**: 10 | ```cpp 11 | std::stack st; 12 | // Add some strings to the stack 13 | st.emplace("C++ world"); 14 | st.emplace("Hello"); 15 | std::cout << st.top(); 16 | st.pop(); 17 | std::cout << ", " << st.top() << std::endl; 18 | ``` 19 | **[Run Code](https://rextester.com/TJAOH56215)** 20 | -------------------------------------------------------------------------------- /algorithm/is_permutation.md: -------------------------------------------------------------------------------- 1 | # is_permutation 2 | 3 | **Description :** is_permutation() compares the elements in both the containers and returns a `true` value if all the elements in both the containers are found to be matching even if in different order else returns `false`. The first range is from `[first1, last1)` and the second starts from `first2`. 4 | 5 | **Example** : 6 | ```cpp 7 | std::vector a{ 1, 2, 3, 4 }; 8 | std::vector b{ 3, 4, 1, 2 }; 9 | 10 | if(is_permutation(a.begin(),a.end(),b.begin())) { 11 | std::cout<<"a and b have same elements"; 12 | } 13 | else std::cout<<"a and b don't have the same elements"; 14 | ``` 15 | **[Run Code](https://rextester.com/CTOTV33649)** 16 | -------------------------------------------------------------------------------- /vector/insert.md: -------------------------------------------------------------------------------- 1 | # insert 2 | 3 | **Description** : insert() is a function in C++ STL which inserts new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted. 4 | 5 | **Example**: 6 | ```cpp 7 | // initialising the vector 8 | std::vector vec = { 10, 20, 30, 40 }; 9 | 10 | // inserts 5 at front 11 | auto it = vec.insert(vec.begin(), 5); 12 | 13 | std::cout << "The vector elements are: "; 14 | for (auto it = vec.begin(); it != vec.end(); ++it) 15 | std::cout << *it << " "; 16 | std::cout << std::endl; 17 | ``` 18 | **[See Sample code](../snippets/vector/insert.cpp)** 19 | **[Run Code](https://rextester.com/AHENR90238)** 20 | -------------------------------------------------------------------------------- /algorithm/iota.md: -------------------------------------------------------------------------------- 1 | # iota 2 | 3 | **Description :** This function operates on a range [first, last) of array elements and assigns the elements with successive values of val, as if incremented with ++val after each element is written. Also, the header file is required to access the function. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | int main() { 9 | int arr[10]; 10 | //Initialising the starting value as 500 11 | int num = 500; 12 | //Applying the function iota to each element 13 | std::iota(arr, arr + 10, num); 14 | // Printing the final output 15 | for(int i = 0; i<10; i++) 16 | cout< 10 | auto minMax1 = std::minmax(a, b); 11 | std::cout << "Min is " << minMax1.first << 12 | " Max is " << minMax1.second << std::endl; 13 | 14 | // returns pair <-1, 10> 15 | auto minMax2 = std::minmax({-1, 3, 10, 0}); 16 | std::cout << "Min is " << minMax2.first << 17 | " Max is " << minMax2.second << std::endl; 18 | 19 | ``` 20 | **[See Sample code](../snippets/algorithm/minmax.cpp)** 21 | **[Run Code](https://rextester.com/AYBQP34022)** -------------------------------------------------------------------------------- /set/lower_bound.md: -------------------------------------------------------------------------------- 1 | # lower_bound 2 | 3 | **Description :** 4 | This method returns an iterator pointing to the first element in the container which is greater or equal to the passed parameter 'x'. 5 | 6 | **Example :** 7 | ```cpp 8 | //Run Code To Demonstrate use of set.lower_bound(x) 9 | #include 10 | #include 11 | 12 | int main(){ 13 | // Create a set object holding integers 14 | std::set mySet {1,2,4,-5,6}; 15 | 16 | std::cout << "The lower_bound of 4 is: " << *(mySet.lower_bound(4)) << std::endl; 17 | 18 | std::cout << "The lower_bound of 5 is: " << *(mySet.lower_bound(5)) << std::endl; 19 | 20 | return 0; 21 | } 22 | 23 | ``` 24 | 25 | **[Run Code](https://ideone.com/pIWrf3)** 26 | -------------------------------------------------------------------------------- /snippets/set/rbegin.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : Cameron Bradley 5 | Date : 11/10/2020 6 | Time : 13:30 7 | Description : Return an iterator pointing to the last element in the set. 8 | */ 9 | #include 10 | #include 11 | 12 | int main() { 13 | // Create a set object holding integers 14 | std::set s{ 14, 12, 15, 11, 10 }; 15 | 16 | std::set::reverse_iterator rit = s.rbegin(); 17 | 18 | // prints all elements in reverse order 19 | for (; rit != s.rend(); ++rit) { 20 | std::cout << *rit << std::endl; 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /snippets/vector/reserve.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Y. Sai Sriram 3 | Date : 29/09/2019 4 | Time : 09:03 5 | Description : Helps specify the minimum size of a vector 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main(){ 12 | //create an empty vector 13 | std::vector myvector; 14 | 15 | //reserve a size of 'atleast' 5 elements 16 | myvector.reserve(5); 17 | 18 | //inserting 5 elements 19 | for(int i=0; i<5; i++) 20 | { 21 | myvector.push_back(i); 22 | } 23 | 24 | //when 6th element is added, vector automatically grows 25 | myvector.push_back(5); 26 | 27 | //prints '6' 28 | std::cout << "The size of the vector is: " << myvector.size() << std::endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /algorithm/none_of.md: -------------------------------------------------------------------------------- 1 | # none_of 2 | 3 | **Description :** This function operates on whole range of array elements and checks for a given property on every element and returns true when no element in range satisfies specified property, else returns false. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | int main() { 14 | 15 | std::vector v{1, 3, 5, 7}; 16 | 17 | if (std::none_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; })) { 18 | std::cout << "No number is even\n"; 19 | } 20 | else { 21 | std::cout << "A number is even\n"; 22 | } 23 | } 24 | ``` 25 | **[Run Code](https://rextester.com/KCAUPV41408)** 26 | -------------------------------------------------------------------------------- /snippets/unordered_map/bucket.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | // Declaration 9 | std::unordered_map m; 10 | 11 | // Initilisation 12 | m = { { "India", "England" }, 13 | { "U.S.", "Washington" }, 14 | { "France", "Paris" } }; 15 | 16 | // prints the bucket number of the beginning element 17 | auto it = m.begin(); 18 | 19 | // stores the bucket number of the key k 20 | int number = m.bucket(it->first); 21 | std::cout << "The bucket number of key " << it->first 22 | << " is " << number; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /algorithm/replace.md: -------------------------------------------------------------------------------- 1 | # replace 2 | 3 | **Description** : Replaces all elements satisfying equal to `old_value` with `new_value` in the range `[first, last)`. 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector origin {3, 5, 3, 1, 2, 3}; 8 | 9 | // replaces 3 by 0 10 | std::replace(origin.begin(), //first 11 | origin.end(), //last 12 | 3, //old_vale 13 | 0 //new_value 14 | ); 15 | 16 | // origin is now {0, 5, 0, 1, 2, 0} 17 | for (auto value : origin) { 18 | std::cout << value << " "; 19 | } 20 | ``` 21 | **[See Sample code](../snippets/algorithm/replace.cpp)** 22 | **[Run Code](https://rextester.com/RMU81526)** -------------------------------------------------------------------------------- /snippets/algorithm/lower_bound.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 23:00 5 | Description : Retrieve smallest element that is not smaller than given value. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() { 13 | std::vector vec = {3, -1, 2, 5, 10, 6, 7, 4}; 14 | // lower_bound requires a sorted input {-1, 2, 3, 4, 5, 6, 7, 10} 15 | std::sort(vec.begin(), vec.end()); 16 | 17 | auto lowerBoundIt = std::lower_bound(vec.begin(), vec.end(), 5); 18 | // prints 5 (first value element that is not smaller than 5 is 5) 19 | std::cout << "The lower bound element is " << *lowerBoundIt << '\n'; 20 | } -------------------------------------------------------------------------------- /snippets/algorithm/minmax.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 23:00 5 | Description : Retrieve min and max of values or from a list. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() { 13 | int a = 7, b = 3; 14 | 15 | // returns pair <3, 7> 16 | auto minMax1 = std::minmax(a, b); 17 | std::cout << "Min is " << minMax1.first << 18 | " Max is " << minMax1.second << std::endl; 19 | 20 | // returns pair <-1, 10> 21 | auto minMax2 = std::minmax({-1, 3, 10, 0}); 22 | std::cout << "Min is " << minMax2.first << 23 | " Max is " << minMax2.second << std::endl; 24 | } -------------------------------------------------------------------------------- /algorithm/pop_heap.md: -------------------------------------------------------------------------------- 1 | # pop_heap 2 | 3 | **Description :** This function is used to delete the maximum element of the heap. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | //sample vector 9 | std::vector v1 = {2,5,8,7,4,5}; 10 | 11 | //converting vector into heap 12 | make_heap(v1.begin(),v1.end()); 13 | //printing first element 14 | std::cout << "first element of the heap: " << v1.front() << std::endl; 15 | //outputs 8 16 | 17 | pop_heap(v1.begin(),v1.end()); 18 | //pops the maximum element in the heap 19 | //printing frist element 20 | std::cout << "first element of the heap after using pop_heap: " << v1.front() << std::endl; 21 | ``` 22 | **[Run Code](https://rextester.com/MWK38216)** 23 | -------------------------------------------------------------------------------- /snippets/algorithm/sort_heap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Italo Vinicius 3 | Date : 01/10/2020 4 | Time : 21:28 PM 5 | Description : Sorts a heap within the range specified by start and end. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() 13 | { 14 | std::vector v = {3, 1, 4, 1, 5, 9}; 15 | 16 | std::make_heap(v.begin(), v.end()); 17 | 18 | std::cout << "heap:\t"; 19 | for (const auto &i : v) { 20 | std::cout << i << ' '; 21 | } 22 | 23 | std::sort_heap(v.begin(), v.end()); 24 | 25 | std::cout << "\nsorted:\t"; 26 | for (const auto &i : v) { 27 | std::cout << i << ' '; 28 | } 29 | std::cout << '\n'; 30 | } -------------------------------------------------------------------------------- /snippets/map/emplace.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Nikhit 3 | Date : 20/10/2019 4 | Time : 16:15 5 | Description : Demonstration the emplace key-value pair in STL map 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() { 13 | // creating a container and inserting values using "emplace" function 14 | std::map ages; 15 | ages.emplace("John", 45); 16 | ages.emplace("Stan", 65); 17 | ages.emplace("Mark", 25); 18 | ages.emplace("Zuck", 30); 19 | 20 | std::cout << "Key\tValue\n"; 21 | for(auto i = ages.begin(); i != ages.end(); ++i) { 22 | std::cout << i->first << "\t" << i->second << "\n"; 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /algorithm/lower_bound.md: -------------------------------------------------------------------------------- 1 | # lower_bound 2 | 3 | **Description** : Returns an iterator pointing to the first element in the range `[first, last)` which does not compare less than value passed. 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector vec = {3, -1, 2, 5, 10, 6, 7, 4}; 8 | // lower_bound requires a sorted input {-1, 2, 3, 4, 5, 6, 7, 10} 9 | std::sort(vec.begin(), vec.end()); 10 | 11 | auto lowerBoundIt = std::lower_bound(vec.begin(), vec.end(), 5); 12 | // prints 5 (first value element that is not smaller than 5 is 5) 13 | std::cout << "The lower bound element is " << *lowerBoundIt << '\n'; 14 | 15 | ``` 16 | **[See Sample code](../snippets/algorithm/lower_bound.cpp)** 17 | **[Run Code](https://rextester.com/KFK22059)** -------------------------------------------------------------------------------- /list/push_back.md: -------------------------------------------------------------------------------- 1 | # push_back 2 | 3 | **Description** : The list::push_back() function in C++ STL appends an element to the end of a list container. 4 | 5 | **Example**: 6 | 7 | ```cpp 8 | // Demonstrates push_back() 9 | #include 10 | #include 11 | 12 | int main(){ 13 | //declare an empty list 14 | std::list mylist; 15 | 16 | //append elements to the list 17 | mylist.push_back(1); 18 | mylist.push_back(2); 19 | mylist.push_back(3); 20 | 21 | //print list elements 22 | std::cout << "List contains:" << std::endl; 23 | for (auto element : mylist) { 24 | std::cout << element << " "; 25 | } 26 | 27 | return 0; 28 | } 29 | ``` 30 | **[Run Code](https://rextester.com/UJJF73793)** 31 | -------------------------------------------------------------------------------- /snippets/algorithm/replace.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 09/09/2019 4 | Time : Time format 23:00 5 | Description : Replace elements from vector by a new value. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | std::vector origin {3, 5, 3, 1, 2, 3}; 14 | 15 | // replaces 3 by 0 16 | std::replace(origin.begin(), //first 17 | origin.end(), //last 18 | 3, //old_vale 19 | 0 //new_value 20 | ); 21 | 22 | // origin is now {0, 5, 0, 1, 2, 0} 23 | for (auto value : origin) { 24 | std::cout << value << " "; 25 | } 26 | } -------------------------------------------------------------------------------- /snippets/map/contains.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Liel Fridman 3 | Date : 25/10/2019 4 | Time : 16:13 5 | Description : create a simple map and test for two keys: 1 and 2. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() { 12 | /* Should print: 13 | 2 found. 14 | 1 not found. */ 15 | std::map example = {{2, 3}, {4, 5}}; 16 | 17 | if (example.contains(2)) { 18 | std::cout << "2 found." << std::endl; 19 | } else { 20 | std::cout << "2 not found." << std::endl; 21 | } 22 | 23 | if (example.contains(1)) { 24 | std::cout << "1 found." << std::endl; 25 | } else { 26 | std::cout << "1 not found." << std::endl; 27 | } 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /vector/shrink_to_fit.md: -------------------------------------------------------------------------------- 1 | # shrink_to_fit 2 | 3 | **Description** : Reduces the capacity of the container to fit its size. 4 | 5 | **Example**: 6 | ```cpp 7 | //create a vector of 5 elements 8 | std::vector myvec{10, 20, 30, 40, 50}; 9 | 10 | //adding a sixth element, vector's capacity will increase for future insertions 11 | myvec.push_back(60); 12 | 13 | //output the inital capacity 14 | std::cout << "Initial capacity is: " << myvec.capacity(); 15 | 16 | //output the shrunk capacity 17 | myvec.shrink_to_fit(); 18 | 19 | std::cout << "Shrunk capacity is: " << myvec.capacity(); 20 | ``` 21 | 22 | **[See Sample Code](../snippets/vector/shrink_to_fit.cpp)** 23 | **[Run Code](https://rextester.com/PQZOBU11154)** 24 | -------------------------------------------------------------------------------- /vector/max_size.md: -------------------------------------------------------------------------------- 1 | # max_size 2 | 3 | **Description** : 4 | - Returns the maximum number of elements that a vector can hold. 5 | - Depends on the limitations of the system on which program is running. 6 | 7 | **Example**: 8 | ```cpp 9 | //Create two vectors, one empty and one having 6 elements 10 | std::vector vector1; 11 | std::vector vector2{10, 30, 30, 40, 50, 60}; 12 | 13 | //Display the maximum number of elements both can hold 14 | std::cout << "The maximum size of vector1 is: " << vector1.max_size() << std::endl; 15 | std::cout << "The maximum size of vector2 is: " << vector2.max_size() << std::endl; 16 | ``` 17 | 18 | **[See Sample Code](../snippets/vector/max_size.cpp)** 19 | **[Run Code](https://rextester.com/FPVV25502)** 20 | 21 | -------------------------------------------------------------------------------- /map/find.md: -------------------------------------------------------------------------------- 1 | # find 2 | 3 | **Description :** The map::find() is a function which returns an iterator through which the key and value pair can be accessed. If not found the iterator returns map.end() 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | // Demonstrates find() 9 | #include 10 | #include 11 | 12 | int main() { 13 | // declaration of map container 14 | std::map mymap; 15 | mymap[0] = 10; 16 | mymap[1] = 11; 17 | mymap[2] = 12; 18 | mymap[3] = 13; 19 | 20 | // using find() function 21 | auto itr = mymap.find(1); 22 | std::cout << "The value at position " << itr->first << " is " << itr->second << "\n"; 23 | return 0; 24 | } 25 | ``` 26 | **[Run Code](https://rextester.com/TNMP46907)** 27 | -------------------------------------------------------------------------------- /vector/crend.md: -------------------------------------------------------------------------------- 1 | # crend 2 | 3 | **Description** : The function returns an reverse_iterator pointing to the last element in the container (i.e., its reverse beginning). The iterator points to past-the-end element of the vector. If the container is empty, this function returns the same as vector::cbegin. 4 | 5 | **Example**: 6 | ```cpp 7 | // Demonstrates cend() 8 | #include 9 | #include 10 | 11 | nt main () 12 | { 13 | std::vector myvector = {10,20,30,40,50}; 14 | 15 | std::cout << "myvector contains:"; 16 | 17 | for (auto it = myvector.cbegin(); it != myvector.cend(); ++it) 18 | std::cout << ' ' << *it; 19 | std::cout << '\n'; 20 | 21 | return 0; 22 | } 23 | ``` 24 | **[Run Code](https://rextester.com/HOY41620)** 25 | 26 | -------------------------------------------------------------------------------- /algorithm/nth_element.md: -------------------------------------------------------------------------------- 1 | # nth_element 2 | 3 | **Description** :Is an STL algorithm which rearranges the list in such a way such that the element at the nth position is the one which should be at that position if we sort the list. 4 | 5 | **Example**: 6 | ```cpp 7 | // Creating array of size 8 8 | int v[] = { 3, 2, 10, 45, 33, 56, 23, 47 }, i; 9 | 10 | // Using std::nth_element with n as 5 11 | std::nth_element(v, v + 4, v + 8); 12 | 13 | // Since, n is 5 so 5th element should be sorted 14 | for (i = 0; i < 8; ++i) { 15 | cout << v[i] << " "; 16 | } 17 | return 0; 18 | 19 | ``` 20 | **[See Sample code](../snippets/algorithm/nth_element.cpp)** 21 | **[Run Code](https://rextester.com/FJMW84431)** -------------------------------------------------------------------------------- /snippets/algorithm/__gcd.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Nishanth Sanjeev 3 | Date : 07/10/2019 4 | Time : 21:45 5 | Description : Finding gcd of two numbers, with only a function call 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | int num1 = 18; 13 | int num2 = 4; 14 | 15 | // An example of finding gcd() of these two numbers. 16 | // The answer returned should be 2, as 2 is the gcd of 18 and 4. 17 | 18 | std::cout << "GCD of num1 and num2 = " << __gcd(num1,num2) << endl; 19 | 20 | /* 21 | As can be seen, we do not have to write Euclid's algorithm to find the gcd of two numbers. 22 | A simple function call works wonders! 23 | */ 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /snippets/vector/crbegin.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Jayshree Aishwarya 3 | Date : 18/10/2019 4 | Time : 22:20 5 | Description : It returns a const_reverse_iterator pointing to the last element in the container (i.e., its reverse beginning). 6 | */ 7 | 8 | #include 9 | #include 10 | //Demonstrating the use of crbegin() 11 | 12 | int main () 13 | { 14 | // the vector stores b'days in October among the students in a class 15 | std::vector vbday = {2, 7, 10, 16, 19, 20, 20, 21, 23, 26, 31}; 16 | 17 | std::cout << "Birthdays are on : " << std::endl; 18 | for (auto i = vbday.crbegin() ; i != vbday.crend(); ++i) { 19 | std::cout << "October " << *i; 20 | std::cout << std::endl; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /algorithm/transform.md: -------------------------------------------------------------------------------- 1 | # transform 2 | 3 | **Description** :This function can add arrays in single line. 4 | 5 | **Example**: 6 | ```cpp 7 | int arr1[] = {1, 2, 3}; // Creating array of size 3 8 | int arr2[] = {4, 5, 6}; // Creating array of size 3 9 | int n = sizeof(arr1)/sizeof(arr1[0]); 10 | int res[n]; 11 | 12 | /* 13 | Applies op to each of the elements and stores the value returned by each operation in the range that begins at result. 14 | */ 15 | transform(arr1, arr1+n, arr2, res, plus()); 16 | 17 | // print result 18 | for (int i=0; i 8 | #include 9 | using namespace std; 10 | int main(){ 11 | std::vector v = {1,1,3,3,5,5,5,1,2,2,8,9,9,9,0,8}; 12 | 13 | std::vector::iterator itr; 14 | itr = std::unique(v.begin(),v.end()); 15 | 16 | v.resize(std::distance(v.begin(),itr)); 17 | 18 | for(itr = v.begin();itr!=v.end();++itr){ 19 | cout<<" "<<*itr; 20 | } 21 | 22 | ``` 23 | **[Run Code](https://rextester.com/GNHPSK20621)** 24 | -------------------------------------------------------------------------------- /snippets/algorithm/unique_copy.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Eduardo Magalhaes 3 | Date : 09/10/2020 4 | Time : 14:24 5 | Description : Copies the elements in the range [first,last) to the range beginning at result, except consecutive duplicates (elements that compare equal to the element preceding). 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | int main() { 14 | std::string s1 = "The string with many spaces!"; 15 | std::cout << "before: " << s1 << '\n'; 16 | 17 | std::string s2; 18 | std::unique_copy(s1.begin(), s1.end(), std::back_inserter(s2), 19 | [](char c1, char c2){ return c1 == ' ' && c2 == ' '; }); 20 | 21 | std::cout << "after: " << s2 << '\n'; 22 | } -------------------------------------------------------------------------------- /algorithm/generate.md: -------------------------------------------------------------------------------- 1 | # generate 2 | **Description** : Used to generate numbers based upon a generator function, and then, it assigns those values to the elements in the container in the range [first, last). 3 | The generator function has to be defined by the user, and it is called successively for assigning the numbers. 4 | 5 | **Example**: 6 | ```cpp 7 | int gen() { 8 | static int i = 0; 9 | return ++i; 10 | } 11 | 12 | int main() { 13 | int i; 14 | std::vector v1(10); 15 | 16 | std::generate(v1.begin(), v1.end(), gen); 17 | 18 | for (auto value : v1) { 19 | std::cout << value << " "; 20 | } 21 | 22 | return 0; 23 | } 24 | ``` 25 | **[Run Code](https://rextester.com/UVF27948)** 26 | -------------------------------------------------------------------------------- /algorithm/generate_n.md: -------------------------------------------------------------------------------- 1 | # generate_n 2 | 3 | **Description:** 4 | Assigns values to the first n elements in a given sequence. Values are determined by a passed function. 5 | 6 | 7 | **Example:** 8 | 9 | ```cpp 10 | 11 | //function to generate values 12 | int value = 0; 13 | int assignNumber() { 14 | return ++value * value; 15 | } 16 | 17 | int main() { 18 | 19 | //initialize empty array 20 | int newArray[10]; 21 | 22 | //call method to fill array 23 | std::generate_n(newArray, 10, assignNumber); 24 | 25 | //print filled array 26 | std::cout << "The array values are:"; 27 | for (int i = 0; i < 10; ++i){ 28 | std::cout << " " << newArray[i]; 29 | } 30 | 31 | return 0; 32 | } 33 | ``` 34 | 35 | **[Run Code](https://rextester.com/VDUH77387)** 36 | -------------------------------------------------------------------------------- /snippets/algorithm/remove_if.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 09/09/2019 4 | Time : Time format 23:00 5 | Description : Removes elements from vector that satisfies a criteria. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | auto isOdd = [](int i) { 14 | return ((i%2) == 1); 15 | }; 16 | 17 | std::vector v {1, 2, 3, 4, 5}; 18 | 19 | // Remove all elements that returns true for isOdd 20 | auto newEndIt = std::remove_if(v.begin(), v.end(), isOdd); 21 | 22 | // Erase elements from [newEndIt, v.end()] 23 | v.erase(newEndIt, v.end()); 24 | 25 | // v is now {2, 4} 26 | for (auto value : v) { 27 | std::cout << value << " "; 28 | } 29 | } -------------------------------------------------------------------------------- /snippets/unordered_map/count.cpp: -------------------------------------------------------------------------------- 1 | //Demonstrate count() 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | // unordered map 10 | unordered_map umap; 11 | 12 | // Inserting elements into the map 13 | umap.insert(make_pair("car" , 1)); 14 | umap.insert(make_pair("truck" , 2)); 15 | umap.insert(make_pair("bike" , 1)); 16 | umap.insert(make_pair("auto" , 3)); 17 | umap.insert(make_pair("scooty" ,2)); 18 | 19 | // Check if element with key car is present using 20 | // count() function 21 | if(umap.count("car")) 22 | { 23 | cout<<"Element Found"< vector1{60, 70, 80, 90}; 10 | std:: iter_swap(array1, vector1.begin()); //array1: [60] 20 30 40 50 11 | //vector1: [10] 70 80 90 12 | 13 | std::iter_swap(array1 + 3, vector1.begin() + 2); //array1: 60 20 30 [80] 50 14 | //vector1: 10 70 [40] 90 15 | 16 | std::cout<<"vector1 contains:"<<'\n'; 17 | for(std::vector ::iterator it = vector1.begin(); it < vector1.end(); ++it) 18 | { 19 | std::cout<<*it<<" "; 20 | } 21 | 22 | std::cout<<'\n'; 23 | ``` 24 | 25 | **[Run Code](https://rextester.com/WXQUT76012)** 26 | 27 | --- 28 | -------------------------------------------------------------------------------- /algorithm/search.md: -------------------------------------------------------------------------------- 1 | # search 2 | 3 | **Description** : Searches the range `[first1,last1)` for the first occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found. 4 | 5 | **Example**: 6 | ```cpp 7 | 8 | std::vector haystack {10, 20, 30, 40, 50, 60, 70, 80, 90}; 9 | int needle1[] = {40,50,60,70}; 10 | std::vector::iterator it; 11 | it = std::search (haystack.begin(), haystack.end(), needle1, needle1+4); 12 | if (it!=haystack.end()){ 13 | std::cout << "needle1 found at position " << (it-haystack.begin()) << '\n'; 14 | } 15 | else{ 16 | std::cout << "needle1 not found\n"; 17 | } 18 | 19 | ``` 20 | **[See Sample code](../snippets/algorithm/search.md)** 21 | **[Run Code](https://rextester.com/)** 22 | -------------------------------------------------------------------------------- /map/begin.md: -------------------------------------------------------------------------------- 1 | # begin 2 | 3 | **Description :** This function is used to return an iterator pointing to the first element of the map container. begin() function returns a bidirectional iterator to the first element of the container. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | // Demonstrates begin() 9 | #include 10 | #include 11 | 12 | int main() { 13 | // declaration of map container 14 | std::map mymap; 15 | mymap['a'] = 1; 16 | mymap['b'] = 2; 17 | mymap['c'] = 3; 18 | 19 | // using begin() to print map 20 | for (auto it = mymap.begin(); it != mymap.end(); ++it) 21 | std::cout << it->first << " = " 22 | << it->second << '\n'; 23 | return 0; 24 | } 25 | ``` 26 | **[Run Code](https://rextester.com/AUMIKR77967)** -------------------------------------------------------------------------------- /snippets/algorithm/copy_if.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 02:00 5 | Description : Copies the elements in one range to another range if it matches a condition. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | auto isOdd = [](int i) { 14 | return ((i%2) == 1); 15 | }; 16 | 17 | std::vector origin {1, 2, 3}; 18 | std::vector destination; 19 | 20 | // Will copy from origin [begin, end), to destination 21 | std::copy_if(origin.begin(), origin.end(), std::back_inserter(destination), isOdd); 22 | 23 | // destination is now {1, 3} 24 | for (auto value : destination) { 25 | std::cout << value << " "; 26 | } 27 | } -------------------------------------------------------------------------------- /snippets/algorithm/fill.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Italo Vinicius 3 | Date : 01/10/2019 4 | Time : 22:00 5 | Description : Can fill in specific positions a specific number. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() { 13 | std::vector v(10); 14 | 15 | //Lets print what we have in the vector 16 | for(int i = 0;i<10;i++){ 17 | std::cout<< v[i] << " "; 18 | } 19 | std::cout<< "\n"; 20 | 21 | //But i want to put number 8 five times starting in the fourth position 22 | std::fill(v.begin()+3, v.end() - 1, 8); 23 | 24 | //Print the changes in the vector 25 | 26 | for(int i = 0;i<10;i++){ 27 | std::cout<< v[i] << " "; 28 | } 29 | std::cout<< "\n"; 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /snippets/vector/shrink_to_fit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Y. Sai Sriram 3 | Date : 29/09/2019 4 | Time : 11:09 5 | Description : Reduces the capacity of the container to fit its size. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main(){ 12 | //create a vector of 5 elements 13 | std::vector myvec{10, 20, 30, 40, 50}; 14 | 15 | //adding a sixth element, vector's capacity will increase for future insertions 16 | myvec.push_back(60); 17 | 18 | //output the inital capacity 19 | std::cout << "Initial capacity is: " << myvec.capacity(); 20 | std::cout << std::endl; 21 | 22 | //output the shrunk capacity 23 | myvec.shrink_to_fit(); 24 | 25 | std::cout << "Shrunk capacity is: " << myvec.capacity(); 26 | std::cout << std::endl; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /snippets/algorithm/count.cpp: -------------------------------------------------------------------------------- 1 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 2 | /* 3 | Author : Bhupesh Varshney 4 | Date : 19/02/2019 5 | Time : 5:10 PM 6 | Description : number of elements in the range `[first, last)` satisfying specific criteria. 7 | */ 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | std::vector v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 }; 14 | 15 | int target = 3; //Value to find 16 | // determines how many integers in a C++ vector matches a particular value. 17 | 18 | int num_items = std::count(v.begin(), v.end(), target); //returns count of target 19 | std::cout << "Number: " << target << " count: " << num_items; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /snippets/algorithm/fill_n.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Klajdi Bodurri 3 | Date : 08/01/2020 4 | Time : 14:00 5 | Description : Show an example of fill_n 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | void print_vector(const std::vector &v) { 13 | // iterate through v and print its elements 14 | for(auto &elem:v) { 15 | std::cout << elem << " "; 16 | } 17 | std::cout< v = {1,1,1,1,1,1,1,1}; 22 | 23 | std::cout << "Before fill_n: "; 24 | print_vector(v); 25 | 26 | // set the first half of v's elements to zero 27 | std::fill_n(v.begin(), v.size()/2, 0); 28 | 29 | std::cout << "After fill_n: "; 30 | print_vector(v); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: Bug 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Compiler (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Compiler Version [e.g. gcc 8.2] 29 | 30 | **Additional context** 31 | Add any other context about the problem here. 32 | -------------------------------------------------------------------------------- /algorithm/copy_if.md: -------------------------------------------------------------------------------- 1 | # copy_if 2 | 3 | **Description** : Copies the elements in the range, defined by `[first, last)`, to another range beginning at passed argument, if the value satisfy specific criteria. 4 | 5 | **Example**: 6 | ```cpp 7 | auto isOdd = [](int i) { 8 | return ((i%2) == 1); 9 | }; 10 | 11 | std::vector origin {1, 2, 3}; 12 | std::vector destination; 13 | 14 | // Will copy from origin [begin, end), to destination 15 | std::copy_if(origin.begin(), origin.end(), std::back_inserter(destination), isOdd); 16 | 17 | // destination is now {1, 3} 18 | for (auto value : destination) { 19 | std::cout << value << " "; 20 | } 21 | ``` 22 | **[See Sample code](../snippets/algorithm/copy_if.cpp)** 23 | **[Run Code](https://rextester.com/OMC23438)** -------------------------------------------------------------------------------- /map/count.md: -------------------------------------------------------------------------------- 1 | # count 2 | 3 | **Description :** The map::count() is a function which returns 1 if the element with key K is present in the map container. It returns 0 if the element with key K is not present in the container. 4 | Syntax: map_name.count(key k); 5 | 6 | **Example** : 7 | 8 | ```cpp 9 | // Demonstrates count() 10 | #include 11 | #include 12 | 13 | int main() { 14 | // declaration of map container 15 | std::map mymap; 16 | mymap['a'] = 1; 17 | mymap['b'] = 2; 18 | mymap['c'] = 3; 19 | 20 | // using count() function 21 | std::cout< 11 | #include 12 | using namespace std; 13 | 14 | int main() { 15 | // declaration of map container and inserting elements 16 | std::map mymap; 17 | mymap.insert({'a',1}); 18 | mymap.insert({'b',2}); 19 | mymap.insert({'c',3}); 20 | 21 | // printing the contents of the map 22 | for (auto key2Value : mymap) { 23 | std::cout << key2Value.first << " = " << key2Value.second << '\n'; 24 | } 25 | return 0; 26 | } 27 | ``` 28 | **[Run Code](https://rextester.com/SSANI89056)** 29 | -------------------------------------------------------------------------------- /set/swap.md: -------------------------------------------------------------------------------- 1 | # swap 2 | 3 | **Description** :This function can swap the content of two sets of same type 4 | 5 | **Example**: 6 | ```cpp 7 | // Creating rwo sets of same type and of size 3 8 | std::set A{6,10,21}; 9 | std::set B{11,34,56}; 10 | 11 | // I want to swap set A with set B 12 | A.swap(B); 13 | 14 | // Print the content in set A 15 | for(std::set::iterator i=A.begin(); i!=A.end(); ++i){ 16 | std::cout << ' ' << *i; 17 | } 18 | std::cout << '\n'; 19 | 20 | // Print the content in set B 21 | for(std::set::iterator i=B.begin(); i!=B.end(); ++i){ 22 | std::cout << ' ' << *i; 23 | } 24 | std::cout << '\n'; 25 | ``` 26 | 27 | **[See Sample code](../snippets/set/swap.cpp)** 28 | **[Run Code](https://rextester.com/WPZJS63867)** -------------------------------------------------------------------------------- /snippets/algorithm/copy_backward.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 02:00 5 | Description : Copies elements from range to another range ending in argument. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | std::vector origin {1, 2, 3}; 14 | // destination size is required to be at least the number of values to be copied 15 | std::vector destination(origin.size()); 16 | 17 | // Copy origin to destination, starting from the last element 18 | std::copy_backward(origin.begin(), origin.end(), destination.end()); 19 | 20 | // destination is now {1, 2, 3} 21 | for (auto value : destination) { 22 | std::cout << value << " "; 23 | } 24 | } -------------------------------------------------------------------------------- /algorithm/remove_if.md: -------------------------------------------------------------------------------- 1 | # remove_if 2 | 3 | **Description** : Removes elementes from range `[first, last)` that satisfy a condition and returns a new past-the-end iterator for the new end of range. 4 | 5 | **Example**: 6 | ```cpp 7 | auto isOdd = [](int i) { 8 | return ((i%2) == 1); 9 | }; 10 | 11 | std::vector v {1, 2, 3, 4, 5}; 12 | 13 | // Remove all elements that returns true for isOdd 14 | auto newEndIt = std::remove_if(v.begin(), v.end(), isOdd); 15 | 16 | // Erase elements from [newEndIt, v.end()] 17 | v.erase(newEndIt, v.end()); 18 | 19 | // v is now {2, 4} 20 | for (auto value : v) { 21 | std::cout << value << " "; 22 | } 23 | ``` 24 | **[See Sample code](../snippets/algorithm/remove_if.cpp)** 25 | **[Run Code](https://rextester.com/OWAN21750)** -------------------------------------------------------------------------------- /snippets/algorithm/search.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Luca Palumbo 3 | Date : Date format 07/10/2019 4 | Time : Time format 23:00 5 | Description : Search the position of a subarray in a vector. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | int main(){ 12 | // defining two vector 13 | std::vector haystack { 10, 20, 30, 40, 50, 60, 70, 80, 90 }; 14 | int needle1[] = { 40, 50, 60, 70 }; 15 | std::vector::iterator it; 16 | //Will find needle1 in haystack 17 | it = std::search (haystack.begin(), haystack.end(), needle1, needle1+4); 18 | //Print if needle1 was found or not 19 | if (it != haystack.end()){ 20 | std::cout << "needle1 found at position " << (it - haystack.begin() ) << '\n'; 21 | } 22 | else{ 23 | std::cout << "needle1 not found\n"; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /snippets/queue/back.cpp: -------------------------------------------------------------------------------- 1 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 2 | /* 3 | Author : Timothy Itodo 4 | Date : 28/05/2019 5 | Time : 1:00 PM 6 | Description : back() function is used to reference the last element (i.e the newest element) of the queue. This function can be used to fetch the last element of a queue. 7 | */ 8 | #include 9 | #include 10 | 11 | int main(){ 12 | // Empty queue 13 | std::queue myqueue; 14 | 15 | // Add elements to queue using push() 16 | myqueue.push(2); 17 | myqueue.push(5); 18 | myqueue.push(4); 19 | myqueue.push(1); 20 | 21 | // Queue becomes 2, 5, 4, 1 22 | 23 | // Print the last/newest element in the queue 24 | std::cout << myqueue.back(); 25 | } 26 | -------------------------------------------------------------------------------- /list/max_size.md: -------------------------------------------------------------------------------- 1 | # max_size 2 | 3 | **Description** : The list::max_size() is a built-in function in C++ STL which returns the maximum number of elements a list container can hold. 4 | 5 | **Example** : 6 | ```cpp 7 | // CPP program to illustrate the 8 | // list::max_size() function 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | // Creating a list 14 | std::list demoList; 15 | 16 | // checking the max size of the list 17 | std::cout << demoList.max_size(); 18 | 19 | return 0; 20 | } 21 | 22 | //Output is 768614336404564650, it is the number of elements a list container can hold. 23 | ``` 24 | **[See Sample Code](snippets/algorithm/max_size.cpp)** 25 | **[Run Code](https://rextester.com/QRAJ69116)** 26 | -------------------------------------------------------------------------------- /queue/pop.md: -------------------------------------------------------------------------------- 1 | # pop 2 | 3 | **Description** : pop() function is used to remove an element from the front of the queue (ie. the oldest element in the queue). The element is removed from the queue container and the size of the queue is decreased by 1. 4 | 5 | **Example**: 6 | ```cpp 7 | // Empty queue 8 | std::queue myqueue; 9 | 10 | // pushing elements into queue using push() 11 | myqueue.push(0); 12 | myqueue.push(1); 13 | myqueue.push(2); 14 | 15 | // pop items from queue 16 | myqueue.pop(); // pops 0 from queue 17 | myqueue.pop(); // pops 1 from queue 18 | 19 | // print contents of queue 20 | while (!myqueue.empty()) { 21 | std::cout << ' ' << myqueue.front(); 22 | myqueue.pop(); 23 | } 24 | ``` 25 | **[Run Code](https://rextester.com/XACN77371)** 26 | -------------------------------------------------------------------------------- /snippets/queue/front.cpp: -------------------------------------------------------------------------------- 1 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 2 | /* 3 | Author : Timothy Itodo 4 | Date : 28/05/2019 5 | Time : 1:00 PM 6 | Description : front() function is used to reference the first element (ie. the oldest element) of the queue. This function can be used to fetch the first element of a queue. 7 | */ 8 | #include 9 | #include 10 | 11 | int main(){ 12 | // Empty queue 13 | std::queue myqueue; 14 | 15 | // Add elements to queue using push() 16 | myqueue.push(2); 17 | myqueue.push(5); 18 | myqueue.push(4); 19 | myqueue.push(1); 20 | 21 | // Queue becomes 2, 5, 4, 1 22 | 23 | // Print the first/oldest element in the queue 24 | std::cout << myqueue.front(); 25 | } 26 | -------------------------------------------------------------------------------- /snippets/set/crbegin.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : Cameron Bradley 5 | Date : 11/10/2020 6 | Time : 12:32 7 | Description : Get a const reverse iterator pointing at the last element of the container 8 | */ 9 | #include 10 | #include 11 | 12 | int main() { 13 | // Create a set object holding integers 14 | std::set mySet{ 3, 1, 5, 2, 4 }; 15 | 16 | // Get a const_reverse_iterator pointing to the last element 17 | std::set::reverse_iterator r_it = mySet.crbegin(); 18 | 19 | // Iterate through the const_reverse_iterator 20 | for (; r_it != mySet.crend(); ++r_it) { 21 | std::cout << *r_it << std::endl; 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /snippets/queue/push.cpp: -------------------------------------------------------------------------------- 1 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 2 | /* 3 | Author : Rickey Patel 4 | Date : 23/05/2019 5 | Time : 03:50 PM 6 | Description : push() function is used to insert an element at the back of the queue. The element is added to the queue container and the size of the queue is increased by 1. 7 | */ 8 | #include 9 | #include 10 | 11 | int main(){ 12 | // Empty queue 13 | std::queue myqueue; 14 | 15 | // pushing elements into queue using push() 16 | myqueue.push(0); 17 | myqueue.push(1); 18 | myqueue.push(2); 19 | 20 | // print contents of queue 21 | while (!myqueue.empty()){ 22 | std::cout << ' ' << myqueue.front(); 23 | myqueue.pop(); 24 | } 25 | } -------------------------------------------------------------------------------- /list/emplace.md: -------------------------------------------------------------------------------- 1 | # emplace 2 | 3 | **Description** : This function is used to extend list by inserting new element at a given position. 4 | 5 | **Example** : 6 | ```cpp 7 | // declaration of list 8 | std::list< std::pair > mylist; 9 | 10 | // using emplace() function to 11 | // insert new element at the 12 | // beginning of the list 13 | mylist.emplace ( mylist.begin(), 100, 'x' ); 14 | mylist.emplace ( mylist.begin(), 200, 'y' ); 15 | 16 | // printing the list 17 | // after inserting the value 18 | // at the beginning 19 | std::cout << "mylist contains:"; 20 | for (auto& x: mylist) 21 | std::cout << " (" << x.first << "," << x.second << ")"; 22 | 23 | std::cout << '\n'; 24 | } 25 | 26 | ``` 27 | 28 | **[Run Code](https://rextester.com/VEBHW94625)** 29 | -------------------------------------------------------------------------------- /map/contains.md: -------------------------------------------------------------------------------- 1 | # contains 2 | 3 | **Description :** Checks whether there is an element with a specified key in the container. Returns true if there is such an element, otherwise false. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | /* Should print: 9 | 2 found. 10 | 1 not found. */ 11 | std::map example = {{2, 3}, {4, 5}}; 12 | 13 | if (example.contains(2)) { 14 | std::cout << "2 found." << std::endl; 15 | } else { 16 | std::cout << "2 not found." << std::endl; 17 | } 18 | 19 | if (example.contains(1)) { 20 | std::cout << "1 found." << std::endl; 21 | } else { 22 | std::cout << "1 not found." << std::endl; 23 | } 24 | 25 | ``` 26 | **[See Sample code](../snippets/vector/contains.cpp)**
27 | **[Run Code](https://wandbox.org/permlink/JhoFq4AxExrtctY1)** 28 | -------------------------------------------------------------------------------- /snippets/algorithm/generate_n.md: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Sarah Leon 3 | Date : Date format 06/03/2020 4 | Time : Time format 15:48 5 | Description : Assigns values to the first n elements in a given sequence. Values are determined by a passed function. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | 12 | //function to generate values 13 | int value = 0; 14 | int assignNumber() { 15 | return ++value * value; 16 | } 17 | 18 | int main() { 19 | 20 | //initialize empty array 21 | int newArray[10]; 22 | 23 | //call method to fill array 24 | std::generate_n(newArray, 10, assignNumber); 25 | 26 | //print filled array 27 | std::cout << "The array values are:"; 28 | for (int i = 0; i < 10; ++i){ 29 | std::cout << " " << newArray[i]; 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /algorithm/shuffle.md: -------------------------------------------------------------------------------- 1 | # shuffle 2 | 3 | **Description** :This function can rearranges the elements in the range randomly 4 | 5 | **Example**: 6 | ```cpp 7 | // Creating array of size 5 8 | std::array random {1,2,3,4,5}; // 1,2,3,4,5 9 | 10 | // Obtain a time-based seed: 11 | unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); 12 | 13 | // Rearranges the elements in the range [first,last) randomly 14 | shuffle (random.begin(), random.end(), std::default_random_engine(seed)); 15 | 16 | std::cout << "Shuffle:"; 17 | 18 | // Print the content in array random 19 | for (int& x: random) std::cout << ' ' << x; 20 | std::cout << '\n'; 21 | ``` 22 | **[See Sample code](../snippets/algorithm/shuffle.cpp)** 23 | **[Run Code](https://rextester.com/NNI79837)** 24 | -------------------------------------------------------------------------------- /algorithm/find_first_of.md: -------------------------------------------------------------------------------- 1 | # find_first_of 2 | 3 | **Description** : Return iterator to the first element in the range [first, last) that is equal to an element from the range [s_first; s_last). If no such element is found, last is returned.(Searches the range [first, last) for any of the elements in the range [s_first, s_last) ). 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector v{0, 2, 3, 25, 5}; 8 | std::vector t{3, 19, 10, 2}; 9 | 10 | auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end()); 11 | 12 | if (result == v.end()) { 13 | std::cout << "no elements of v were equal to 3, 19, 10 or 2\n"; 14 | } else { 15 | std::cout << "found a match at " 16 | << std::distance(v.begin(), result) << "\n"; 17 | } 18 | ``` 19 | **[Run Code](https://rextester.com/YPXP50467)** 20 | -------------------------------------------------------------------------------- /algorithm/adjacent_difference.md: -------------------------------------------------------------------------------- 1 | # adjacent_difference 2 | 3 | **Description** : Finds the difference of an iterator with the one preceding it in the range from first to the second parameter. The values are stored in third parameter, which is returned. The first pointer of the returned iterator simply stores the value pointed by first parameter. A fourth parameter may contain the function which defines any custom operation on the pair. 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector v {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; 8 | std::vector r; 9 | std::adjacent_difference(v.begin(), v.end(), r.begin()); 10 | 11 | for (auto n : r) 12 | std::cout << n << ' '; 13 | 14 | std::cout << '\n'; 15 | 16 | ``` 17 | **[See Sample code](../snippets/algorithm/adjacent_difference.cpp)** 18 | **[Run Code](https://rextester.com/MUMXYS97563)** 19 | -------------------------------------------------------------------------------- /algorithm/replace_if.md: -------------------------------------------------------------------------------- 1 | # replace_if 2 | 3 | **Description** : Replaces all elements satisfying condition with a `new_value` in the range `[first, last)`. 4 | 5 | **Example**: 6 | ```cpp 7 | auto isOdd = [](int i) { 8 | return ((i%2) == 1); 9 | }; 10 | 11 | std::vector origin {1, 2, 3, 4, 5}; 12 | 13 | // replaces values for which isOdd returns true by 0 14 | std::replace(origin.begin(), //first 15 | origin.end(), //last 16 | isOdd, //condition 17 | 0 //new_value 18 | ); 19 | 20 | // origin is now {0, 2, 0, 4, 0} 21 | for (auto value : origin) { 22 | std::cout << value << " "; 23 | } 24 | ``` 25 | **[See Sample code](../snippets/algorithm/replace_if.cpp)** 26 | **[Run Code](https://rextester.com/AWLX69783)** -------------------------------------------------------------------------------- /list/empty.md: -------------------------------------------------------------------------------- 1 | # empty 2 | 3 | **Description :** The list::empty() is a built-in function in C++ STL is used to check whether a particular list container is empty or not. 4 | 5 | **Example** : 6 | ```cpp 7 | // Creating a list 8 | std::list demoList; 9 | 10 | // check if list is empty 11 | if (demoList.empty()) 12 | std::cout << "Empty List\n"; 13 | else 14 | std::cout << "Not Empty\n"; 15 | 16 | // Add elements to the List 17 | demoList.push_back(10); 18 | demoList.push_back(20); 19 | demoList.push_back(30); 20 | demoList.push_back(40); 21 | 22 | // check again if list is empty 23 | if (demoList.empty()) 24 | std::cout << "Empty List\n"; 25 | else 26 | std::cout << "Not Empty\n"; 27 | 28 | ``` 29 | **[Run Code](https://rextester.com/QKU59237)** -------------------------------------------------------------------------------- /snippets/algorithm/equal_range.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 23:00 5 | Description : Retrieve smallest element that is not smaller than given value. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() { 13 | std::vector vec = {1, 2, 3, 1, 6, 1}; 14 | // equal_range requires a sorted input {1, 1, 1, 2, 3, 6} 15 | std::sort(vec.begin(), vec.end()); 16 | 17 | // returns pair of iterators for positions 0 and 2: 18 | // {1, 1, 1, 2, 3, 6} 19 | // ^ ^ 20 | auto equalRangeIt = std::equal_range(vec.begin(), vec.end(), 1); 21 | for (auto it = equalRangeIt.first; it != equalRangeIt.second; ++it) { 22 | std::cout << " Position: " << (it - vec.begin()) << " = " << *it << std::endl; 23 | } 24 | } -------------------------------------------------------------------------------- /algorithm/move.md: -------------------------------------------------------------------------------- 1 | # move 2 | 3 | **Description**: Moves the element to the vector permanently. The value is lost after the process. 4 | 5 | **Example**: 6 | ```cpp 7 | std::string str1 = "Hello"; 8 | std::string str2 = "Word"; 9 | 10 | std::vector avector; 11 | 12 | avector.push_back (str1); // copies the first string 13 | avector.push_back (std::move(str2)); // moves the second string 14 | 15 | std::cout << "Printing the vector: "; 16 | for (std::string& x:avector) { 17 | std::cout << ' ' << x; 18 | } 19 | std::cout << '\n'; 20 | 21 | std::cout << "First string after copy: " 22 | << str1 << std::endl; 23 | std::cout << "Second string after move: " 24 | << str2 << std::endl; 25 | ``` 26 | **[Run Code](https://rextester.com/ENU74789)** -------------------------------------------------------------------------------- /algorithm/transform_reduce.md: -------------------------------------------------------------------------------- 1 | # transform_reduce 2 | 3 | **Description :** Applies a functor, then reduces. The default functor is multiplication. Available in C++17 and above. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | int main() 9 | { 10 | std::vector arr{1, 2, 3, 4, 5}; 11 | int result; 12 | 13 | // the functor here squares each element 14 | // addition is used to reduce 15 | result = std17::transform_reduce(arr.begin(), 16 | arr.end() , 17 | 0 , 18 | [](auto a, auto b) {return a + b;}, 19 | [](auto a ) {return a * a;}); 20 | // finds sum of squares of arr 21 | std::cout << result << std::endl; 22 | return 0; 23 | } 24 | ``` 25 | **[Run Code](https://rextester.com/YPMMS86271)** 26 | -------------------------------------------------------------------------------- /snippets/vector/get_allocator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 8 4 | 5 | int main() { 6 | std::vector arr; 7 | 8 | // Allocate space for SIZE elements 9 | char *ptr = arr.get_allocator().allocate(SIZE); 10 | 11 | // Construct values in-place on the vector 12 | // The values are the characters 'a', 'b', ... 13 | for (int i = 0; i < SIZE; i++) { 14 | arr.get_allocator().construct(ptr + i, i + (int)'a'); 15 | } 16 | 17 | // Display the vector 18 | for (int i = 0; i < SIZE; i++) { 19 | std::cout << ptr[i] << " "; 20 | } 21 | std::cout << std::endl; 22 | 23 | // Deallocate and free the memory 24 | for (int i = 0; i < SIZE; i++) { 25 | arr.get_allocator().destroy(ptr + i); 26 | } 27 | arr.get_allocator().deallocate(ptr, SIZE); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /algorithm/reverse_copy.md: -------------------------------------------------------------------------------- 1 | # reverse_copy 2 | 3 | **Description**: Copies the elements from a given range but in reverse order. 4 | 5 | **Example**: 6 | ```cpp 7 | int main(){ 8 | int n = 5; 9 | std::vector myvector; 10 | for(int i = 0; i < n; ++i){ 11 | myvector.push_back(i); 12 | std::cout << myvector[i] << " "; 13 | } //myvector contains: 0 1 2 3 4 14 | std::cout << std::endl; 15 | 16 | std::vector temp(n); //temp contains: 0 0 0 0 0 17 | std::reverse_copy(std::begin(myvector), std::end(myvector), std::begin(temp)); 18 | for(int i = 0; i < n; ++i){ 19 | std::cout << temp[i] << " "; 20 | } //temp now contains: 4 3 2 1 0 21 | std::cout << std::endl; 22 | } 23 | ``` 24 | 25 | **[Run Code](https://rextester.com/OLG11049)** 26 | -------------------------------------------------------------------------------- /snippets/queue/emplace.cpp: -------------------------------------------------------------------------------- 1 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 2 | /* 3 | Author : Timothy Itodo 4 | Date : 30/05/2019 5 | Time : 01:00 PM 6 | Description : emplace() function is used to add a new element at the end of the queue, after its current last element. The element is added to the queue container and the size of the queue is increased by 1. 7 | */ 8 | #include 9 | #include 10 | 11 | int main(){ 12 | // Empty queue 13 | std::queue myqueue; 14 | 15 | // adding elements into queue using emplace() 16 | myqueue.emplace(0); 17 | myqueue.emplace(1); 18 | myqueue.emplace(2); 19 | 20 | // print contents of queue 21 | while (!myqueue.empty()){ 22 | std::cout << ' ' << myqueue.front(); 23 | myqueue.pop(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /snippets/algorithm/random_shuffle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Ajay Makwana 3 | Date : Date format 12/10/2019 4 | Time : Time format 12:46 AM 5 | Description : Randomly Shuffle Element of user defined vector. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int main() { 14 | srand(unsigned(time(0))); 15 | vector arr; 16 | 17 | // set some values: 18 | for (int j = 1; j < 10; ++j) { 19 | // 1 2 3 4 5 6 7 8 9 20 | arr.push_back(j); 21 | } 22 | 23 | // using built-in random generator 24 | random_shuffle(arr.begin(), arr.end()); 25 | 26 | // print out content: 27 | cout << "arr contains:"; 28 | for (auto i = arr.begin(); i != arr.end(); ++i) { 29 | cout << ' ' << *i; 30 | } 31 | 32 | cout << endl; 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /map/size.md: -------------------------------------------------------------------------------- 1 | # size 2 | 3 | **Description** : size() function returns the number of elements in a map 4 | 5 | **Example**: 6 | ```cpp 7 | // creates a map 8 | std::map asciiMap; 9 | 10 | // adds four elements in the map 11 | asciiMap['A'] = 65; 12 | asciiMap['B'] = 66; 13 | asciiMap['C'] = 67; 14 | asciiMap['D'] = 68; 15 | 16 | // prints the size of the map 17 | std::cout << "Size of map (asciiMap.size()): " << asciiMap.size() << std::endl; 18 | 19 | // adds two elements in the map 20 | asciiMap['E'] = 69; 21 | asciiMap['F'] = 70; 22 | 23 | std::cout << "\nAfter adding two more elements" << std::endl; 24 | std::cout << "Size of map (asciiMap.size()): " << asciiMap.size() << std::endl; 25 | ``` 26 | **[See Sample Code](../snippets/map/size.cpp)**
27 | **[Run Code](https://rextester.com/LSR13900)** 28 | -------------------------------------------------------------------------------- /snippets/algorithm/replace_if.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 09/09/2019 4 | Time : Time format 23:00 5 | Description : Replace elements from vector by a new value. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | auto isOdd = [](int i) { 14 | return ((i%2) == 1); 15 | }; 16 | 17 | std::vector origin {1, 2, 3, 4, 5}; 18 | 19 | // replaces values for which isOdd returns true by 0 20 | std::replace_if(origin.begin(), //first 21 | origin.end(), //last 22 | isOdd, //condition 23 | 0 //new_value 24 | ); 25 | 26 | // origin is now {0, 2, 0, 4, 0} 27 | for (auto value : origin) { 28 | std::cout << value << " "; 29 | } 30 | } -------------------------------------------------------------------------------- /snippets/vector/sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Italo Vinicius 3 | Date : 01/10/2019 4 | Time : 21:00 5 | Description : Returns a ordered vector in increasing or decreasing order. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() { 13 | // create a vector of 10 integers 14 | std::vector v {-1,0,4,-5,12,2,10,-11,3,5}; 15 | 16 | // display the vector in incresing order with sort; 17 | 18 | std::sort(v.begin(), v.end()); 19 | 20 | for (int i = 0 ; i < 10; i++) { 21 | std::cout << v[i] << " "; 22 | } 23 | std::cout << std::endl; 24 | 25 | // display the vector in decresing order with sort; 26 | 27 | sort(v.begin(), v.end(), std::greater()); 28 | 29 | for (int i = 0 ; i < 10; i++) { 30 | std::cout << v[i] << " "; 31 | } 32 | std::cout << std::endl; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /vector/empty.md: -------------------------------------------------------------------------------- 1 | # empty 2 | 3 | **Description :** The vector::empty() is a built-in function in C++ STL is used to check whether a particular vector container is empty or not. 4 | 5 | **Example** : 6 | ```cpp 7 | // Creating a vector 8 | std::vector example; 9 | 10 | // check if vector is empty 11 | if (example.empty()) 12 | std::cout << "Empty Vector\n"; 13 | else 14 | std::cout << "Not Empty\n"; 15 | 16 | // Add elements to the Vector 17 | example.push_back(10); 18 | example.push_back(20); 19 | example.push_back(30); 20 | example.push_back(40); 21 | 22 | // check again if vector is empty 23 | if (example.empty()) 24 | std::cout << "Empty Vector\n"; 25 | else 26 | std::cout << "Not Empty\n"; 27 | 28 | ``` 29 | **[Run Code](https://rextester.com/ZMFKZ74225)** 30 | -------------------------------------------------------------------------------- /set/crbegin.md: -------------------------------------------------------------------------------- 1 | # crbegin 2 | 3 | **Description :** 4 | Get a const_reverse_iterator pointing at the last element of the container. This is useful 5 | when wanting a read-only iterator to iterate a container in reverse order. 6 | 7 | **Example :** 8 | ```cpp 9 | //Run Code To Demonstrate use of set.size() 10 | #include 11 | #include 12 | 13 | int main() { 14 | // Create a set object holding integers 15 | std::set mySet{ 3, 1, 5, 2, 4 }; 16 | 17 | // Get a const_reverse_iterator pointing to the last element 18 | std::set::reverse_iterator r_it = mySet.crbegin(); 19 | 20 | // Iterate through the const_reverse_iterator 21 | for (; r_it != mySet.crend(); ++r_it){ 22 | std::cout << *r_it << std::endl; 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | ``` 29 | 30 | **[See Sample code](../snippets/set/crbegin.cpp)** 31 | **[Run Code](https://ideone.com/ccq58H)** -------------------------------------------------------------------------------- /set/emplace_hint.md: -------------------------------------------------------------------------------- 1 | # emplace_hint 2 | 3 | **Description :** 4 | This method is used to insert new elements in set, if unique, with a hint on the insertion position. If the new element is inserted into the set with this function, an iterator to the new element in the set will be returned 5 | 6 | **Example :** 7 | ```cpp 8 | //Run Code To Demonstrate use of set.emplace_hint() 9 | #include 10 | #include 11 | 12 | int main(){ 13 | std::set myset; 14 | auto it = myset.begin(); 15 | it = myset.emplace_hint (it,"red"); 16 | it = myset.emplace_hint (it,"orange"); 17 | it = myset.emplace_hint (it,"yellow"); 18 | 19 | std::cout << "myset contains:"; 20 | for (const std::string& mystring: myset) 21 | std::cout << ' ' << mystring; 22 | std::cout << '\n'; 23 | 24 | return 0; 25 | } 26 | 27 | ``` 28 | 29 | **[Run Code](https://ideone.com/HSukoG)** -------------------------------------------------------------------------------- /algorithm/equal_range.md: -------------------------------------------------------------------------------- 1 | # equal_range 2 | 3 | **Description** : Returns the bounds of the subrange that includes all the elements of the range `[first, last)` with values equivalent to passed value. 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector vec = {1, 2, 3, 1, 6, 1}; 8 | // equal_range requires a sorted input {1, 1, 1, 2, 3, 6} 9 | std::sort(vec.begin(), vec.end()); 10 | 11 | // returns pair of iterators for positions 0 and 2: 12 | // {1, 1, 1, 2, 3, 6} 13 | // ^ ^ 14 | auto equalRangeIt = std::equal_range(vec.begin(), vec.end(), 1); 15 | for (auto it = equalRangeIt.first; it != equalRangeIt.second; ++it) { 16 | std::cout << " Position: " << (it - vec.begin()) << " = " << *it << std::endl; 17 | } 18 | 19 | ``` 20 | **[See Sample code](../snippets/algorithm/equal_range.cpp)** 21 | **[Run Code](https://rextester.com/ZYGGE30271)** 22 | -------------------------------------------------------------------------------- /list/swap.md: -------------------------------------------------------------------------------- 1 | # swap 2 | 3 | **Description** : This function is used to swap the contents of one list with another list of same type and size. 4 | 5 | **Example**: 6 | ```cpp 7 | // list container declaration 8 | std::list mylist1{ 1, 2, 3, 4 }; 9 | std::list mylist2{ 3, 5, 7, 9 }; 10 | 11 | // using swap() function to 12 | //swap elements of lists 13 | mylist1.swap(mylist2); 14 | 15 | // printing the first list 16 | std::cout << "mylist1 = "; 17 | for (auto value : mylist1) { 18 | std::cout << ' ' << value; 19 | } 20 | 21 | // printing the second list 22 | std::cout << std::endl << "mylist2 = "; 23 | for (auto value : mylist2) { 24 | std::cout << ' ' << value; 25 | } 26 | 27 | ``` 28 | 29 | **[See Sample Code](../snippets/list/swap.cpp)** 30 | **[Run Code](https://rextester.com/YBG44941)** 31 | -------------------------------------------------------------------------------- /map/equal_range.md: -------------------------------------------------------------------------------- 1 | # equal_range 2 | **Description :** Returns the bounds of a range that includes all the elements in the container which have a key equivalent to k. 3 | 4 | **Example** : 5 | 6 | ```cpp 7 | // Demonstrates equal_range 8 | 9 | #include 10 | #include 11 | 12 | int main (){ 13 | std::map mymap; 14 | 15 | mymap['a']=10; 16 | mymap['b']=20; 17 | mymap['c']=30; 18 | 19 | std::pair::iterator,std::map::iterator> ret; 20 | ret = mymap.equal_range('b'); 21 | 22 | std::cout << "lower bound points to: "; 23 | std::cout << ret.first->first << " => " << ret.first->second << '\n'; 24 | 25 | std::cout << "upper bound points to: "; 26 | std::cout << ret.second->first << " => " << ret.second->second << '\n'; 27 | 28 | return 0; 29 | } 30 | 31 | ``` 32 | **[Run Code](https://rextester.com/PDL21133)** 33 | -------------------------------------------------------------------------------- /algorithm/copy_backward.md: -------------------------------------------------------------------------------- 1 | # copy_backward 2 | 3 | **Description** : Copies elements from range defined by `[first, last)`, to another range ending at passed iterator. 4 | Elements are copied in reverse order (last element is copied first), but the relative order is kept. 5 | 6 | **Example**: 7 | ```cpp 8 | std::vector origin {1, 2, 3}; 9 | // destination size is required to be at least the number of values to be copied 10 | std::vector destination(origin.size()); 11 | 12 | // Copy origin to destination, starting from the last element 13 | std::copy_backward(origin.begin(), origin.end(), destination.end()); 14 | 15 | // destination is now {1, 2, 3} 16 | for (auto value : destination) { 17 | std::cout << value << " "; 18 | } 19 | ``` 20 | **[See Sample code](../snippets/algorithm/copy_backward.cpp)** 21 | **[Run Code](https://rextester.com/INZK42877)** -------------------------------------------------------------------------------- /map/clear.md: -------------------------------------------------------------------------------- 1 | # clear 2 | 3 | **Description :** This function removes all elements from the map container (which are destroyed), leaving the container with a size of 0. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | map m; 14 | //Suppose we add some entries in the map,example 15 | m['a'] = 14; 16 | m['d'] = 23; 17 | m['s'] = 16; 18 | m['f'] = 10; 19 | cout<<"This is the initial size of map : "< 10 | #include 11 | #include 12 | 13 | int main() 14 | { 15 | int n=3; //move 3 smallest elements to the beginning 16 | std::vector v = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; 17 | 18 | std::partial_sort(v.begin(), v.begin() + n, v.end()); 19 | for (int a : v) { 20 | std::cout << a << " "; 21 | } 22 | } 23 | 24 | ``` 25 | **[See Sample code](../snippets/algorithm/partial_sort.cpp)** 26 | **[Run Code](https://rextester.com/GLP60885)** -------------------------------------------------------------------------------- /snippets/vector/crend.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Rishabh Arora 3 | Date : 14/10/2019 4 | Time : 21:42 5 | Description : The function returns a constant iterator to the reverse end of the sequence. 6 | 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main(){ 13 | //Declaring a vector 14 | std::vector vectorSample; 15 | 16 | //Inititializing the vector 17 | vectorSample.push_back(10); 18 | vectorSample.push_back(20); 19 | vectorSample.push_back(30); 20 | vectorSample.push_back(40); 21 | vectorSample.push_back(50); 22 | 23 | //Printing the vector using crend() to point to the 24 | //beginning of the vector 25 | std::cout << "Contents of the vector:" << std::endl; 26 | for (auto itr = vectorSample.crend();itr >= vectorSample.crbegin(); --itr){ 27 | std::cout << *itr << std::endl; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /algorithm/equal.md: -------------------------------------------------------------------------------- 1 | # equal 2 | 3 | **Description** : Compares the elements of a vector in a given range to the elements of another vector in the previously defined range. 4 | 5 | **Example** : 6 | ```cpp 7 | std::vector v1 {3, 5, 3, 1, 2, 3}; 8 | std::vector v2 {1, 2 ,3, 4, 5, 6}; 9 | std::vector v3 {3, 5, 3, 1, 2, 3}; 10 | 11 | // Compare two equal vectors 12 | if(std::equal(v1.begin(),v1.end(),v3)){ 13 | std::cout << "Vectors are equa!"; 14 | } 15 | else { 16 | std::cout << "Vectors are not equal"; 17 | } 18 | 19 | // Compare two unequal vectors 20 | if(std::equal(v1.begin(),v1.end(),v2)){ 21 | std::cout << "Vectors are equa!"; 22 | } 23 | else { 24 | std::cout << "Vectors are not equal"; 25 | } 26 | ``` 27 | **[See Sample Code](snippets/algorithm/equal.cpp)** 28 | **[Run Code](https://rextester.com/RXRNU59365)** 29 | -------------------------------------------------------------------------------- /algorithm/is_heap.md: -------------------------------------------------------------------------------- 1 | # is_heap 2 | 3 | **Description :** The C++ function `std::is_heap` returns `true` if the elements in the range `[first, last)` form a _max heap_, such as is constructed by _make_heap_, and `false` otherwise. 4 | 5 | **Example :** 6 | 7 | ```cpp 8 | #include 9 | #include 10 | #include 11 | 12 | int main() 13 | { 14 | std::vector v { 8, 6, 7, 5, 3, 0, 9 }; 15 | 16 | std::cout << "Intial value for v: "; 17 | for (auto i : v) std::cout << i << ' '; 18 | std::cout << '\n'; 19 | 20 | if (!std::is_heap(v.begin(), v.end())) { 21 | std::cout << "Creating heap:\n"; 22 | std::make_heap(v.begin(), v.end()); 23 | } 24 | 25 | std::cout << "After call to make_heap, v: "; 26 | for (auto i : v) std::cout << i << ' '; 27 | std::cout << '\n'; 28 | } 29 | ``` 30 | 31 | **[Run Code](https://rextester.com/CWLO88991)** 32 | -------------------------------------------------------------------------------- /snippets/algorithm/equal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author: Michael Guzman 3 | Date: 10/16/2019 4 | Time: 19:43 5 | Description: Compares the elements of two vectors in the given range. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() { 13 | std::vector v1 {3, 5, 3, 1, 2, 3}; 14 | std::vector v2 {1, 2 ,3, 4, 5, 6}; 15 | std::vector v3 {3, 5, 3, 1, 2, 3}; 16 | 17 | // Compare two equal vectors 18 | if(std::equal(v1.begin(),v1.end(),v3.begin())){ 19 | std::cout << "Vectors are equal!\n"; 20 | } 21 | else { 22 | std::cout << "Vectors are not equal!\n"; 23 | } 24 | 25 | // Compare two unequal vectors 26 | if(std::equal(v1.begin(),v1.end(),v2.begin())){ 27 | std::cout << "Vectors are equal!\n"; 28 | } 29 | else { 30 | std::cout << "Vectors are not equal!\n"; 31 | } 32 | } -------------------------------------------------------------------------------- /snippets/list/swap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Y. Sai Sriram 3 | Date : 29/09/2019 4 | Time : 11:26 5 | Description : This function is used to swap the contents of one list with another list of same type and size. 6 | 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main(){ 13 | // list container declaration 14 | std::list mylist1{1, 2, 3, 4}; 15 | std::list mylist2{3, 5, 7, 9}; 16 | 17 | //using swap() function to swap elements of lists 18 | mylist1.swap(mylist2); 19 | 20 | //printing the first list 21 | std::cout << "list1 after swapping: "; 22 | for(auto value : mylist1){ 23 | std::cout << " " << value; 24 | } 25 | std::cout << std::endl; 26 | 27 | //printing the second list 28 | std::cout << "list2 after swapping: "; 29 | for(auto value : mylist2){ 30 | std::cout << " " << value; 31 | } 32 | std::cout << std::endl; 33 | 34 | return 0; 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /set/max_size.md: -------------------------------------------------------------------------------- 1 | # max_size 2 | 3 | **Description :** 4 | This method is used to find out the maximum number of elements that can be stored in a set. It returns a numerical value of that max potnetial size. It is still possible for failure to insert elements to occur sometime before reaching this max_size however. 5 | 6 | **Example :** 7 | ```cpp 8 | //Run Code To Demonstrate use of set.max_size() 9 | #include 10 | #include 11 | 12 | int main () 13 | { 14 | int i; 15 | std::set myset; 16 | 17 | if (myset.max_size() > 5) 18 | { 19 | myset.insert(1); 20 | myset.insert(2); 21 | myset.insert(3); 22 | myset.insert(4); 23 | myset.insert(5); 24 | std::cout << "The set can hold " << myset.max_size() << " elements.\n"; 25 | std::cout << "The set currently holds 5 elements.\n"; 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | ``` 32 | 33 | **[Run Code](https://ideone.com/1jSnv4)** -------------------------------------------------------------------------------- /snippets/map/insert.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : Luis Malicay 5 | Date : 06/06/2019 6 | Time : 12:43 PM 7 | Description : Demonstrate the insert key value pair of STL map 8 | */ 9 | #include 10 | #include 11 | #include 12 | 13 | int main(){ 14 | // Create container and insert values 15 | std::map grades; 16 | grades.insert({"John", 98}); 17 | grades.insert({"Joe", 95}); 18 | grades.insert({"Sally", 97}); 19 | grades.insert({"Jane", 96}); 20 | 21 | // Print contents of map 22 | std::cout << "Key\tValue\n"; 23 | for(auto i = grades.begin(); i != grades.end(); ++i){ 24 | std::cout << i->first << '\t' 25 | << i->second << '\n'; 26 | } 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /unordered_map/size.md: -------------------------------------------------------------------------------- 1 | # size 2 | 3 | **Description :** This function is used to return size of an unordered map. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | // Demonstrates size() 9 | #include 10 | #include 11 | 12 | int main(){ 13 | //declares an empty map. O(1) 14 | std::unordered_map mymap; 15 | 16 | //print size of mymap before inserting key and value in unordered_map 17 | std::cout << "mymap size is = " < 11 | #include 12 | #include 13 | 14 | int main() { 15 | std::vector array{ 1, 3, 2, 4, 5, 8, 9, 2 }; 16 | 17 | // Partition the array in an even and odd pair 18 | stable_partition(array.begin(), array.end(), [](int val) { 19 | return val % 2 == 0; 20 | }); 21 | 22 | for (auto val : array) { 23 | std::cout << val << " "; 24 | } 25 | 26 | std::cout << '\n'; 27 | 28 | return 0; 29 | } 30 | ``` 31 | 32 | 33 | **[Run Code](https://ideone.com/JFOxNU)** 34 | -------------------------------------------------------------------------------- /snippets/map/size.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Kabindra Shrestha 3 | Date : Date format 25/10/2019 4 | Time : Time format 12:28 5 | Description : returns the size of the map, i.e. the number of elements in 6 | the map 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | // Prints the current size of the map 13 | void printSize(std::map &asciiMap){ 14 | std::cout << "Size of map (asciiMap.size()): " << asciiMap.size() << std::endl; 15 | } 16 | 17 | int main(){ 18 | std::map asciiMap; 19 | // creates a map 20 | 21 | // adds four elements in the map 22 | asciiMap['A'] = 65; 23 | asciiMap['B'] = 66; 24 | asciiMap['C'] = 67; 25 | asciiMap['D'] = 68; 26 | 27 | printSize(asciiMap); 28 | 29 | // adds two elements in the map 30 | asciiMap['E'] = 69; 31 | asciiMap['F'] = 70; 32 | 33 | std::cout << "\nAfter adding two more elements" << std::endl; 34 | printSize(asciiMap); 35 | } -------------------------------------------------------------------------------- /algorithm/is_sorted_until.md: -------------------------------------------------------------------------------- 1 | # is_sorted_until 2 | 3 | **Description :** std::is_sorted_until is used to find out the first unsorted element in the range [first, last). It returns an iterator to the first unsorted element in the range, so all the elements in between first and the iterator returned are sorted.

4 | 5 | 6 | **Example** : 7 | 8 | ```cpp 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | int main() 14 | { 15 | int v[] = { 1, 2, 3, 4, 7, 10, 8, 9 }, i; 16 | 17 | int* ip; 18 | 19 | // Using std::is_sorted_until 20 | ip = std::is_sorted_until(v, v + 8); 21 | 22 | cout << "There are " << (ip - v) << " sorted elements in " 23 | << "the list and the first unsorted element is " << *ip; 24 | return 0; 25 | } 26 | ``` 27 | **[Run Code](https://rextester.com/NLRDYA45690)** 28 | -------------------------------------------------------------------------------- /list/splice.md: -------------------------------------------------------------------------------- 1 | # splice 2 | 3 | **Description :** The list::splice() is a built-in function in C++ STL which is used to transfer elements from one list to another 4 | 5 | **Example** : 6 | ```cpp 7 | // initializing lists 8 | std::list l1 = { 1, 2, 3 }; 9 | std::list l2 = { 4, 5 }; 10 | std::list l3 = { 6, 7, 8 }; 11 | 12 | // transfer all the elements of l2 13 | l1.splice(l1.begin(), l2); 14 | 15 | // at the beginning of l1 16 | std::cout << "list l1 after splice operation" << " "; 17 | for (auto x : l1) { 18 | std::cout << x << " "; 19 | } 20 | 21 | // transfer all the elements of l1 22 | l3.splice(l3.begin(), l1); 23 | 24 | // at the end of l3 25 | std::cout << "\nlist l3 after splice operation" << " "; 26 | for (auto x : l3) { 27 | std::cout << x << " "; 28 | } 29 | 30 | ``` 31 | **[Run Code](https://rextester.com/WNBD26175)** -------------------------------------------------------------------------------- /snippets/algorithm/merge.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 02/09/2019 4 | Time : Time format 02:00 5 | Description : Merge two ranges into a new destination. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | std::vector v1 {8, 2, 0, 4}; 14 | std::vector v2 {7, 3, 5}; 15 | // the destination needs to fit all merged values 16 | std::vector destination(v1.size() + v2.size()); 17 | 18 | // merge requires sorted ranges 19 | std::sort(v1.begin(), v1.end()); 20 | std::sort(v2.begin(), v2.end()); 21 | 22 | std::merge(v1.begin(), v1.end(), // first range 23 | v2.begin(), v2.end(), // second range 24 | destination.begin()); 25 | 26 | // destination is now {0, 2, 3, 4, 5, 7, 8} 27 | for (auto value : destination) { 28 | std::cout << value << " "; 29 | } 30 | } -------------------------------------------------------------------------------- /snippets/vector/cbegin.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Spencer Koss 3 | Date : 10/01/2019 4 | Time : 5:11 5 | Description : The function returns an iterator which is used to iterate container. 6 | 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main() 13 | { 14 | //Create a vector 15 | std::vector vectorExample; 16 | 17 | //5 integer values are stored 18 | vectorExample.push_back(1); 19 | vectorExample.push_back(2); 20 | vectorExample.push_back(3); 21 | vectorExample.push_back(4); 22 | vectorExample.push_back(5); 23 | 24 | //Displays the contents of the vector using c.begin() to point to the 25 | //beginning of the vector 26 | std::cout << "Contents of the vector:" << std::endl; 27 | for (auto itr = vectorExample.cbegin(); 28 | itr != vectorExample.end(); ++itr) 29 | { 30 | std::cout << *itr << std::endl; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /map/rbegin.md: -------------------------------------------------------------------------------- 1 | # rbegin 2 | 3 | **Description :** It returns a reverse iterator which points to the last element of the map. The reverse iterator iterates in reverse order and incrementing it means moving towards beginning of map. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | // C++ Program to illustrate map::rbegin() method 9 | #include 10 | #include 11 | 12 | int main() { 13 | 14 | std::map mp = { 15 | { 'a', 1 }, 16 | { 'b', 2 }, 17 | { 'c', 3 }, 18 | { 'd', 4 }, 19 | { 'e', 5 }, 20 | }; 21 | 22 | std::cout << "Map contains following elements in reverse order"<< std::endl; 23 | //rbegin 24 | for (auto i = mp.rbegin(); i != mp.rend(); ++i) 25 | std::cout << i->first 26 | << " = " << i->second 27 | << std::endl; 28 | 29 | return 0; 30 | } 31 | ``` 32 | 33 | **[Run Code](https://rextester.com/EFAJ1452)** 34 | -------------------------------------------------------------------------------- /snippets/algorithm/nth_element.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : Ian Fillipe Pontes Ferreira 5 | Date : 06/10/2019 6 | Time : 20:25 7 | Description : Is an STL algorithm which rearranges the list in such a way such that the element at the nth position is the one which should be at that position if we sort the list. 8 | */ 9 | #include 10 | #include 11 | using namespace std; 12 | int main(){ 13 | int v[] = { 3, 2, 10, 45, 33, 56, 23, 47 }, i; // 3, 2, 10, 45, 33, 56, 23, 47 14 | 15 | // Using std::nth_element with n as 5 16 | std::nth_element(v, v + 4, v + 8); 17 | 18 | // Since, n is 5 so 5th element should be sorted 19 | for (i = 0; i < 8; ++i) { 20 | cout << v[i] << " "; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /snippets/vector/emplace_back.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Natália A. de Brito 3 | Date : 20/10/2019 4 | Time : 21:19 5 | Description : Constructs and places an element at the end of the vector 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | // Simple example struct 12 | struct triangle { 13 | double base, height; 14 | triangle(double b, double h) : base(b), height(h) {} 15 | }; 16 | 17 | void print(const std::vector& v){ 18 | std::cout << "triangles: {" << std::endl; 19 | for (auto t: v){ 20 | std::cout << "\tbase: " << t.base; 21 | std::cout << "\theight: " << t.height << std::endl; 22 | } 23 | std::cout << '}' << std::endl; 24 | } 25 | 26 | int main(){ 27 | //New vector with 2 triangles 28 | std::vector v{triangle(1.1,1.2), triangle(2.1,2.2)}; 29 | 30 | v.emplace_back(3.1,3.2); //Passing arguments used to build a triangle 31 | 32 | //Prints restulting vector 33 | print(v); 34 | } -------------------------------------------------------------------------------- /algorithm/merge.md: -------------------------------------------------------------------------------- 1 | # merge 2 | 3 | **Description** : Combines elements from sorted ranges `[first1, last1)` and `[first2, last2)`, to a new range starting at passed argument. 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector v1 {8, 2, 0, 4}; 8 | std::vector v2 {7, 3, 5}; 9 | // the destination needs to fit all merged values 10 | std::vector destination(v1.size() + v2.size()); 11 | 12 | // merge requires sorted ranges 13 | std::sort(v1.begin(), v1.end()); 14 | std::sort(v2.begin(), v2.end()); 15 | 16 | std::merge(v1.begin(), v1.end(), // first range 17 | v2.begin(), v2.end(), // second range 18 | destination.begin()); 19 | 20 | // destination is now {0, 2, 3, 4, 5, 7, 8} 21 | for (auto value : destination) { 22 | std::cout << value << " "; 23 | } 24 | ``` 25 | **[See Sample code](../snippets/algorithm/merge.cpp)** 26 | **[Run Code](https://rextester.com/TPXUS57604)** -------------------------------------------------------------------------------- /algorithm/minimax_element.md: -------------------------------------------------------------------------------- 1 | # minimax_element 2 | 3 | **Description** :This function returns a pair with an iterator pointing to the element with the smallest value in the range [first,last) as first element, and the largest as second. 4 | 5 | **Example**: 6 | ```cpp 7 | // Creating array of size 7 8 | std::array foo {3,7,2,9,5,8,6}; // 3,7,2,9,5,8,6 9 | 10 | // smallest value in the range [first,last) as first element, and the largest as second 11 | auto result = std::minmax_element (foo.begin(),foo.end()); 12 | 13 | // print result: 14 | std::cout << "min is " << *result.first; 15 | std::cout << ", at position " << (result.first-foo.begin()) << '\n'; 16 | std::cout << "max is " << *result.second; 17 | std::cout << ", at position " << (result.second-foo.begin()) << '\n'; 18 | ``` 19 | **[See Sample code](../snippets/algorithm/minimax_element.cpp)** 20 | **[Run Code](https://rextester.com/ZNGV69767)** -------------------------------------------------------------------------------- /snippets/algorithm/find_end.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Derek Hornacek 3 | Date : 16/03/2020 4 | Time : 05:24pm 5 | Description : This is a code snippet demonstrating the find_end algorithm from the C++ STL. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | std::vector sequence{ 13, 5, 2, }; 14 | std::vector target{ 18, 11, 7, 13, 5, 2, 12, 5, 13, 5, 2 }; 15 | 16 | // find the first element of the last occurrence of the given sequence 17 | std::vector::iterator result = std::find_end(target.begin(), target.end(), sequence.begin(), sequence.end()); 18 | 19 | if(result != target.end()){ 20 | // list wasn't empty and sequence occurred 21 | std::cout << "The result is: " << std::distance(target.begin(), result) << "." << std::endl; 22 | } else{ 23 | std::cout << "No such pattern found or lists are empty." << std::endl; 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /algorithm/minmax_element.md: -------------------------------------------------------------------------------- 1 | # minmax_element 2 | 3 | **Description** : Returns a pair with an iterator pointing to the element with the smallest 4 | value in the range as first element, and the largest as second. 5 | 6 | **Example**: 7 | ```cpp 8 | std::array myarray {3,2,1,5,4}; // define myarray for manipulation 9 | 10 | auto result = std::minmax_element (myarray.begin(),myarray.end()); 11 | 12 | std::cout << "min is " << *result.first; // returns the min element from the pair 13 | std::cout << ", at position " << (result.first-myarray.begin()) << '\n'; // returns the position of the min element 14 | std::cout << "max is " << *result.second; // returns the max element from the pair 15 | std::cout << ", at position " << (result.second-myarray.begin()) << '\n'; // returns the position of the max element 16 | 17 | ``` 18 | **[See Sample code](../snippets/algorithm/minmax_element.cpp)** 19 | **[Run Code](https://rextester.com/WZHNI30832)** -------------------------------------------------------------------------------- /snippets/vector/emplace.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Y. Sai Sriram 3 | Date : 29/09/2019 4 | Time : 10:53 5 | Description : Places an element in the vector at the specified position 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | void print(const std::vector &vec){ 12 | for(auto num : vec){ 13 | std::cout << num << " "; 14 | } 15 | } 16 | 17 | int main(){ 18 | //creating a vector of 5 elements 19 | std::vector vector1{10, 20, 30, 40, 50}; 20 | 21 | std::cout << "Initial vector: "; 22 | print(vector1); 23 | std::cout << std::endl; 24 | 25 | //add an element at the beginning 26 | vector1.emplace(vector1.begin(), -10); 27 | 28 | //add an element at the end 29 | vector1.emplace(vector1.end(), 60); 30 | 31 | //add an element after 2 elements 32 | vector1.emplace(vector1.begin() + 2, 15); 33 | 34 | std::cout << "Modified vector: "; 35 | print(vector1); 36 | std::cout << std::endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /priority_queue/pop.md: -------------------------------------------------------------------------------- 1 | # pop 2 | 3 | **Description** : removes the first(largest) value in the priority_queue and reorders it accordingly to contain the second largest as the first value 4 | 5 | **Example**: 6 | ``` cpp 7 | 8 | std::priority_queue my; 9 | my.push(2); 10 | my.push(10); 11 | my.push(4); 12 | 13 | // returns the first value of the queue(largest) 14 | std::cout << my.top() << "\n"; 15 | 16 | // inserts 20 as it is now the largest it comes to the first position 17 | my.push(20); 18 | 19 | std::cout << my.top() << "\n"; 20 | 21 | // removes the largest value i.e 20 22 | my.pop(); 23 | 24 | // largest is 10 now as 20 is removed 25 | std::cout << my.top() << "\n"; 26 | 27 | // removes largest values i.e 10 28 | my.pop(); 29 | 30 | // largest is 4 now 31 | std::cout << my.top(); 32 | 33 | ``` 34 | 35 | **[Run Code](https://rextester.com/XWWB77826)** 36 | 37 | 38 | -------------------------------------------------------------------------------- /vector/clear.md: -------------------------------------------------------------------------------- 1 | # clear 2 | 3 | **Description**: Remove all current elements of the vector, reducing its size to 0. 4 | 5 | **Example**: 6 | ```cpp 7 | std::vector myVector; 8 | 9 | // Initialize vector with the values {1,2,3,4,5} 10 | myVector = {1,2,3,4,5}; 11 | 12 | // Output: "1 2 3 4 5" 13 | for (int i : myVector) { 14 | std::cout << i << " "; 15 | } 16 | std::cout << std::endl; 17 | 18 | // Size of vector is 5 19 | std::cout << "Vector size: " << myVector.size() << std::endl; 20 | 21 | // Clear the vector of all values, myVector now contains no values 22 | myVector.clear(); 23 | 24 | // Output is blank 25 | for (int i : myVector) { 26 | std::cout << i << " "; 27 | } 28 | std::cout << std::endl; 29 | 30 | // Size of vector is 0 31 | std::cout << "Vector size: " << myVector.size() << std::endl; 32 | ``` 33 | **[Run Code](https://rextester.com/IPP45757)** 34 | -------------------------------------------------------------------------------- /set/find.md: -------------------------------------------------------------------------------- 1 | # find 2 | 3 | **Description :** 4 | This method is used to know if a value already exists in a set. If the value exists in the set, it returns an iterator to it, otherwise it returns an iterator to set::end. 5 | 6 | **Example :** 7 | 8 | ```cpp 9 | //Run Code To Demonstrate use of set.find() 10 | #include 11 | #include 12 | int main() { 13 | // Create a set object holding integers 14 | std::set mySet {1, 2, 3, 4, -5}; 15 | if (mySet.find(1) != mySet.end()) { 16 | std::cout << "1 : is a element of mySet" << std::endl; 17 | } else { 18 | std::cout << "1 : is not an element of myset" << std::endl; 19 | } 20 | if (mySet.find(12) != mySet.end()) { 21 | std::cout << "12 : is a element of mySet" << std::endl; 22 | } else { 23 | std::cout << "12 : is not an element of myset" << std::endl; 24 | } 25 | return 0; 26 | } 27 | ``` 28 | 29 | **[Run Code](https://ideone.com/BkboDS)** 30 | -------------------------------------------------------------------------------- /snippets/algorithm/transform.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : Ian Fillipe Pontes Ferreira 5 | Date : 06/10/2019 6 | Time : 20:41 7 | Description : Using transform function of STL, we can add arrays in single line. 8 | */ 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int main(){ 14 | int arr1[] = {1, 2, 3}; // 1,2,3 15 | int arr2[] = {4, 5, 6}; // 4,5,6 16 | int n = sizeof(arr1)/sizeof(arr1[0]); 17 | int res[n]; 18 | 19 | /* 20 | Applies op to each of the elements and stores the value returned by each operation in the range that begins at result. 21 | */ 22 | transform(arr1, arr1+n, arr2, res, plus()); 23 | 24 | // print result 25 | for (int i=0; i 10 | #include 11 | 12 | int main (){ 13 | // declaration of list 14 | std::list< std::pair > mylist; 15 | 16 | // using emplace() function to 17 | // insert new element at the 18 | // beginning of the list 19 | mylist.emplace ( mylist.begin(), 100, 'x' ); 20 | mylist.emplace ( mylist.begin(), 200, 'y' ); 21 | 22 | // printing the list 23 | // after inserting the value 24 | // at the beginning 25 | std::cout << "mylist contains:"; 26 | for (auto& x: mylist) 27 | std::cout << " (" << x.first << "," << x.second << ")"; 28 | 29 | std::cout << '\n'; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /snippets/list/pop_back.cpp: -------------------------------------------------------------------------------- 1 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 2 | 3 | /* 4 | Author : Nicole Spoto 5 | Date : 10/10/2019 6 | Time : 01:30 PM 7 | Description : pop_back() function is used to remove the last element in the list container and the size of the list is decreased by 1. 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | int main(){ 14 | // list container declaration 15 | std::list mylist{5, 4, 3, 2, 1}; 16 | 17 | //printing the list 18 | std::cout << "list after initializing: "; 19 | for(auto value : mylist){ 20 | std::cout << " " << value; 21 | } 22 | std::cout << std::endl; 23 | 24 | // pop items from list 25 | mylist.pop_back(); 26 | 27 | // print the list after removing last element 28 | std::cout << "list after popping: "; 29 | for(auto value : mylist){ 30 | std::cout << " " << value; 31 | } 32 | std::cout << std::endl; 33 | } -------------------------------------------------------------------------------- /snippets/vector/capacity.cpp: -------------------------------------------------------------------------------- 1 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 2 | /* 3 | Author : Y. Sai Sriram 4 | Date : 28/09/2019 5 | Time : 10:17 6 | Description : Returns the space currently allocated to the vector 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main() 13 | { 14 | // create a vector of 10 integers 15 | std::vector myvector{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; 16 | 17 | // display the capacity of vector, because no value has been pushed, it returns the size of vector 18 | std::cout << "The capacity of the vector is: " << myvector.capacity(); 19 | myvector.push_back(110); 20 | std::cout << std::endl; 21 | // notice how capacity returns a value greater than 11... it stores extra memory for further allocation 22 | std::cout << "The capacity of the vector is: " << myvector.capacity(); 23 | std::cout << std::endl; 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /set/count.md: -------------------------------------------------------------------------------- 1 | # count 2 | 3 | **Description :** 4 | This method is used to know if value exits in a set. It returns `1` if the value is a element of set, otherwise `0`. It is mostly used in conditional statements. 5 | 6 | **Example :** 7 | ```cpp 8 | //Run Code To Demonstrate use of set.count() 9 | #include 10 | #include 11 | 12 | int main() { 13 | // Create a set object holding integers 14 | std::set mySet {1,2,3,4,-5}; 15 | 16 | if (mySet.count(1) != 0) { 17 | std::cout << "1 : is not an element of myset" << std::endl; 18 | } 19 | else { 20 | std::cout << "1 : is a element of mySet" << std::endl; 21 | } 22 | if (mySet.count(12) != 0) { 23 | std::cout << "12 : is not an element of myset" << std::endl; 24 | } 25 | else { 26 | std::cout << "12 : is a element of mySet" << std::endl; 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | ``` 33 | 34 | **[Run Code](https://ideone.com/1F5j3h)** 35 | -------------------------------------------------------------------------------- /snippets/set/swap.cpp: -------------------------------------------------------------------------------- 1 | // Follow the Style Guide while submitting code PR. 2 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 3 | /* 4 | Author : Italo Vinicius 5 | Date : 02/10/2019 6 | Time : 21:09 7 | Description : Swap the content of two sets of same type 8 | */ 9 | #include 10 | #include 11 | #include 12 | 13 | int main(){ 14 | std::set A{21,10,6}; // 6,10,21 15 | std::set B{56,11,34}; // 11,34,56 16 | 17 | // I want to swap set A with set B 18 | A.swap(B); 19 | 20 | // Print the content in set A 21 | for(std::set::iterator i=A.begin(); i!=A.end(); ++i){ 22 | std::cout << ' ' << *i; 23 | } 24 | std::cout << '\n'; 25 | 26 | // Print the content in set B 27 | for(std::set::iterator i=B.begin(); i!=B.end(); ++i){ 28 | std::cout << ' ' << *i; 29 | } 30 | std::cout << '\n'; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /algorithm/sort_heap.md: -------------------------------------------------------------------------------- 1 | # sort_heap 2 | 3 | **Description** : Is a function that sorts a heap within the range specified by start and end. 4 | 5 | **Example**: 6 | 7 | ```cpp 8 | #include 9 | #include 10 | #include 11 | 12 | int main() 13 | { 14 | std::vector v = {8, 6, 2, 1, 5, 10}; 15 | 16 | // make_heap transform a vector to a heap 17 | std::make_heap(v.begin(), v.end()); 18 | 19 | std::cout << "Heap: "; 20 | for (const auto &i : v) { 21 | std::cout << i << ' '; 22 | } 23 | 24 | std::sort_heap(v.begin(), v.end()); 25 | 26 | std::cout < 15 | #include 16 | 17 | template 18 | const T& clamp(const T& value, const T& minValue, const T& maxValue) { 19 | return std::min(maxValue, std::max(value, minValue)); 20 | } 21 | 22 | int main(void) 23 | { 24 | for (int i = 0; i < 20; i++) 25 | { 26 | std::cout << clamp(i, 0, 10) << std::endl; 27 | } 28 | 29 | system("pause"); 30 | return 0; 31 | } 32 | ``` 33 | **[Run Code](https://ideone.com/mYXmTt)** 34 | -------------------------------------------------------------------------------- /list/remove.md: -------------------------------------------------------------------------------- 1 | # remove 2 | 3 | **Description** :The list::remove() is a built-in function in C++ STL which is used to remove elements from a list container. 4 | 5 | **Example**: 6 | ```cpp 7 | // Creating a list 8 | std::list demoList; 9 | 10 | // Add elements to the List 11 | demoList.push_back(10); 12 | demoList.push_back(20); 13 | demoList.push_back(20); 14 | demoList.push_back(30); 15 | demoList.push_back(40); 16 | 17 | // List before removing elements 18 | std::cout << "List before removing elements: "; 19 | for (auto value : demoList) { 20 | std::cout << value << " "; 21 | } 22 | 23 | // delete all elements with value 20 24 | demoList.remove(20); 25 | 26 | // List after removing elements 27 | std::cout << "\nList after removing elements: "; 28 | for (auto value : demoList) { 29 | std::cout << value << " "; 30 | } 31 | 32 | ``` 33 | **[Run Code](https://rextester.com/KDDH67495)** -------------------------------------------------------------------------------- /snippets/queue/pop.cpp: -------------------------------------------------------------------------------- 1 | // Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide 2 | /* 3 | Author : Rickey Patel 4 | Date : 23/05/2019 5 | Time : 03:50 PM 6 | Description : pop() function is used to remove an element from the front of the queue (ie. the oldest element in the queue). The element is removed from the queue container and the size of the queue is decreased by 1. 7 | */ 8 | #include 9 | #include 10 | 11 | int main(){ 12 | // Empty queue 13 | std::queue myqueue; 14 | 15 | // pushing elements into queue using push() 16 | myqueue.push(0); 17 | myqueue.push(1); 18 | myqueue.push(2); 19 | 20 | // pop items from queue 21 | myqueue.pop(); // pops 0 from queue 22 | myqueue.pop(); // pops 1 from queue 23 | 24 | // print contents of queue 25 | while (!myqueue.empty()){ 26 | std::cout << ' ' << myqueue.front(); 27 | myqueue.pop(); 28 | } 29 | } -------------------------------------------------------------------------------- /algorithm/accumulate.md: -------------------------------------------------------------------------------- 1 | # accumulate 2 | 3 | **Description :** This function is used to perform certain operations on the range of iterators provided. It takes two iterators and initial value as parameters.The function is overloaded with first type accepting only these three arguments and returning sum of all elements that can be iterated using the given iterators while the second type takes a binary operation function as additional parameter and performs certain user defined operations on pair of elements pointed by the iterators. 4 | 5 | 6 | **Example** : 7 | 8 | ```cpp 9 | int GCD(int x, int y){ 10 | return std::__gcd(x,y); 11 | } 12 | 13 | int main() { 14 | 15 | std::vector v{10, 2, 4, 6}; 16 | 17 | std::cout << "The sum of elements of v is : " << 18 | std::accumulate(v.begin(), v.end(), 0) << 19 | " and GCD of all elements is : " << 20 | std::accumulate(v.begin(), v.end(), 0, GCD) << std::endl; 21 | } 22 | ``` 23 | **[Run Code](https://ideone.com/Gf2d0G)** 24 | -------------------------------------------------------------------------------- /list/end.md: -------------------------------------------------------------------------------- 1 | # end 2 | 3 | **Description** : The list::end() is a built-in function in C++ STL which is used to get an iterator to past the last element. 4 | 5 | **Example**: 6 | ```cpp 7 | // Creating a list 8 | std::list demoList; 9 | 10 | // Add elements to the List 11 | demoList.push_back(10); 12 | demoList.push_back(20); 13 | demoList.push_back(30); 14 | demoList.push_back(40); 15 | 16 | // using end() to get iterator 17 | // to past the last element 18 | std::list::iterator it = demoList.end(); 19 | 20 | // This will not print the last element 21 | std::cout << "Returned iterator points to : " << *it << std::endl; 22 | 23 | // Using end() with begin() as a range to 24 | // print all of the list elements 25 | for (auto itr = demoList.begin(); 26 | itr != demoList.end(); itr++) { 27 | std::cout << *itr << " "; 28 | } 29 | 30 | ``` 31 | **[Run Code](https://rextester.com/ZMWJ67741)** 32 | -------------------------------------------------------------------------------- /list/insert.md: -------------------------------------------------------------------------------- 1 | # insert 2 | 3 | **Description** : The list::insert() is used to insert the elements at any position of list. 4 | 5 | **Example**: 6 | ```cpp 7 | // declaring list 8 | std::list list1; 9 | 10 | // using assign() to insert multiple numbers 11 | // creates 3 occurrences of "2" 12 | list1.assign(3, 2); 13 | 14 | // initializing list iterator to beginning 15 | std::list::iterator it = list1.begin(); 16 | 17 | // iterator to point to 3rd position 18 | advance(it, 2); 19 | 20 | // using insert to insert 1 element at the 3rd position 21 | // inserts 5 at 3rd position 22 | list1.insert(it, 5); 23 | 24 | // Printing the new list 25 | std::cout << "The list after inserting" << " 1 element using insert() is : "; 26 | for (auto value : list1) { 27 | std::cout << value << " "; 28 | } 29 | 30 | std::cout << std::endl; 31 | 32 | ``` 33 | **[Run Code](https://rextester.com/CCMFL13509)** 34 | -------------------------------------------------------------------------------- /map/cend.md: -------------------------------------------------------------------------------- 1 | # cend 2 | 3 | **Description :** This function returns a constant iterator pointing to the theoretical element that follows last element in the multimap. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | // C++ program to illustrate the map::cend() function 9 | #include 10 | 11 | int main() { 12 | 13 | // initialize container 14 | std::map mp; 15 | 16 | // insert elements in random order 17 | mp.insert({ 2, 30 }); 18 | mp.insert({ 1, 40 }); 19 | mp.insert({ 3, 60 }); 20 | mp.insert({ 4, 20 }); 21 | mp.insert({ 5, 50 }); 22 | 23 | // print the elements 24 | std::cout << "\nThe map is : \n"; 25 | std::cout << "KEY\tELEMENT\n"; 26 | //cend() function called 27 | for (auto itr = mp.cbegin(); itr != mp.cend(); ++itr) { 28 | std::cout << itr->first 29 | << '\t' << itr->second << '\n'; 30 | } 31 | return 0; 32 | } 33 | ``` 34 | **[Run Code](https://rextester.com/QMAMQ71096)** 35 | -------------------------------------------------------------------------------- /snippets/algorithm/remove_copy.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Thamara Andrade 3 | Date : Date format 09/09/2019 4 | Time : Time format 23:00 5 | Description : Removes elements from vector that satisfies a criteria. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(){ 13 | std::vector origin {3, 5, 3, 1, 2, 3}; 14 | std::vector destination; 15 | 16 | // Copy elements to destination that are not 3 17 | std::remove_copy(origin.begin(), //first 18 | origin.end(), //last 19 | std::back_inserter(destination), //d_first 20 | 3); 21 | 22 | // origin is still {3, 5, 3, 1, 2, 3} 23 | for (auto value : origin) { 24 | std::cout << value << " "; 25 | } 26 | std::cout << std::endl; 27 | 28 | // destination is {5, 1, 2} 29 | for (auto value : destination) { 30 | std::cout << value << " "; 31 | } 32 | std::cout << std::endl; 33 | } -------------------------------------------------------------------------------- /map/empty.md: -------------------------------------------------------------------------------- 1 | # empty 2 | 3 | **Description :** It is used to check if the map container is empty or not. It accepts no parameters. It return true if map is empty otherwise false. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | // C++ program to Demonstrate map::empty() function 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int main() { 14 | 15 | // initialize container 16 | std::map mp; 17 | 18 | // insert elements in random order 19 | mp.insert({ 1, 10 }); 20 | mp.insert({ 2, 20 }); 21 | mp.insert({ 3, 30 }); 22 | mp.insert({ 4, 40 }); 23 | mp.insert({ 5, 50 }); 24 | mp.insert({ 6, 60 }); 25 | 26 | // print bool value return by empty() STL 27 | std::cout << " MAP IS EMPTY : " ; 28 | if (mp.empty()) { 29 | std::cout << "True"; 30 | } 31 | else { 32 | std::cout << "False"; 33 | } 34 | 35 | return 0; 36 | } 37 | ``` 38 | 39 | **[Run Code](https://rextester.com/UBR30476)** 40 | -------------------------------------------------------------------------------- /queue/queue.md: -------------------------------------------------------------------------------- 1 | # queue 2 | 3 | **Description :** The Queue is a abstract data type that is a lnlo (last in last out) meaning that the first element that you [push](https://github.com/Bhupesh-V/30-seconds-of-cpp/blob/master/queue/push.md) will be the first element to [pop](https://github.com/Bhupesh-V/30-seconds-of-cpp/blob/master/queue/pop.md). Below are examples of Constructor's to inialize a queue 4 | **Example** : 5 | 6 | ```cpp 7 | #include 8 | #include 9 | 10 | int main(void) 11 | { 12 | // Create a empty queue 13 | std::queue emptyConstructor; 14 | 15 | // Push a value to it so it is no longer empty 16 | emptyConstructor.push(3); 17 | 18 | // Copy the above queue 19 | std::queue copiedConstructor(emptyConstructor); 20 | 21 | std::cout << emptyConstructor.front() << std::endl; 22 | 23 | std::cout << copiedConstructor.front() << std::endl; 24 | 25 | return 0; 26 | } 27 | ``` 28 | **[Run Code](https://rextester.com/TGP83427)** 29 | -------------------------------------------------------------------------------- /algorithm/find_end.md: -------------------------------------------------------------------------------- 1 | # find_end 2 | 3 | **Description :** Return an iterator which points to the first element of the last occurrence of the sequence [s_first, s_last) in the range target [first, last). If list is empty or sequence isn't found, returns last. 4 | 5 | **Example** : 6 | 7 | ```cpp 8 | std::vector sequence{ 13, 5, 2, }; 9 | std::vector target{ 18, 11, 7, 13, 5, 2, 12, 5, 13, 5, 2 }; 10 | 11 | // find the first element of the last occurrence of the given sequence 12 | std::vector::iterator result = std::find_end(target.begin(), target.end(), sequence.begin(), sequence.end()); 13 | 14 | if(result != target.end()){ 15 | // list wasn't empty and sequence occurred 16 | std::cout << "The result is: " << std::distance(target.begin(), result) << "." << std::endl; 17 | } else{ 18 | std::cout << "No such pattern found or lists are empty." << std::endl; 19 | } 20 | ``` 21 | **[See Sample code](snippets/vector/find_end.cpp)**
22 | **[Run Code](https://rextester.com/UYNGGS38128)** 23 | 24 | -------------------------------------------------------------------------------- /algorithm/make_heap.md: -------------------------------------------------------------------------------- 1 | # make_heap 2 | 3 | **Description :** make_heap() is used to transform a given sequence into a heap. 4 | A heap is a data structure which points to highest( or lowest) element and making its access in O(1) time. 5 | 6 | **Example** : 7 | ```cpp 8 | int main() { 9 | int i; 10 | // initializing vector; 11 | vector vi = { 4, 6, 7, 9, 11, 4 }; 12 | 13 | // using make_heap() to transform vector into 14 | // a max heap 15 | 16 | make_heap(vi.begin(),vi.end()); 17 | 18 | //checking if heap using 19 | // front() function 20 | cout << "The maximum element of heap is : "; 21 | cout << vi.front() << endl; 22 | 23 | //printing the heap 24 | for(i=0;i