Non-Modifying sequence operations:
1. Non-modifying sequence operations: _of (CPP 11)
std::array all_of_elem = { 3,5,7,11,13,17,19,23 };
1. if (std::all_of(all_of_elem.begin(), all_of_elem.end(), [](int i) {return i % 2; }))
std::cout << "All the elements are odd numbers.\n";
std::array any_of_elem = { 0,1,-1,3,-3,5,-5 };
2. if (std::any_of(any_of_elem.begin(), any_of_elem.end(), [](int i) {return i<0 font="">))0>
std::cout << "There are negative elements in the range.\n";
std::array foo = { 1,2,4,8,16,32,64,128 };
3. if (std::none_of(foo.begin(), foo.end(), [](int i) {return i<0 font="">))0>
std::cout << "There are no negative elements in the range.\n";
2. Non-modifying sequence operations: find
std::string myints[] = { "Hello", "Hi", "Bye", "ByeBye" };
std::vector myvector(myints, myints + 4);
std::vector::iterator it;
//Find Without predicate comparison
1. it = find(myvector.begin(), myvector.end(), "Hi");
if (it != myvector.end())
std::cout << "Element found in myvector: " << *it << '\n';
else
std::cout << "Element not found in myvector\n";
2. std::vector::iterator iterator = std::find_if(myvector.begin(), myvector.end(), [=](std::string x) { return x == "Hi"; });
if(iterator != myvector.end())
std::cout << "find_if: Element found in myvector: " << *iterator << '\n';
else
std::cout << "find_if: Element not found in myvector\n";
(CPP 11)
3. std::vector::iterator iterator_not = std::find_if_not(myvector.begin(), myvector.end(), [=](std::string x) {return x.compare("ii") != 0;});
if (iterator_not != myvector.end())
std::cout << "find_if_not: Element found in myvector: " << *iterator_not << '\n';
else
std::cout << "find_if_not: Element not found in myvector\n";
//Find With predicate comparison
4. int sampleints[] = { 1,2,3,4,6,1,2,3,4,5 };
std::vector haystack(sampleints, sampleints + 10);
int needle1[] = { 1,2,3, 4 };
std::vector::iterator it_find_end;
// using default comparison:
it_find_end = std::find_end(haystack.begin(), haystack.end(), needle1, needle1 + 4);
if (it_find_end != haystack.end())
std::cout << "find_end: needle1 last found at position " << (it_find_end - haystack.begin()) << '\n';
else
std::cout << "find_end: needle1 last found at position " << '\n';
//Using predicate comparison :
int needle2[] = { 3,4,5};
it_find_end = std::find_end(haystack.begin(), haystack.end(), needle2, needle2 + 3, myfunction);
if (it_find_end != haystack.end())
std::cout << "find_end: needle2 last found at position " << (it_find_end - haystack.begin()) << '\n';
else
std::cout << "find_end: needle2 last found at position " << '\n';
int mychars[] = { 'a','b','c','A','B','C' };
std::vector myystack(mychars, mychars + 6);
std::vector::iterator it_find_first_of;
int needle[] = { 'A','B','C' };
// using default comparison:
5. it_find_first_of = find_first_of(myystack.begin(), myystack.end(), needle, needle + 3);
if (it_find_first_of != myystack.end())
std::cout << "find_first_of: The first match is: " << *it_find_first_of << '\n';
else
std::cout << "find_first_of: The first match is: NONE"<< '\n';
// using predicate comparison:
it_find_first_of = find_first_of(myystack.begin(), myystack.end(),
needle, needle + 3, comp_case_insensitive);
if (it_find_first_of != myystack.end())
std::cout << "find_first_of: The first match is: " << *it_find_first_of << '\n';
else
std::cout << "find_first_of: The first match is: NONE" << '\n';
int myAdjacentints[] = { 5,20,5,30,30,20,10,10,20 };
std::vector myAdjacentVector(myAdjacentints, myAdjacentints + 8);
std::vector::iterator it_adjacent_find;
// using default comparison:
6. it_adjacent_find = std::adjacent_find(myAdjacentVector.begin(), myAdjacentVector.end());
if (it_adjacent_find != myAdjacentVector.end())
std::cout << "the first pair of repeated elements are: " << *it_adjacent_find << '\n';
//using predicate comparison:
it_adjacent_find = std::adjacent_find(++it_adjacent_find, myAdjacentVector.end(), myfunction);
if (it_adjacent_find != myAdjacentVector.end())
std::cout << "the second pair of repeated elements are: " << *it_adjacent_find << '\n';
1. Non-modifying sequence operations: _of (CPP 11)
std::array
1. if (std::all_of(all_of_elem.begin(), all_of_elem.end(), [](int i) {return i % 2; }))
std::cout << "All the elements are odd numbers.\n";
std::array
2. if (std::any_of(any_of_elem.begin(), any_of_elem.end(), [](int i) {return i<0 font="">))0>
std::cout << "There are negative elements in the range.\n";
std::array
3. if (std::none_of(foo.begin(), foo.end(), [](int i) {return i<0 font="">))0>
std::cout << "There are no negative elements in the range.\n";
2. Non-modifying sequence operations: find
std::string myints[] = { "Hello", "Hi", "Bye", "ByeBye" };
std::vector
std::vector
//Find Without predicate comparison
1. it = find(myvector.begin(), myvector.end(), "Hi");
if (it != myvector.end())
std::cout << "Element found in myvector: " << *it << '\n';
else
std::cout << "Element not found in myvector\n";
2. std::vector
if(iterator != myvector.end())
std::cout << "find_if: Element found in myvector: " << *iterator << '\n';
else
std::cout << "find_if: Element not found in myvector\n";
(CPP 11)
3. std::vector
if (iterator_not != myvector.end())
std::cout << "find_if_not: Element found in myvector: " << *iterator_not << '\n';
else
std::cout << "find_if_not: Element not found in myvector\n";
//Find With predicate comparison
4. int sampleints[] = { 1,2,3,4,6,1,2,3,4,5 };
std::vector
int needle1[] = { 1,2,3, 4 };
std::vector
// using default comparison:
it_find_end = std::find_end(haystack.begin(), haystack.end(), needle1, needle1 + 4);
if (it_find_end != haystack.end())
std::cout << "find_end: needle1 last found at position " << (it_find_end - haystack.begin()) << '\n';
else
std::cout << "find_end: needle1 last found at position " << '\n';
//Using predicate comparison :
int needle2[] = { 3,4,5};
it_find_end = std::find_end(haystack.begin(), haystack.end(), needle2, needle2 + 3, myfunction);
if (it_find_end != haystack.end())
std::cout << "find_end: needle2 last found at position " << (it_find_end - haystack.begin()) << '\n';
else
std::cout << "find_end: needle2 last found at position " << '\n';
int mychars[] = { 'a','b','c','A','B','C' };
std::vector
std::vector
int needle[] = { 'A','B','C' };
// using default comparison:
5. it_find_first_of = find_first_of(myystack.begin(), myystack.end(), needle, needle + 3);
if (it_find_first_of != myystack.end())
std::cout << "find_first_of: The first match is: " << *it_find_first_of << '\n';
else
std::cout << "find_first_of: The first match is: NONE"<< '\n';
// using predicate comparison:
it_find_first_of = find_first_of(myystack.begin(), myystack.end(),
needle, needle + 3, comp_case_insensitive);
if (it_find_first_of != myystack.end())
std::cout << "find_first_of: The first match is: " << *it_find_first_of << '\n';
else
std::cout << "find_first_of: The first match is: NONE" << '\n';
int myAdjacentints[] = { 5,20,5,30,30,20,10,10,20 };
std::vector
std::vector
// using default comparison:
6. it_adjacent_find = std::adjacent_find(myAdjacentVector.begin(), myAdjacentVector.end());
if (it_adjacent_find != myAdjacentVector.end())
std::cout << "the first pair of repeated elements are: " << *it_adjacent_find << '\n';
//using predicate comparison:
it_adjacent_find = std::adjacent_find(++it_adjacent_find, myAdjacentVector.end(), myfunction);
if (it_adjacent_find != myAdjacentVector.end())
std::cout << "the second pair of repeated elements are: " << *it_adjacent_find << '\n';
Comments
Post a Comment