Sorting vector using std::sort - C++ - Source...

来源:百度文库 编辑:神马文学网 时间:2024/03/29 18:09:06
C++ Vector Sort

Join 409,116 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,486 people online right now.Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert

Sorting vector using std::sort

Submitted By: Xing Actions:
Rating:
Views: 32,853

Language: C++

Last Modified: December 9, 2006

Snippet


  1. #include
  2. #include
  3. #include
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9.     int val, count = 0;
  10.     vector vec;
  11.     cout << "Enter integers, after each, Z to finish:" << endl;
  12.  
  13.     //Validating input and saving in vector container
  14.     while( cin >> val, cin.good() ) {
  15.       vec.push_back( val );
  16.       ++count;
  17.     }
  18.  
  19.     if ( count ) {
  20.  
  21.       //Sorting vector
  22.       sort( vec.begin(), vec.end() );
  23.  
  24.       //Displaying data
  25.       cout<<"Sorted Vector: ";
  26.       for (vector::const_iterator it=vec.begin(); it!=vec.end(); ++it)
  27.         cout << *it << " ";
  28.       cout << endl;
  29.     }
  30.     return 0;
  31. }

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to to leave comments.