#include <array>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
#include <iostream>
#include <string>
// INSERT_ELEMENTS (collection, first, last)
// - fill values from first to last into the collection
// - NOTE: NO half-open range
template <typename T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
for (int i=first; i<=last; ++i) {
coll.insert(coll.end(),i);
}
}
// PRINT_ELEMENTS()
// - prints optional string optcstr followed by
// - all elements of the collection coll
// - separated by spaces
template <typename T>
inline void PRINT_ELEMENTS (const T& coll,
const std::string& optcstr="")
{
std::cout << optcstr;
for (auto elem : coll) {
std::cout << elem << ' ';
}
std::cout << std::endl;
}
// PRINT_MAPPED_ELEMENTS()
// - prints optional string optcstr followed by
// - all elements of the key/value collection coll
// - separated by spaces
template <typename T>
inline void PRINT_MAPPED_ELEMENTS (const T& coll,
const std::string& optcstr="")
{
std::cout << optcstr;
for (auto elem : coll) {
std::cout << '[' << elem.first
<< ',' << elem.second << "] ";
}
std::cout << std::endl;
}
// The c++ standard libary 2nd p535
bool checkEven (int elem, bool even)
{
if (even) {
return elem % 2 == 0;
}
else {
return elem % 2 == 1;
}
}
// The c++ standard libary 2nd p541
bool doubled (int elem1, int elem2)
{
return elem1 * 2 == elem2;
}
// The c++ standard libary 2nd p543
bool bothEvenOrOdd (int elem1, int elem2)
{
return elem1 % 2 == elem2 % 2;
}
int main()
{
std::deque<int> coll;
coll = { 1, 2, 7, 7, 6, 3, 9, 5, 7, 7, 7, 3, 6 };
PRINT_ELEMENTS(coll);
// find three consecutive elements with value 7
std::deque<int>::iterator pos;
pos = std::search_n (coll.begin(), coll.end(), // range
3, // count
7); // value
// print result
if (pos != coll.end()) {
std::cout << "three consecutive elements with value 7 "
<< "start with " << distance(coll.begin(),pos) +1
<< ". element" << std::endl;
}
else {
std::cout << "no four consecutive elements with value 7 found"
<< std::endl;
}
// find four consecutive odd elements
pos = std::search_n (coll.begin(), coll.end(), // range
4, // count
0, // value
[](int elem, int value){ // criterion
return elem%2==1;
});
// print result
if (pos != coll.end()) {
std::cout << "first four consecutive odd elements are: ";
for (int i=0; i<4; ++i, ++pos) {
std::cout << *pos << ' ';
}
}
else {
std::cout << "no four consecutive elements with value > 3 found";
}
std::cout << std::endl;
// The c++ standard libary 2nd p534
std::deque<int> coll2;
std::list<int> subcoll;
INSERT_ELEMENTS(coll2,1,7);
INSERT_ELEMENTS(coll2,1,7);
INSERT_ELEMENTS(subcoll,3,6);
PRINT_ELEMENTS(coll2, "coll2: ");
PRINT_ELEMENTS(subcoll, "subcoll: ");
// search first occurrence of subcoll in coll
std::deque<int>::iterator pos2;
pos2 = std::search (coll2.begin(), coll2.end(), // range
subcoll.begin(), subcoll.end()); // subrange
// loop while subcoll found as subrange of coll
while (pos2 != coll2.end()) {
// print position of first element
std::cout << "subcoll found starting with element "
<< std::distance(coll2.begin(), pos2) + 1
<< std::endl; //error coll.begin() subcoll found starting with element 34831
// subcoll found starting with element 34838
// search next occurrence of subcoll
++pos2;
pos2 = std::search (pos2, coll2.end(), // range
//error pos, 造成死循环 2020年06月19日 20时05分42秒
subcoll.begin(), subcoll.end()); // subrange
}
// The c++ standard libary 2nd p535
std::vector<int> coll3;
INSERT_ELEMENTS(coll3, 1, 9);
PRINT_ELEMENTS(coll3, "coll3: ");
// arguments for checkEven()
// - check for: "even odd even"
bool checkEvenArgs[3] = { true, false, true };
// search first subrange in coll
std::vector<int>::iterator pos3;
pos3 = std::search (coll3.begin(), coll3.end(), // range
checkEvenArgs, checkEvenArgs+3, // subrange values
checkEven); // subrange criterion
// loop while subrange found
while (pos3 != coll3.end()) {
// print position of first element
std::cout << "subrange found starting with element "
<< std::distance(coll3.begin(),pos3) + 1
<< std::endl;
// search next subrange in coll
pos3 = std::search (++pos3, coll3.end(), // range
checkEvenArgs, checkEvenArgs+3, // subr. values
checkEven); // subr. criterion
}
// The c++ standard libary 2nd p539
std::deque<int> coll4;
std::list<int> subcoll2;
INSERT_ELEMENTS(coll4,1,7);
INSERT_ELEMENTS(coll4,1,7);
INSERT_ELEMENTS(subcoll2,3,6);
PRINT_ELEMENTS(coll4, "coll4: ");
PRINT_ELEMENTS(subcoll2,"subcoll2: ");
// search last occurrence of subcoll in coll
std::deque<int>::iterator pos4;
pos4 = std::find_end (coll4.begin(), coll4.end(), // range
subcoll2.begin(), subcoll2.end()); // subrange
// loop while subcoll found as subrange of coll
std::deque<int>::iterator end(coll4.end());
while (pos4 != end) {
// print position of first element
std::cout << "subcoll found starting with element "
<< std::distance(coll4.begin(),pos4) + 1
<< std::endl;
// search next occurrence of subcoll
end = pos4;
pos4 = std::find_end (coll4.begin(), end, // range
subcoll2.begin(), subcoll2.end()); // subrange
}
// The c++ standard libary 2nd p540
std::vector<int> coll5;
std::list<int> searchcoll;
INSERT_ELEMENTS(coll5,1,11);
INSERT_ELEMENTS(searchcoll,3,5);
PRINT_ELEMENTS(coll5, "coll5: ");
PRINT_ELEMENTS(searchcoll,"searchcoll: ");
// search first occurrence of an element of searchcoll in coll
std::vector<int>::iterator pos5;
pos5 = std::find_first_of (coll5.begin(), coll5.end(), // range
searchcoll.begin(), // beginning of search set
searchcoll.end()); // end of search set
std::cout << "first element of searchcoll in coll5 is element "
<< std::distance(coll5.begin(),pos5) + 1
<< std::endl;
// search last occurrence of an element of searchcoll in coll
std::vector<int>::reverse_iterator rpos2;
rpos2 = std::find_first_of (coll5.rbegin(), coll5.rend(), // range
searchcoll.begin(), // beginning of search set
searchcoll.end()); // end of search set
std::cout << "last element of searchcoll in coll5 is element "
<< std::distance(coll5.begin(),rpos2.base())
<< std::endl;
// The c++ standard libary 2nd p541
std::vector<int> coll6;
coll6.push_back(1);
coll6.push_back(3);
coll6.push_back(2);
coll6.push_back(4);
coll6.push_back(5);
coll6.push_back(5);
coll6.push_back(0);
PRINT_ELEMENTS(coll6,"coll6: ");
// search first two elements with equal value
std::vector<int>::iterator pos6;
pos6 = std::adjacent_find (coll6.begin(), coll6.end());
if (pos6 != coll6.end()) {
std::cout << "first two elements with equal value have position "
<< std::distance(coll6.begin(),pos6) + 1
<< std::endl;
}
// search first two elements for which the second has double the value of the first
pos6 = std::adjacent_find (coll6.begin(), coll6.end(), // range
doubled); // criterion
if (pos6 != coll6.end()) {
std::cout << "first two elements with second value twice the "
<< "first have pos. "
<< std::distance(coll6.begin(),pos6) + 1
<< std::endl;
}
// The c++ standard libary 2nd p543
std::vector<int> coll7;
std::list<int> coll8;
INSERT_ELEMENTS(coll7,1,7);
INSERT_ELEMENTS(coll8,3,9);
PRINT_ELEMENTS(coll7,"coll7: ");
PRINT_ELEMENTS(coll8,"coll8: ");
// check whether both collections are equal
if (std::equal (coll7.begin(), coll7.end(), // first range
coll8.begin())) { // second range
std::cout << "coll7 == coll8" << std::endl;
}
else {
std::cout << "coll7 != coll8" << std::endl;
}
// check for corresponding even and odd elements
if (std::equal (coll7.begin(), coll7.end(), // first range
coll8.begin(), // second range
bothEvenOrOdd)) { // comparison criterion
std::cout << "even and odd elements correspond" << std::endl;
}
else {
std::cout << "even and odd elements do not correspond" << std::endl;
}
// The c++ standard libary 2nd p545
std::vector<int> coll11;
std::list<int> coll12;
std::deque<int> coll13;
coll11 = { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
coll12 = { 1, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
coll13 = { 11, 12, 13, 19, 18, 17, 16, 15, 14, 11 };
PRINT_ELEMENTS(coll11,"coll11: ");
PRINT_ELEMENTS(coll12,"coll12: ");
PRINT_ELEMENTS(coll13,"coll13: ");
// check whether both collections have equal elements in any order
if (std::is_permutation (coll11.cbegin(), coll11.cend(), // first range
coll12.cbegin())) { // second range
std::cout << "coll11 and coll12 have equal elements" << std::endl;
}
else {
std::cout << "coll11 and coll12 don't have equal elements" << std::endl;
}
// check for corresponding number of even and odd elements
if (std::is_permutation (coll11.cbegin(), coll11.cend(), // first range
coll13.cbegin(), // second range
bothEvenOrOdd)) { // comparison criterion
std::cout << "numbers of even and odd elements match" << std::endl;
}
else {
std::cout << "numbers of even and odd elements don't match" << std::endl;
}
}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c13 c13.cpp
wannian07@wannian07-PC:~$ ./c13
1 2 7 7 6 3 9 5 7 7 7 3 6
three consecutive elements with value 7 start with 9. element
first four consecutive odd elements are: 3 9 5 7
coll2: 1 2 3 4 5 6 7 1 2 3 4 5 6 7
subcoll: 3 4 5 6
subcoll found starting with element 3
subcoll found starting with element 10
coll3: 1 2 3 4 5 6 7 8 9
subrange found starting with element 2
subrange found starting with element 4
subrange found starting with element 6
coll4: 1 2 3 4 5 6 7 1 2 3 4 5 6 7
subcoll2: 3 4 5 6
subcoll found starting with element 10
subcoll found starting with element 3
coll5: 1 2 3 4 5 6 7 8 9 10 11
searchcoll: 3 4 5
first element of searchcoll in coll5 is element 3
last element of searchcoll in coll5 is element 5
coll6: 1 3 2 4 5 5 0
first two elements with equal value have position 5
first two elements with second value twice the first have pos. 3
coll7: 1 2 3 4 5 6 7
coll8: 3 4 5 6 7 8 9
coll7 != coll8
even and odd elements correspond
coll11: 1 1 2 3 4 5 6 7 8 9
coll12: 1 9 8 7 6 5 4 3 2 1
coll13: 11 12 13 19 18 17 16 15 14 11
coll11 and coll12 have equal elements
numbers of even and odd elements match