ccplusplus.com
Learn C, C++ Concepts
Sunday, December 4, 2011
stl algorithms example
/*********************************************************************** * File : algo1.cpp * Author : Saurabh Gupta * Desc : stl algorithms in c++ * Source : http://saurabhgupta0527.blogspot.com/2011/10/standard-template-library.html * Created : AM 11:36 04 December 2011 * Note : ************************************************************************/ #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector <int> coll; vector <int>::iterator pos; /* * insert elements from 1 to 6 in arbitrary order */ coll.push_back(2); coll.push_back(5); coll.push_back(4); coll.push_back(1); coll.push_back(6); coll.push_back(3); /* * find and print minimum and maximum elements */ pos = min_element (coll.begin(), coll.end()); cout << "min: " << *pos << endl; pos = max_element (coll.begin(), coll.end()); cout << "max: " << *pos << endl; /* * sort all elements */ sort (coll.begin(), coll.end()); /* * find the first element with value 3 */ pos = find (coll.begin(), coll.end(), //range 3); //value /* * reverse the order of the found element with value 3 and all following elements */ reverse (pos, coll.end()); /* * print all elements */ for (pos=coll.begin(); pos!=coll.end(); ++pos) { cout << *pos << ' ' ; } cout << endl; } /* * OUTPUT * [sgupta@rhel6x64 stl]$ c++ algo1.cpp -o algo1 [sgupta@rhel6x64 stl]$ ./algo1 min: 1 max: 6 1 2 6 5 4 3 [sgupta@rhel6x64 stl]$ */
See Also:
Standard Template Library Concepts and sample codes in C++
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment