In C++14, there are pretty good new overloads of std::equal
, std::mismatch
, and std::is_permutation
that can be used to take a pair of iterators for the second range. They can be used with the new C++ Builder 12 along with the 11.x, or 10.x versions too. In this post, we explain these new overloads that we use ranges of iterations.
Table of Contents
What are the new overloads for ranges in C++ 14?
In C++14, there are new overloads of std::equal
, std::mismatch
, and std::is_permutation
that can be used to take a pair of iterators for the second range. We can pass them in two full ranges that have a beginning and end.
In modern C++, the range parameter is obtained from a std::list without the original list that needs to be traversed through to get the size. In C++11, std::equal
, std::mismatch
, and std::is_permutation
had 3 parameters, these were First1, Last1 for the Range1 and First2 for the Range2. In C++ 14, there is one more parameter that you can use, it is the Last2 for the Range2.
What is the new overload for the std::equal in C++14?
In C++11, std::equal is defined as below.
1 2 3 4 |
template<class _InIt1, class _InIt2> inline bool equal(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2) |
The std::equal
is defined in the <xutility>
header included in the <algorithm>
header that returns true if the range between the First1 and Last1 is equal to the range between the First2 and the First2 + (Last1 – First1), and it returns false if this is not satisfied.
1 2 3 4 |
std::string s = "abCba"; std::cout << std::equal(s.cbegin(), s.cbegin() + s.size() / 2, s.crbegin()); |
In C++14, std::equal is defined with the new input iteration parameter as below,
1 2 3 4 |
template<class _InIt1, class _InIt2> inline bool equal(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2) |
In C++ 14, the std::equal returns true if the range between the First1 and the Last1 is equal to the range between the First2 and the Last2, and it returns false if this is not satisfied. Here is an example,
1 2 3 4 |
std::string s = "abCba"; std::cout << std::equal( s.cbegin(), s.cbegin() + s.size() / 2, s.crbegin(), s.crend()); |
What is the new overload for the std::mismatch in C++14?
The std::mismatch is defined in the <algorithm>
header that returns the first mismatching pair of elements from two different ranges. First range is defined by First1 and the Last1 and the another one is defined by First2 and Last2 as in definition below,
1 2 3 4 |
template<class _InIt1, class _InIt2, class _Pr> inline pair<_InIt1, _InIt2> mismatch(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _Pr _Pred) |
Here is an example:
1 2 3 4 |
std::string s = "LearnCPlusPlus.org"; auto m = std::mismatch( str.begin(), str.end(), str.rbegin(), str.rend() ); |
What is the new overload for the std::is_permutation in C++14?
The std::is_permutation is defined in the <algorithm>
header that returns true if there exists a permutation of the elements in the range between the First1 and Last1 that makes that range equal to the range between First2 and Last 2. If the Last2 t was not given it denotes first2 + (last1 – first1). It is defined as below.
1 2 3 4 |
template<class _FwdIt1, class _FwdIt2, class _Pr> inline bool is_permutation(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred) |
Is there a full example about the new overloads for ranges in C++ 14?
Here is a full example about the new overloads for std::equal
, std::mismatch
, and std::is_permutation
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <string> #include <string_view> #include <iomanip> #include <algorithm> int main() { auto vec = {10, 20, 30, 40, 50}; std::string str("LearnCPlusPlus.org"); std::cout << std::equal(str.cbegin(), str.cbegin(), str.crbegin(), str.crend() ) << std::endl; auto m = std::mismatch( str.begin(), str.end(), str.rbegin(), str.rend() ); std::cout << std::is_permutation( vec.begin(), vec.end(), vec.begin(), vec.end() ) << std::endl; system("pause"); return 0; } |
For more details, please see this https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3671.html
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.