Skip to content

Commit

Permalink
Documenting algorithm/includes
Browse files Browse the repository at this point in the history
  • Loading branch information
thamara committed Sep 3, 2019
1 parent d2a6139 commit 52bcc87
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
28 changes: 28 additions & 0 deletions algorithm/includes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# includes

**Description** : Test whether sorted range `[first1, last1)` contains (includes) all elements from another sorted range `[first2, last2)`.

**Example**:
```cpp
std::vector<int> v1 {8, 2, 0, 4};
std::vector<int> v2 {2, 0};
std::vector<int> v3 {1, 0};

// includes requires sorted ranges
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
std::sort(v3.begin(), v3.end());

auto v1ContainsV2 = std::includes(v1.begin(), v1.end(), // first range
v2.begin(), v2.end()); // second range
// prints 1/true
std::cout << " v1 contains v2? " << v1ContainsV2 << std::endl;

auto v1ContainsV3 = std::includes(v1.begin(), v1.end(), // first range
v3.begin(), v3.end()); // second range
// prints 0/false
std::cout << " v1 contains v3? " << v1ContainsV3 << std::endl;

```
**[See Sample code](snippets/algorithm/includes.cpp)**
**[Run Code](https://rextester.com/MMPQ67900)**
32 changes: 32 additions & 0 deletions snippets/algorithm/includes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Author : Thamara Andrade
Date : Date format 02/09/2019
Time : Time format 02:00
Description : Checks if a range is included in another range.
*/

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
std::vector<int> v1 {8, 2, 0, 4};
std::vector<int> v2 {2, 0};
std::vector<int> v3 {1, 0};

// includes requires sorted ranges
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
std::sort(v3.begin(), v3.end());

auto v1ContainsV2 = std::includes(v1.begin(), v1.end(), // first range
v2.begin(), v2.end()); // second range
// prints 1/true
std::cout << " v1 contains v2? " << v1ContainsV2 << std::endl;

auto v1ContainsV3 = std::includes(v1.begin(), v1.end(), // first range
v3.begin(), v3.end()); // second range
// prints 0/false
std::cout << " v1 contains v3? " << v1ContainsV3 << std::endl;
}

0 comments on commit 52bcc87

Please sign in to comment.