Problem solving-using STL in structs

Source: Internet
Author: User

===================== Declaration ==========================

This article is original, reproduced please specify the source and author, and maintain the integrity of the article (including this statement).

This article links:

==================================================

As for why the use of such wonderful things ... For the glory of the tribe.

----------------------------------------------------------------------------------------

Using STL's set in a struct is a bit more complicated than using vectors, and it feels as if the set is stored in a tree, so write "<".

On the sample code.

 #include < Iostream> #include <set>using namespace std;struct AA {int a1;int a2;bool operator < (const aa& ODR) const{ Return a1<odr.a1;}}; struct nn{std::set<aa> stset;}; int main (int argc, char* argv[]) {NN stn1;for (int i=6;i>0;i--) {AA sta;sta.a1=i;sta.a2=10*i;stn1.stset.insert (StA);} Std::set<aa>::iterator It=stn1.stset.begin (); Std::set<aa>::iterator itEnd=stN1.stSet.end (); for (; it!= itend;it++) {printf ("vector:a1=%d,a2=%d\n", IT->A1,IT->A2);} printf ("sizeof (NN) =%d\n", sizeof (NN));p rintf ("sizeof (StN) =%d\n", sizeof (stN1));///////////////////////////////// NN stn2;stn2.stset=stn1.stset;stn1.stset.clear (); It=stn2.stset.begin (); itend= StN2.stSet.end (); for (; it!=itend;it++) {printf ("vector:a1=%d,a2=%d\n", IT->A1,IT->A2);} printf ("sizeof (NN) =%d\n", sizeof (NN));p rintf ("sizeof (StN) =%d\n", sizeof (STN2)); return 0;} 

===================== Split line and say goodbye to you =======================

Problem solving-using STL in structs

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.