Exercise using STL sort to sort containers

Source: Internet
Author: User

The main exercise is to use std: sort to sort std: vector, std: string, etc, you can also learn how to customize a sorting rule by specifying a custom sorting method or reloading the operator. [Cpp] # include <string> # include <vector> # include <algorithm> # include <functional> # include <iostream> struct Vec2D {float x; float y; Vec2D (): x (0.f), y (0.f) {} Vec2D (float fx, float fy): x (fx), y (fy) {}}; std :: ostream & operator <(std: ostream & out, const Vec2D & val) {return out <"<val. x <"," <val. y <")" ;}struct Vec3D {float x; float y; float z; Vec3D (): x (0.f), y (0.f), z (0.f ){} Vec3D (float fx, float fy, float fz): x (fx), y (fy), z (fz) {} float length_squared () const {return x * x + y * y + z * z;} float length () const {return sqrtf (length_squared ();} bool operator <(const Vec3D & rhs) {return length_squared () <rhs. length_squared () ;}}; std: ostream & operator <(std: ostream & out, const Vec3D & val) {return out <"(" <val. x <"," <val. y <"," <val. z <")";} templa Te <class T> void print_vector (const std: vector <T> & v) {using std: cout; using std: endl; for (std :: vector <T>: const_iterator iter = v. begin (); iter! = V. end (); ++ iter) {cout <(* iter) <"\ t" ;}cout <endl ;}// vector <int> sorting method, here, the static bool sort_comp_int (int elem1, int elem2) {return abs (elem1) <abs (elem2);} // vector <Vec2D> sorting method is sorted in ascending order of absolute values, here, sort by x from large to small. If x is the same, then sort by y from small to large static bool sort_comp_Vec2D (const Vec2D & elem1, const Vec2D & elem2) {if (elem1.x> elem2.x) return true; else if (elem1.x = elem2.x) return elem1.y <elem2.y; return false;} in T main (int argc, char ** argv) {using std: cout; using std: cin; using std: endl; cout <"========= sort std :: vector <int >================= "<endl; std: vector <int> vi; vi. push_back (3); vi. push_back (-4); vi. push_back (1); vi. push_back (5); cout <"before sort:" <endl <"\ t"; print_vector <int> (vi ); // use the default Sorting Algorithm (operator <) from small to large sorting std: sort (vi. begin (), vi. end (); cout <"sort default:" <endl <"\ t"; print _ Vector <int> (vi); // use the descending order algorithm std: sort (vi. begin (), vi. end (), std: greater <int> (); cout <"sort greater:" <endl <"\ t "; print_vector <int> (vi); // use the custom Sorting Algorithm std: sort (vi. begin (), vi. end (), sort_comp_int); cout <"sort user defined:" <endl <"\ t"; print_vector <int> (vi ); //////////////////////////////////////// /// // cout <"= ========= sort std:: string ============== ==== "<Endl; std: string s =" Hello, world! "; Cout <" before sort: "<endl <" \ t "; cout <s <endl; std: sort (s. begin (), s. end (); cout <"sort default:" <endl <"\ t"; cout <s <endl; //////////////////////////////////////// /// // cout <"= ========= sort std:: vector <Vec2D >=============== "<endl; std: vector <Vec2D> v2; v2.push _ back (Vec2D (3.f,-1.f)); v2.push _ back (Vec2D (1.f, 6.f)); v2.push _ back (Vec2D (3.f, 3.2f); v2.push _ Back (Vec2D (2.f, 0.f)); cout <"before sort:" <endl <"\ t"; print_vector <Vec2D> (v2 ); // use the custom Sorting Algorithm std: sort (v2.begin (), v2.end (), sort_comp_Vec2D); cout <"sort user defined: "<endl <" \ t "; print_vector <Vec2D> (v2 ); //////////////////////////////////////// /// // cout <"= ========= sort std:: vector <Vec3D >=============== "<endl; std: vector <Vec3D> v3; v3.push _ back (Vec3D (1.f, 0. f, 0.f)); v3.push _ back (Vec3D (1.f, 1.f, 1.f)); v3.push _ back (Vec3D (0.f, 2.f, 0.f)); v3.push _ back (Vec3D (0.f, 1.f, 1.f )); cout <"before sort:" <endl <"\ t"; print_vector <Vec3D> (v3); // The default sorting algorithm uses operator <as the sorting method std:: sort (v3.begin (), v3.end (); cout <"sort operator <:" <endl <"\ t"; print_vector <Vec3D> (v3 ); cin. get (); return EXIT_SUCCESS;} output result: [plain] ========== sort std :: vector <int >=========== ==== Before sort: 3-4 1 5 sort default:-4 1 3 5 sort greater: 5 3 1-4 sort user defined: 1 3-4 5 ========== sort std: string ==================== before sort: Hello, world! Sort default :!, Hdellloorw ========= sort std: vector <Vec2D >====================== before sort: (3, -1) (3.2) (3, 3.2) () sort user defined: www.2cto.com (3,-1) (3) ========= sort std: vector <Vec3D >====================== before sort, 0) (, 1) (, 0) (, 1) sort operator <: (, 0) (, 1) (, 1) (, 1, 0)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.