Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added C++ support #41

Merged
merged 3 commits into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/assets/snippets/cpp/adjacent_find.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# adjacent_find

**Description** : Binary function which returns first adjacent element pairs based on certain condition (as third argument). Default condition checks equality.

**Example** :
```cpp
#include<vector>
#include<algorithm>
#include<iostream>

int main(){
std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
// Binary function which returns first adjacent element pairs based on certain condition (as third argument) .
// Default condition checks equality.
auto i = std::adjacent_find (v.begin(), v.end());
std::cout <<"First adjacent element that are equal " << i <<"\n";

return 0;
}
```
26 changes: 26 additions & 0 deletions src/assets/snippets/cpp/assign.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# assign

**Description** :The list::assign() is a built-in function in C++ STL which is used to assign values to a list.

**Example**:
```cpp
#include<iostream>
#include<list>

int main(){
// Initialization of list
std::list<int> demo_list;

// Assigning the value 100, 5 times
// to the list, list_demo.
demo_list.assign(5, 100);

// Displaying the list
for (int itr : demo_list) {
std::cout << itr << " ";
}

return 0;
}

```
22 changes: 22 additions & 0 deletions src/assets/snippets/cpp/back.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# back

**Description** : The list::back() function in C++ STL returns a direct reference to the last element in the list container.

**Example**:
```cpp
#include<iostream>
#include<list>

int main(){
// Initialization of list
std::list<int> demo_list;
// Adding elements to the list
demo_list.push_back(10);
demo_list.push_back(20);
demo_list.push_back(30);
// prints the last element of demo_list
std::cout << demo_list.back();

return 0;
}
```
20 changes: 20 additions & 0 deletions src/assets/snippets/cpp/begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# begin

**Description** : begin() function is used to return an iterator pointing to the first element of the list container.

**Example**:
```cpp
#include<iostream>
#include<list>

int main(){
// declaration of list container
std::list<int> mylist{ 1, 2, 3, 4, 5 };

// using begin() to print list
for (auto it = mylist.begin(); it != mylist.end(); ++it)
std::cout << " " << *it;

return 0;
}
```
30 changes: 30 additions & 0 deletions src/assets/snippets/cpp/binary_search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# binary_search

**Description** : Binary search is a widely used searching algorithm that requires the array to be sorted before search is applied. The main idea behind this algorithm is to keep dividing the array in half (divide and conquer) until the element is found, or all the elements are exhausted.

**Example** :
```cpp
#include<iostream>
#include<algorithm>

void show(int a[], int arraysize) {
for (int i = 0; i < arraysize; ++i)
std::cout << a[i] << " ";
}
int main() {
int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int asize = sizeof(a) / sizeof(a[0]);
std::cout << "\n The array is : ";
show(a, asize);
/* Make sure to sort the array before applyng binary_search() */
std::sort(a, a + asize);

std::cout << "\nSearch for element 10 : ";
if (binary_search(a, a + 10, 10))
std::cout << "\nElement found in the array";
else
std::cout << "\nElement not found in the array";

return 0;
}
```
21 changes: 21 additions & 0 deletions src/assets/snippets/cpp/clear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# clear

**Description** : clear() function is used to remove all the elements of the list container, thus making it size 0.

**Example**:
```cpp
#include<iostream>
#include<list>

int main(){

std::list<int> mylist{ 1, 2, 3, 4, 5 };
// List becomes empty
mylist.clear();
// Printing the list
for (auto it = mylist.begin(); it != mylist.end(); ++it)
std::cout << ' ' << *it;

return 0;
}
```
20 changes: 20 additions & 0 deletions src/assets/snippets/cpp/count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# count

**Description :** : Returns the number of elements in the range `[first, last)` satisfying specific criteria(counts the elements that are equal to *value*).

**Example** :
```cpp
#include<vector>
#include<algorithm>
#include<iostream>

int main(){
std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
int target1 = 3; // determine how many integers in a std::vector match a target value.
int num_items1 = count(v.begin(), v.end(), target1);
std::cout << "number: " << target1 << " count: " << num_items1 << '\n';

return 0;
}
```
**[See Sample Code](https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/algorithm/count.cpp)**
19 changes: 19 additions & 0 deletions src/assets/snippets/cpp/count_if.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# count_if

**Description** : Returns the number of elements in the range `[first, last)` satisfying specific criteria(counts the elements that are equal to *value*).

**Example** :
```cpp
#include<vector>
#include<algorithm>
#include<iostream>

int main(){
vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
// use a lambda expression to count elements divisible by 3.
int num_items3 = count_if(v.begin(), v.end(), [](int i){return i % 3 == 0;});
std::cout << "Number of items divisible by 3 : " << num_items3 << '\n';

return 0;
}
```
33 changes: 33 additions & 0 deletions src/assets/snippets/cpp/erase.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# erase

**Description** : The list::erase() is a built-in function in C++ STL which is used to delete elements from a list container. This function can be used to remove a single element or a range of elements from the specified list container.

**Example** :
```cpp
#include<iostream>
#include<list>

int main(){
// Creating a list
std::list<int> demoList;

// Add elements to the List
demoList.push_back(10);
demoList.push_back(20);
demoList.push_back(30);

// Creating iterator to point to first
// element in the list
std::list<int>::iterator itr = demoList.begin();
// Deleting the first element
demoList.erase(itr);

// Printing elements of list after deleting first element
std::cout << "\nList after deleting first element:";
for (auto itr = demoList.begin(); itr != demoList.end(); itr++) {
std::cout << *itr << " ";
}

return 0;
}
```
23 changes: 23 additions & 0 deletions src/assets/snippets/cpp/find.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# find

**Description** : Returns the first element in the range [first, last) that satisfies specific criteria(searches for an element equal to *value*).

**Example** :
```cpp
#include<vector>
#include<algorithm>
#include<iostream>

int main(){
std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
int searchme = 4;

if(find(std::begin(v), std::end(v), searchme) != end(v)){
std::cout <<"\n v conatins 3";
}
else
std::cout<<"No match !!";

return 0;
}
```
26 changes: 26 additions & 0 deletions src/assets/snippets/cpp/find_first_of.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# find_first_of

**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) ).

**Example** :
```cpp
#include<vector>
#include<algorithm>
#include<iostream>

int main(){
std::vector<int> v{0, 2, 3, 25, 5};
std::vector<int> t{3, 19, 10, 2};

auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end());

if (result == v.end()) {
std::cout << "no elements of v were equal to 3, 19, 10 or 2\n";
}
else {
std::cout << "found a match at "<< std::distance(v.begin(), result) << "\n";
}

return 0;
}
```
24 changes: 24 additions & 0 deletions src/assets/snippets/cpp/find_if.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# find_if

**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*).

**Example** :
```cpp
#include<vector>
#include<algorithm>
#include<iostream>

bool IsOdd (int i) {
return ((i%2)==1);
}

int main(){
std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
if(find_if(std::begin(v), std::end(v),IsOdd) != end(v)){
std::cout <<"\n Odd Value Found";
}
else
std::cout<<"No match !!";
return 0;
}
```
24 changes: 24 additions & 0 deletions src/assets/snippets/cpp/find_if_not.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# find_if_not

**Description** : Returns the first element in the range [first, last) that satisfies specific criteria(searches for an element for which predicate q returns *false*).

**Example** :
```cpp
#include<vector>
#include<algorithm>
#include<iostream>

bool IsOdd (int i) {
return ((i%2)==1);
}

int main(){
std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
if(find_if_not(std::begin(v), std::end(v),IsOdd) != end(v)){
std::cout <<"\n First Even Value";
}
else
std::cout<<"No match !!";
return 0;
}
```
29 changes: 29 additions & 0 deletions src/assets/snippets/cpp/front.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# front

**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.

**Example**:
```cpp
#include<iostream>
#include<list>

int main(){
// Creating a list
std::list<int> demoList;

// Add elements to the List
demoList.push_back(10);
demoList.push_back(20);
demoList.push_back(30);
demoList.push_back(40);

// get the first element using front()
int element = demoList.front();

// Print the first element (i.e 10)
std::cout << element;

return 0;
}

```
29 changes: 29 additions & 0 deletions src/assets/snippets/cpp/generate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# generate

**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).
The generator function has to be defined by the user, and it is called successively for assigning the numbers.

**Example** :
```cpp
#include<vector>
#include<algorithm>
#include<iostream>

int gen() {
static int i = 0;
return ++i;
}

int main() {
int i;
std::vector<int> v1(10);

std::generate(v1.begin(), v1.end(), gen);

vector<int>::iterator i1;
for (i1 = v1.begin(); i1 != v1.end(); ++i1) {
std::cout << *i1 << " ";
}
return 0;
}
```
Loading