forked from Bhupesh-V/30-seconds-of-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |