Valarray Container in c++ language , what is c++ valarray efficiency in hindi

इससे पहले के आर्टिकल मे , STL (Standard Template Library) के basic को discuss किया है जिसमे तीन मुख्य component होते है :- (1) container (2) alogorithm (3)Iterators | Containers का algorithm या set of command है जिसका use किसी प्रकार के टाइप के दो या दो से अधिक objects के collection को manage करने के लिए किया जाता है | C++ STL (Standard Template Library) मे निन्म मुख्य containers होता है :- deque, list, vector, map ,valarray आदि | इस article मे valarray के basic को कुछ predefine functions को discuss करेगे |

std:: valarray class in C++

C++STL (Standard Template Library) मे array के सामान दो प्रकार के container को introduce किया गया है |

(1) Vector

vector एक container है जिसका structure array की तरह होता है | इसमें  मुख्य system oriented activity जैसे sorting , insertion , rearrangement , searching को perform किया जा सकता  है | vector को सार से बाकि के article मई discuss करेगे |

(2)Valarray

Valarray भी एक special container हैजिसका structure array की तरह  ही होता है | इसमें और vectorमे एक ही difference होता है :

valarray मे सभी mathematical operations को array पर efficiently apply किया जा सकता है | लेकिन vector मे कुछ basic operation जैसे sorting , insertion , rearrangement , searching , swaping और sizing आदि को perform किया जा सकता है |

valarray element-wise mathematical operations  और  generalized subscript operators जैसे slicing and indirect access को भी support करता है |

और vector से ज्यादा , valarray से container array पर operation को efficiently किया जाता है | और compleaity भी काफी कम होती है |

इसमें निन्म public functions होता है :-

1. apply() :-

इस function का use किसी valarray elements पर इस function मई pass किये गये manipulation को apply करता है | और इस function से new valarray with manipulated values return होता है |

2. sum() :- इस function का use , किसी valarray container के सभी element के sum को find out करने के लिए किया जाता है |

इन दोनों ही function के लिए combine उदहारण नींम है :-

// C++ Programe to demonstrate apply() and sum()

#include<iostream>

#include<valarray> // for valarray functions

using namespace std;

int main()

{

// Declareing and Initializing valarray

valarray<int> val_array = { 10, 2, 20, 1, 30 };

// Declaring new valarray

valarray<int> val_array1 ;

// Using apply() to increment all elements by 5

val_array1 = val_array.apply([](int a){return a=a+5;});

// Displaying new elements value

cout << “Manipulated Valarray : “;

for (int &a: val_array1) cout << a << ” “;

cout << endl;

// Displaying sum of both old and new valarray

cout << “The sum of old valarray : “;

cout << val_array.sum() << endl;

cout << “The sum of new valarray : “;

cout << val_array1.sum() << endl;

getch();

}

इस उदाहरन मे ,

val_array1 = val_array.apply([](int a){return a=a+5;})statement से val_array के सभी element पर a=a+5;operation को apply किया गया है | और इस ऑपरेशन के बाद val_array के सभी element को val_array1 मई असाइन कर दिया जाता है |

इसके अलावा , val_array.sum() से ,  val_array के सभी element के sum को calculate किया जाता है |

Output:

Manipulated Valarray : 15 7 25 6 35

The sum of old valarray : 63

The sum of new valarray : 88

3. min() :- इस फंक्शन का use का , किसी valarray container के सभी element मे से minimum element को find out करने के लिए किया जाता है |

4. maa() :- इस फंक्शन का use का , किसी valarray container के सभी element मे से maximum element को find out करने के लिए किया जाता है |

// C++ code to demonstrate the working of

// maa() and min()

#include<iostream>

#include<valarray> // for valarray functions

using namespace std;

int main()

{

// Initializing valarray

valarray<int> val_array = { 10, 2, 20, 1, 30 };

// Displaying Maximum element of valarray

cout << “The Maximum  element of valarray : “;

cout << val_array.maa() << endl;

// Displaying Minimum element of valarray

cout << “The Minimum element of valarray : “;

cout << val_array.min() << endl;

getch();

}

Output:

The Maximum element of valarray : 30

The Minimum element of valarray : 1

7. swap() :- इस function का use , किसी  valarray container के सभी element को दुसरे valarray container के element से swap करने के लिए किया जाता है |

// C++ code to demonstrate of swap()

#include<iostream>

#include<valarray> // for valarray functions

using namespace std;

int main()

{

valarray<int> val_array1 = {1, 2, 3, 4};

valarray<int> val_array2 = {2, 4, 6, 8};

// Displaying valarrays before swapping

cout << “The contents of val_array1 before swapping : “;

for (int &a : val_array1)

cout << a << ” “;

cout << endl;

cout << “The contents of val_array2 before swapping : “;

for (int &a : val_array2)

cout << a << ” “;

cout << endl;

// Use of swap() to swap the valarrays

val_array1.swap(val_array2);

// Displaying valarrays after swapping

cout << “The contents of val_array1 after swapping : “;

for (int &a : val_array1)

cout << a << ” “;

cout << endl;

cout << “The contents of val_array2 after swapping : “;

for (int &a : val_array2)

cout << a << ” “;

cout << endl;

}

Output:

The contents of val_array1 before swapping : 1 2 3 4

The contents of val_array2 before swapping : 2 4 6 8

The contents of val_array1 after swapping  : 2 4 6 8

The contents of val_array2 after swapping  : 1 2 3 4