ccplusplus.com
Learn C, C++ Concepts
Wednesday, December 7, 2011
min and max in array using stl
/*********************************************************************** * File : minimum-max-in-array-stl.cpp * Author : Saurabh Gupta * Desc : minimum and maximum in array using stl * Source : http://saurabhgupta0527.blogspot.com/2011/10/standard-template-library.html * http://saurabhgupta0527.blogspot.com/p/c_15.html * Created : PM 03:12 04 December 2011 * Note : ************************************************************************/ #include
#include
#include
using namespace std; int main() { vector
coll; vector
::iterator pos; /* * insert elements from 1 to 6 in arbitrary order */ int nArraySize, nElement; cout << "Enter the size of the array ?" << endl; cin >> nArraySize; for (int i = 0; i < nArraySize; i++) { cout << "Enter element [" << i << "]" << endl; cin >> nElement; coll.push_back(nElement); } /* * 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++ -o minimum-max-in-array-stl minimum-max-in-array-stl.cpp [sgupta@rhel6x64 stl]$ ./minimum-max-in-array-stl Enter the size of the array ? 5 Enter element [0] 4 Enter element [1] 1 Enter element [2] 6 Enter element [3] 10 Enter element [4] 5 min: 1 max: 10 1 4 5 6 10 [sgupta@rhel6x64 stl]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment