C++ STL

########################################### 
###   STL (standard template library)   ### 
########################################### 
 
STL consists of 3 components: 
 
1. algorithms 
2. containers 
3. iterators 
 
 
############################ 
###    STL containers    ### 
############################ 
 
e.g. 
std::vector 
std::set 
std::multiset 
 
 
(ref) https://www.geeksforgeeks.org/multiset-in-cpp-stl/ 
 
 
### 
###  custom comparator 
### 
 
super useful 
 
(ref) http://neutrofoton.github.io/blog/2016/12/30/c-plus-plus-set-with-custom-comparator/ 
 
 
 
#################################### 
####    predicate  (English)    #### 
#################################### 
 
predicate - affirm, declare, found, base 
 
e.g.  his clames are predicated upoin scientific evidence. 
 
 
################################ 
####    prediacate (C++)    #### 
################################ 
 
very important and commonly used in STL algo set. 
 
predicate usually is one of the below four, that examines input[s] and returns a bool. 
 
(1) a function object                                //  a class that overloads operator() 
(2) class member func that overloads operator other than (), like operator<  // less than check 
(3) global operator overload 
(4) a function pointer to some global function       // called "predicate function" commonly used in C as callback 
                                                     // we can use this as class mem mfunc but ONLY as static 
 
a typical example is it is used as one of the paramters for a sort() function. 
 
e.g.  std::sort(v.begin(), v.end(), lessThan() );    //  functor overloading operator() 
 
 
----------   Here is the C++ standard (quote)  ------------ 
 
The Predicate parameter is used whenever an algorithm expects a function object that when applied to the result of dereferencing the corresponding iterator returns a value testable as true. In other words, if an algorithm takes Predicate pred as its argument and first as its iterator argument, it should work correctly in the construct if (pred(*first)){...}. The function object pred shall not apply any non-constant function through the dereferenced iterator. This function object may be a pointer to function, or an object of a type with an appropriate function call operator. 
 
----------------------------------------------------------- 
 
 
 
one advantageous use of functor over callback func pointer is functor can be a type in template. 
 
e.g. 
----------------------------------------------- 
 
template<typename T> class Functor 

   public: 
     bool operator()(T t); 
}; 
 
template<typename F> void func(F f) 

   int i = 0; 
   f(i);           // functor 

 
----------------------------------------------- 
 
 
(ref) 
http://stackoverflow.com/questions/5921609/what-is-predicate-in-c 
http://en.cppreference.com/w/cpp/concept/Predicate 
http://homepage2.nifty.com/well/sort.html 
http://d.hatena.ne.jp/cou929_la/20091024/1256352709 
 
 
 
################################### 
####      std::accumulate      #### 
################################### 
 
- part of <numeric> lib. 
- adds all elems up 
- notice the use of the third arg. 
 
 
(ref) 
http://kowaimononantenai.blogspot.jp/2013/11/stdaccumulatevectorstdaccumulate3.html 
 
----------------------------------------//  accum.cpp 
#include <iostream> 
#include <vector> 
#include <numeric> 
 
int main(){ 
  std::vector<double> vec; 
  for (double i = 0.3; i < 5 ; i++){ 
    vec.push_back(i);                  // 0.3 + 1.3 + 2.3 + 3.3 + 4.3 = 11.5 
  } 
 
  std::cout  <<  std::accumulate(vec.begin(), vec.end(), 0 )       << std::endl;    //  10 
  std::cout  <<  std::accumulate(vec.begin(), vec.end(), 0.0 )     << std::endl;    //  11.5 
  std::cout  <<  std::accumulate(vec.begin(), vec.end(), 1000.0 )  << std::endl;    //  1011.5 
 
  return 0; 

 
----------------------------------------

  1. 2014-04-08 22:57:48 |
  2. Category : cpp
  3. Page View:

Google Ads