STL Algorithm cheetsheet-I
This is only for notedown syntax, how to use particuler algorithm.
This has taken referance from www.cplusplus.com
for_each : Apply function to range
void myforeach(int& i){i=rand();}
void testforeach()
{
vector< int > intvec(10);
copy(intvec.begin(),intvec.end(),ostream_iterator< int >(cout,",")); cout << std::endl;
for_each(intvec.begin(),intvec.end(),&myforeach);
copy(intvec.begin(),intvec.end(),ostream_iterator< int >(cout,",")); cout << std::endl;
}
find : Find value in range
void myforeach(int& m){static int i=0;m=++i;}
void testfind()
{
vector< int > intvec(10);
for_each(intvec.begin(),intvec.end(),&myforeach);
copy(intvec.begin(),intvec.end(),ostream_iterator< int >(cout,","));cout<::iterator it=find(intvec.begin(),intvec.end(),11);
if(it!=intvec.end()) cout << "Value found\n";
else cout << "Value not found\n";
}
find_if : Find element in range
Returns an iterator to the first element in the range [first,last) for which applying pred to it, is true.
void myforeach(int& m){static int i=0;m=++i;}
bool IsOdd (int i) { return ((i%2)==1);}
void testfindif()
{
vectorcount :Count appearances of value in range
Returns the number of elements in the range [first,last) that compare equal to value
void myforeach(int& m){static int i=0;m=(++i)%2;}
void testcount()
{
vector< int > intvec(10);
for_each(intvec.begin(),intvec.end(),&myforeach);
copy(intvec.begin(),intvec.end(),ostream_iterator< int >(cout,","));cout << std::endl;
int iCount=(int)count(intvec.begin(),intvec.end(),1);
cout << "Number of element count " << iCount << std::endl;
}
count_if : Return number of elements in range satisfying condition
Returns the number of elements in the range [first,last) for which condition pred is true.
void myforeach(int& m){static int i=0;m=(++i);}
bool IsOdd (int i) { return ((i%2)==1);}
void testcount()
{
vector< int > intvec(10);
for_each(intvec.begin(),intvec.end(),&myforeach);
copy(intvec.begin(),intvec.end(),ostream_iterator< int >(cout,","));cout << std::endl;
int iCount=(int)count_if(intvec.begin(),intvec.end(),&IsOdd);
cout << "Number of element count " << iCount << std::endl;
}
transform : Apply function to range
The first version applies op to all the elements in the input range ([first1,last1)) and stores each returned value in the range beginning at result.
The second version uses as argument for each call to binary_op one element from the first input range ([first1,last1)) and one element from the second input range (beginning at first2).
transform (input.begin(), input.end(), result.begin(), unaryfun);
transform (input1.begin(), input1.end(), input2.begin(), result.begin(), binaryfun);
copy : Copy range of elements
Copies the elements in the range [first,last) into a range beginning at result.
copy(input.begin(), input.end(), result.begin());
copy(input.begin(),input.end(),ostream_iteratorswap : Exchange values of two objects
Assigns the content of a to b and the content of b to a.
//Exchange values of two objects
swap(avec,bvecc);
/* swap_ranges Exchange values of two ranges
Swaps the values of each of the elements in the range [first1,last1) with those of their respective elements in the range beginning at first2.
*/
swap_ranges(first.begin(), first.end(), second.begin());
fill : Fill range with value
Sets value to all elements in the range [first,last).
// scaler value fill
fill(first.begin(), first.end(), value);
// scaler value to range n element
fill_n((first.begin(), nelem, value);
generate : Generate values for range with function
Sets value to all elements in the range [first,last).
int uid(){static int uid=rand()%10; return uid+=(rand()%10);}
void testgen()
{
vectorremove : Remove value from range
Compares the elements in the range [first,last) against value, and removes those that compare equal from the resulting range. The resulting range consists of the elements between first and the iterator returned by the function, which points to the new end of the range.
int uid(){static int i=0; return ++i;}// unary function
bool IsOdd(int &i){return ((i%2)==1);}// predicate
void testremove()
{
vector
No comments:
Post a Comment
would you like it. :)