Standard Library Range ComparisonsRange ComparisonsCompare
New to C++'s standard library algorithms? ⇒ Short Introduction
std::vector<int> r1 {0,1,2,3,4,5,6,7,8,9};
std::vector<int> r2 {1,2,3,4,5,6,0,0};
// compare subranges (as shown in image):
cout << equal(begin(r1)+2, begin(r1)+7, begin(r2)+1); // true
// compare entire containers:
cout << equal(begin(r1), end(r1), begin(r2)); // false
// compare other subranges:
cout << equal(begin(r1)+1, begin(r1)+7, begin(r2), begin(r2)+6); // true
// custom type
struct P { int x; char q; };
std::vector<P> a { P{1,'n'}, P{2,'m'} };
std::vector<P> b { P{1,'y'}, P{2,'z'} };
// compare 'P's only by member 'x'
cout << equal(begin(a),end(a), begin(b),end(b),
[](P p1, P p2) { return p1.x < p2.x; } ); // true
std::vector<int> range1 {2,3,4,5,6};
std::vector<int> range2 {2,3,4,5,6};
// compare subranges (as shown in image):
cout << std::ranges::equal(range1, range2); // true
std::vector<int> r1 {0,1,2,3,4,5,6,7,8,9};
std::vector<int> r2 {1,2,3,4,5,7,8,8};
// compare subranges (as shown in image):
auto p = mismatch(begin(r1)+2, begin(r1)+9, begin(r2)+1);
// != end-of-range ⇒ mismatch
if(p.first != begin(r1)+9) { auto p1value = *p.first; } // 6
if(p.second != end(r2)) { auto p2value = *p.second; } // 7
// compare entire containers:
auto q = mismatch(begin(r1), end(r1), begin(r2));
if(q.first != end(r1)) {
auto q1value = *q.first; // 0
auto q1index = distance(begin(r1), q.first); // 0
}
if(q.second != end(r2)) {
auto q2value = *q.second; // 1
auto q2index = distance(begin(r2), q.second); // 0
}
std::vector<int> range1 {1,5,4,6,3};
std::vector<int> range2 {1,5,4,7,3};
auto [p1,p2] = std::ranges::mismatch(range1, range2);
auto const value1 = *p1; // 6
auto const value2 = *p2; // 7
std::string r1 = "xalgori";
std::string r2 = "abced";
// compare subranges (as shown in image):
cout << lexicographical_compare( begin(r1)+1, begin(r1)+5, begin(r2)+1, begin(r2)+4); // true (r1 before r2)
// compare entire containers:
cout << lexicographical_compare( begin(r1), end(r1), begin(r2), end(r2)); // false (r1 after r2)
// strings are actually already comparable:
cout << (r1 < r2); // false
cout << (r1 > r2); // true (r1 after r2)
// custom type
struct P { int x; int y; };
std::vector<P> a { P{1,9}, P{2,9} };
std::vector<P> b { P{1,8}, P{3,8} };
// compare 'P's only by member 'x'
cout << lexicographical_compare(begin(a),end(a), begin(b),end(b),
[](P p1, P p2) { return p1.x < p2.x; } ); // true
std::vector<char> range1 = {'a','l','g','o'};
std::vector<char> range2 = {'b','c','e'};
cout << std::ranges::lexicographical_compare(range1, range2); // true
cout << std::ranges::lexicographical_compare(range1, range2, std::greater<>{}); // false
std::string r1 = "xalgori";
std::string r2 = "abced";
// compare subranges (as shown in image):
auto const lcA = lexicographical_compare_three_way(
begin(r1)+1, begin(r1)+5, begin(r2)+1, begin(r2)+4);
cout << (lcA < 0); // true
cout << (lcA == 0); // false
cout << (lcA > 0); // false
// compare entire strings // with C++20's 'spaceship' operator:
auto const lcB = r1 <=> r2;
cout << (lcB < 0); // false
cout << (lcB == 0); // false
cout << (lcB > 0); // true
Related …
- Standard Algorithms Overview
- Standard Algorithms Introduction
- Standard Sequence Containers (
vector
,deque
,list
, …) - Standard Associative Containers (
map
,set
, …) - Standard Sequence Views
- cppreference: Algorithms Library
- cppreference: Containers Library
- A Tour of C++: Containers and Algorithms
- Algorithms Overview Sheet