Category Archives: C++ । সি প্লাস প্লাস

Print minimum, maximum, and sum of the valarray

/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <valarray>
using namespace std;
 
// print valarray
template <class T>
void printValarray (const valarray<T>& va)
{
    for (int i=0; i<va.size(); i++) {
        cout << va[i] << ' ';
    }
    cout << endl;
}
 
int main()
{
    // define two valarrays with ten elements
    valarray<double> va1(10), va2(10);
 
    // assign values 0.0, 1.1, up to 9.9 to the first valarray
    for (int i=0; i<10; i++) {
        va1[i] = i * 1.1;
    }
 
    // assign -1 to all elements of the second valarray
    va2 = -1;
 
    // print both valarrays
    printValarray(va1);
    printValarray(va2);
 
    // print minimum, maximum, and sum of the first valarray
    cout << "min(): " << va1.min() << endl;
    cout << "max(): " << va1.max() << endl;
    cout << "sum(): " << va1.sum() << endl;
 
    // assign values of the first to the second valarray
    va2 = va1;
 
    // remove all elements of the first valarray
    va1.resize(0);
 
    // print both valarrays again
    printValarray(va1);
    printValarray(va2);
}
 
/*
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
min(): 0
max(): 9.9
sum(): 49.5
 
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
 
 */

valarray slice

/* The following code example is taken from the book * "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */

#include <iostream>
#include <valarray>
using namespace std; 

// print valarray line-by-line
template<class T>
void printValarray (const valarray<T>& va, int num)
{
   for (int i=0; i<va.size()/num; ++i) {
       for (int j=0; j<num; ++j) {
           cout << va[i*num+j] << ' ';
       }
     cout << endl;
   }
   cout << endl;
} 

int main()
{
   /* valarray with 12 elements
     * - four rows
     * - three columns
     */
   valarray<double> va(12); 

   // fill valarray with values
   for (int i=0; i<12; i++) {
       va[i] = i;
   } 

   printValarray (va, 3);
   // first column = second column raised to the third column
   va[slice(0,4,3)] = pow (valarray<double>(va[slice(1,4,3)]),
                           valarray<double>(va[slice(2,4,3)])); 

   printValarray (va, 3); 
   // create valarray with three times the third element of va
   valarray<double> vb(va[slice(2,4,0)]);
 
   // multiply the third column by the elements of vb
   va[slice(2,4,3)] *= vb;
   printValarray (va, 3); 
   // print the square root of the elements in the second row
   //printValarray (sqrt(valarray<double>(va[slice(3,3,1)])));
   // double the elements in the third row
   va[slice(2,4,3)] = valarray<double>(va[slice(2,4,3)]) * 2.0;
   printValarray (va, 3);
}
/*
0 1 2
3 4 5
6 7 8
9 10 11
1 1 2
1024 4 5
5.7648e+006 7 8
1e+011 10 11
1 1 4
1024 4 10
5.7648e+006 7 16
1e+011 10 22
1 1 8
1024 4 20
5.7648e+006 7 32
1e+011 10 44
*/

 

Call member function for each element in vector

/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
//#define mem_fun1 mem_fun
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
class Person {
private:
   std::string name;
public:
   //...
   void print () const {
       std::cout << name << std::endl;
   }
   void printWithPrefix (std::string prefix) const {
       std::cout << prefix << name << std::endl;
   }
};
void foo (const std::vector<Person>& coll)
{
   using std::for_each;
   using std::bind2nd;
   using std::mem_fun_ref;
   // call member function print() for each element
   for_each (coll.begin(), coll.end(), mem_fun_ref(&Person::print));
   // call member function printWithPrefix() for each element
   // - "person: " is passed as an argument to the member function
   for_each (coll.begin(), coll.end(),bind2nd(mem_fun_ref(&Person::printWithPrefix),"person: "));
}

void ptrfoo (const std::vector<Person*>& coll)
                                   // ^^^ pointer !
{
   using std::for_each;
   using std::bind2nd;
   using std::mem_fun;
   // call member function print() for each referred object
   for_each (coll.begin(), coll.end(),
             mem_fun(&Person::print));
   // call member function printWithPrefix() for each referred object
   // - "person: " is passed as an argument to the member function
   for_each (coll.begin(), coll.end(),bind2nd(mem_fun(&Person::printWithPrefix),"person: "));
} 
 
int main()
{
   std::vector<Person> coll(5);
   foo(coll); 
   std::vector<Person*> coll2;
   coll2.push_back(new Person);
   ptrfoo(coll2);
} 
/*
person:
person:
person:
person:
person:
person: 

*/


	

Use bitset with enum together

/* The following code example is taken from the book
  * "The C++ Standard Library - A Tutorial and Reference"
  * by Nicolai M. Josuttis, Addison-Wesley, 1999
  *
  * (C) Copyright Nicolai M. Josuttis 1999.
  * Permission to copy, use, modify, sell and distribute this software
  * is granted provided this copyright notice appears in all copies.
  * This software is provided "as is" without express or implied
  * warranty, and with no claim as to its suitability for any purpose.
  */
 #include <bitset>
 #include <iostream>
 using namespace std;
 
 int main()
 {
     /* enumeration type for the bits
      * - each bit represents a color
      */
     enum Color { red, yellow, green, blue, white, black, //...,
                  numColors };
 
     // create bitset for all bits/colors
     bitset<numColors> usedColors;
 
     // set bits for two colors
     usedColors.set(red);
     usedColors.set(blue);
 
     // print some bitset data
     cout << "bitfield of used colors:   " << usedColors
          << endl;
     cout << "number   of used colors:   " << usedColors.count()
          << endl;
     cout << "bitfield of unused colors: " << ~usedColors
          << endl;
 
     // if any color is used
     if (usedColors.any()) {
         // loop over all colors
         for (int c = 0; c < numColors; ++c) {
             // if the actual color is used
             if (usedColors[(Color)c]) {
                 //...
             }
         }
     }
 }
 
 /* 
 bitfield of used colors:   001001
 number   of used colors:   2
 bitfield of unused colors: 110110
 
  */