C ++ modifies elements in STL set.

Source: Internet
Author: User

If the set iterator has a const modifier, modifications to its elements will inevitably fail. However, sometimes the STL set element needs to be modified. The general solution to this problem is to first erase the element and then insert it. This is very inefficient, so we need to find a more effective method, at the beginning, compilation is always fail. If you map it into a referenced object using const_cast (* It), you can modify it. A more common method is as follows:

# Include <set> <br/> # include <string> <br/> # include <iostream> <br/> using namespace STD; </P> <p> class cemployee {<br/> Public: <br/> cemployee (); <br/> ~ Cemployee (); <br/> const string getname () const; <br/> void setname (const string name); <br/> const string gettitle () const; <br/> void settitle (String title); <br/> int GETID () const; <br/> void setid (int id); <br/> PRIVATE: <br/> int m_id; <br/> string m_username; <br/> string m_title; </P> <p >}; <br/> cemployee: cemployee () <br/>{</P> <p >}< br/> cemployee ::~ Cemployee () <br/>{< br/>}< br/> const string cemployee: getname () const <br/>{< br/> return m_username; <br/>}< br/> void cemployee: setname (const string username) <br/>{< br/> m_username = username; <br/>}</P> <p> const string cemployee: gettitle () const <br/>{< br/> return m_title; <br/>}</P> <p> void cemployee: settitle (String title) <br/>{< br/> m_title = title; <br/>}< br/> int cemployee: GETID () con St <br/>{< br/> return m_id; <br/>}< br/> void cemployee: setid (int id) <br/>{< br/> m_id = ID; <br/>}< br/> class sortbyid <br/>{< br/> public: <br/> bool operator () (cemployee const & _ A, cemployee const & _ B) const <br/>{< br/> If (_. GETID () <_ B. GETID () return true; <br/> If (_. GETID () = _ B. GETID () return _. getname (). compare (_ B. getname () <0; <br/> return false; <br/>}< br/>}; </P> <p> int Ma In () <br/>{< br/> set <cemployee, sortbyid> empidset; // se is an employee's set, <br/> set <cemployee, sortbyid> :: iterator ITER; <br/> cemployee employeeinfo; <br/> employeeinfo. setname ("employee_one"); <br/> employeeinfo. settitle ("employee"); <br/> employeeinfo. setid (1); <br/> empidset. insert (employeeinfo); <br/> cemployee employeeinfo2; <br/> employeeinfo2.setname ("employee_two"); <br/> employeeinfo2.settitle ("CFO"); <br/> employeeinfo2.setid (5); <br/> empidset. insert (employeeinfo2); </P> <p> cemployee employeeinfo3; <br/> employeeinfo3.setname ("employee_three"); <br/> employeeinfo3.settitle ("manager "); <br/> employeeinfo3.setid (3); <br/> empidset. insert (employeeinfo3); <br/> // Step 1: Find the element to be changed <br/> iter = empidset. find (employeeinfo2); <br/> If (ITER! = Empidset. end () <br/>{< br/> // Step 2: Copy this element <br/> cemployee E (* ITER ); <br/> // Step 3: delete this element. <br/> empidset. erase (ITER ++); <br/> // auto-increment this iterator to keep it valid (see clause 9) <br/> // Step 4: modify this copy <br/> E. settitle ("incluate deity"); <br/> // Step 5: Insert a new value; prompt its location <br/> empidset. insert (ITER, e ); <br/> // same as the original element </P> <p> // No effect <br/> // static_cast <cemployee> (* ITER ). settitle ("shortate deity"); </P> <p >}< br/> else <br/>{< br/> C Out <"failed/N" <Endl; <br/>}< br/> for (iter = empidset. Begin (); iter! = Empidset. end (); ITER ++) <br/>{< br/> cout <ITER-> GETID () <"" <ITER-> getname () <"" <ITER-> gettitle () <Endl; <br/>}< br/> return 0; <br/>}< br/>

Refer:

Valid STL cla22: avoid modifying the set and Multiset keys in the same place

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.