Skip to main content

Part2 : STL Algorithms (Non-Modifying sequence) in c++

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="">


// 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";





Comments

Popular posts from this blog

const used with functions

const used with functions : class Dog {    int age;    string name; public:    Dog() { age = 3; name = "dummy"; }         // const parameters and these are overloaded functions    void setAge(const int& a) { age = a; }    void setAge(int& a) { age = a; }         // Const return value    const string& getName() {return name;}         // const function and these are overloaded functions    void printDogName() const { cout << name << "const" << endl; }  // value of name can't be modified    void printDogName() { cout << getName() << " non-const" << endl; } }; int main() {    Dog d;    d.printDogName();        const Dog d2;    d2.printDogName();     }

Structured Bindings

Returning multiple Values from function C++ 11 vs C++ 17 Returning compound objects Iterating over a compound collection Direct initialization Returning multiple Values from function C++ 11 vs C++ 17 :  C++ 11 (std::tie): std::tuple mytuple() {     char a = 'a';     int i = 123;     bool b = true;     return std::make_tuple(a, i, b);  // packing variable into tuple } To access return value using C++ 11, we would need something like: char a; int i; bool b; std::tie(a, i, b) = mytuple();  // unpacking tuple into variables Where the variables have to be defined before use and the types known in advance. C++ 17 : auto [a, i, b] = mytuple(); Returning compound objects :  This is the easy way to assign the individual parts of a compound type (such as a struct, pair etc) to different variables all in one go – and have the correct types automatically assigned. So let’s have a look at an ...

POST 1 : std::thread in c++ | C++ 11

std::thread   C++  Thread support library std::thread. Threads enable programs to execute across several processor cores. Defined in header Class : thread Member types : native_handle_type Data Member  id    //represents the id of a thread (public member class) Member functions (constructor)    //constructs new thread object (public member function) (destructor)    //destructs the thread object, underlying thread must be joined or detached  (public member function) operator=    //moves the thread object  Observers  // Covered in NEXT POST Operations    // Covered in NEXT POST Constructor :   #include   void f1 ( int n ) { for ( int i = 0 ; i < 5 ; ++ i ) { std:: cout << "Thread 1 executing \n " ; ++ n ; std:: this_thread :: sleep_for ( std:: chrono :: milliseconds ( 10 ) ) ; ...