How does the set container of STL store struct? Sort by what?

Source: Internet
Author: User

BelowProgramWhat is the output result? Why?

# Include <iostream> # include <vector> # include <set> using namespace STD; struct node {char ch; int cost; node () {CH = '\ 0 '; cost = 0;} // commented out this function, without any impact on the entire program bool operator = (const node & RHs) const {return RHS. ch = CH;} // If you annotate this program, a bunch of errors will occur, related to errors in STL, is a bool operator that cannot be debugged or modified <(const node & RHs) const {return RHS. cost> cost; }}; int main (INT argc, char * argv []) {node A, B; Multiset <node> MS; Ms. insert (a); Ms. insert (B); set <node> S; S. insert (a); S. insert (B); cout <"ms. size () = "<Ms. size () <"\ ns. size () = "<S. size () <Endl; // although we have redefined the = function, this is useless. Set only uses the <symbol to judge Ms. clear (); S. clear ();. ch = 'a'; B. ch = 'B'; S. insert (a); S. insert (B); Ms. insert (a); Ms. insert (B); cout <"ms. size () = "<Ms. size () <"\ ns. size () = "<S. size () <Endl; Ms. clear (); S. clear ();. ch = 'a'; B. ch = 'a';. cost = 2; B. cost = 1; S. insert (a); S. insert (B); Ms. insert (a); Ms. insert (B); cout <"ms. size () = "<Ms. size () <"\ ns. size () = "<S. size () <Endl; // You Can See That cout <"(* (S. begin ())). cost = "<(* (S. begin ())). cost <Endl; return 0 ;}

Test results:

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.