Skip to content

Commit

Permalink
Added crbegin example for std set
Browse files Browse the repository at this point in the history
  • Loading branch information
sometallgit committed Oct 11, 2020
1 parent b50dc9b commit 45dcacd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
31 changes: 31 additions & 0 deletions set/crbegin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# crbegin

**Description :**
Get a const_reverse_iterator pointing at the last element of the container. This is useful
when wanting a read-only iterator to iterate a container in reverse order.

**Example :**
```cpp
//Run Code To Demonstrate use of set.size()
#include<iostream>
#include<set>

int main() {
// Create a set object holding integers
std::set<int> mySet{ 3, 1, 5, 2, 4 };

// Get a const_reverse_iterator pointing to the last element
std::set<int>::reverse_iterator r_it = mySet.crbegin();

// Iterate through the const_reverse_iterator
for (; r_it != mySet.crend(); ++r_it){
std::cout << *r_it << std::endl;
}

return 0;
}

```

**[See Sample code](../snippets/set/crbegin.cpp)**
**[Run Code](https://ideone.com/ccq58H)**
25 changes: 25 additions & 0 deletions snippets/set/crbegin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Follow the Style Guide while submitting code PR.
// Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide
/*
Author : Cameron Bradley
Date : 11/10/2020
Time : 12:32
Description : Get a const reverse iterator pointing at the last element of the container
*/
#include<iostream>
#include<set>

int main() {
// Create a set object holding integers
std::set<int> mySet{ 3, 1, 5, 2, 4 };

// Get a const_reverse_iterator pointing to the last element
std::set<int>::reverse_iterator r_it = mySet.crbegin();

// Iterate through the const_reverse_iterator
for (; r_it != mySet.crend(); ++r_it) {
std::cout << *r_it << std::endl;
}

return 0;
}

0 comments on commit 45dcacd

Please sign in to comment.