3. Non-Modifying sequence operations: search
std::vector vecStack;
int vecNeedle1[] = { 40,50,60,70 };
int vecNeedle2[] = { 20,30,50 };
std::vector::iterator it_search;
// vecStack: 10 20 30 40 50 60 70 80 90
for (int i = 1; i<10 10="" font="" i="" vecstack.push_back="">10>
// using default comparison:
1. it_search = std::search(vecStack.begin(), vecStack.end(), vecNeedle1, vecNeedle1 + 4);
if (it_search != vecStack.end())
std::cout << "search: needle1 found at position " << (it_search - vecStack.begin()) << '\n';
else
std::cout << "search : needle1 not found\n";
// using predicate comparison:
auto val = vecNeedle2 + 3;
it_search = std::search(vecStack.begin(), vecStack.end(), vecNeedle2, vecNeedle2 + 3, myfunction);
if (it_search != vecStack.end())
std::cout << "search: needle2 found at position " << (it_search - vecStack.begin()) << '\n';
else
std::cout << "search: needle2 not found\n";
int mySearchnInts[] = { 10,20,30,30,20,10,10,20 };
std::vector vecStackSearchn(mySearchnInts, mySearchnInts + 8);
// using default comparison:
2. it_search = std::search_n(vecStackSearchn.begin(), vecStackSearchn.end(), 2, 30);
if (it_search != vecStackSearchn.end())
std::cout << "search_n: two 30s found at position " << (it_search - vecStackSearchn.begin()) << '\n';
else
std::cout << "search_n: match not found\n";
// using predicate comparison:
it_search = std::search_n(vecStackSearchn.begin(), vecStackSearchn.end(), 2, 10, myfunction);
if (it_search != vecStackSearchn.end())
std::cout << "search_n: two 10s found at position " << int(it_search - vecStackSearchn.begin()) << '\n';
else
std::cout << "search_n: match not found\n";
4. Non-Modifying sequence operations: count
1. int mycount = std::count(vecStackSearchn.begin(), vecStackSearchn.end(), 10);
std::cout << "count: 10 appears " << mycount << " times.\n";
2. int count = count_if(myAdjacentVector.begin(), myAdjacentVector.end(), [=](int x) { return ((x % 2) == 1); });
std::cout << "count_if: myvector contains " << count << " odd values.\n";
5. Non-Modifying sequence operations: for_each
for_each(myAdjacentVector.begin(), myAdjacentVector.end(), [=](int x) { std::cout << x <<" ,"; });
std::cout << '\n';
//is_permutation
std::array Array1 = { 1,2,3,4,5 };
std::array Array2 = { 3,1,4,5,2 };
6. Non-Modifying sequence operations: is_permutation (Cpp 11)
if (std::is_permutation(Array2.begin(), Array2.end(), Array1.begin()))
std::cout << "is_permutation: Array1 and Array2 contain the same elements.\n";
std::vector
int vecNeedle1[] = { 40,50,60,70 };
int vecNeedle2[] = { 20,30,50 };
std::vector
// vecStack: 10 20 30 40 50 60 70 80 90
for (int i = 1; i<10 10="" font="" i="" vecstack.push_back="">10>
// using default comparison:
1. it_search = std::search(vecStack.begin(), vecStack.end(), vecNeedle1, vecNeedle1 + 4);
if (it_search != vecStack.end())
std::cout << "search: needle1 found at position " << (it_search - vecStack.begin()) << '\n';
else
std::cout << "search : needle1 not found\n";
// using predicate comparison:
auto val = vecNeedle2 + 3;
it_search = std::search(vecStack.begin(), vecStack.end(), vecNeedle2, vecNeedle2 + 3, myfunction);
if (it_search != vecStack.end())
std::cout << "search: needle2 found at position " << (it_search - vecStack.begin()) << '\n';
else
std::cout << "search: needle2 not found\n";
int mySearchnInts[] = { 10,20,30,30,20,10,10,20 };
std::vector
// using default comparison:
2. it_search = std::search_n(vecStackSearchn.begin(), vecStackSearchn.end(), 2, 30);
if (it_search != vecStackSearchn.end())
std::cout << "search_n: two 30s found at position " << (it_search - vecStackSearchn.begin()) << '\n';
else
std::cout << "search_n: match not found\n";
// using predicate comparison:
it_search = std::search_n(vecStackSearchn.begin(), vecStackSearchn.end(), 2, 10, myfunction);
if (it_search != vecStackSearchn.end())
std::cout << "search_n: two 10s found at position " << int(it_search - vecStackSearchn.begin()) << '\n';
else
std::cout << "search_n: match not found\n";
4. Non-Modifying sequence operations: count
1. int mycount = std::count(vecStackSearchn.begin(), vecStackSearchn.end(), 10);
std::cout << "count: 10 appears " << mycount << " times.\n";
2. int count = count_if(myAdjacentVector.begin(), myAdjacentVector.end(), [=](int x) { return ((x % 2) == 1); });
std::cout << "count_if: myvector contains " << count << " odd values.\n";
5. Non-Modifying sequence operations: for_each
for_each(myAdjacentVector.begin(), myAdjacentVector.end(), [=](int x) { std::cout << x <<" ,"; });
std::cout << '\n';
//is_permutation
std::array
std::array
6. Non-Modifying sequence operations: is_permutation (Cpp 11)
if (std::is_permutation(Array2.begin(), Array2.end(), Array1.begin()))
std::cout << "is_permutation: Array1 and Array2 contain the same elements.\n";
Comments
Post a Comment