STL map ( no unique element )


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: STL map ( no unique element )

  1. #1
    Join Date
    Aug 2004
    Posts
    6

    Exclamation STL map ( no unique element )

    Hi there,

    Are there any experts out there ?

    I know STL map only allow unique key but how about its element ?
    I want the element to be unique too. Is there an easy way to do it ?

    I can compare every element in the map and make sure it's unique but i am posting here for a better way.

    eg grade_list["john"] = 'A';

    The 'A' is the element and i want it to be unique for all element in the map. There's a reason for what i am doing.

    I don't want to create additional set structure.

    Please help.

    cheers

  2. #2
    Join Date
    Jan 2007
    Posts
    145
    Either you can write a wrapper or try some customized STL (i guess few are available as part of ACE)

  3. #3
    Join Date
    Aug 2004
    Posts
    6

    Exclamation

    It seems like i have to compare every element in the map.

    so is this efficient enough ?

    Code:
    for(iter = mymap.begin(); iter != mymap.end(); iter++)
        {
            cout << endl;
            
            for(iter1 = mymap.begin(); iter1 != mymap.end(); iter1++)
            {
            	if(*iter != *iter1)
            	{
            		// if two elements are equal, then print out "Equal"
            		if()
            		cout << " iter: " << iter->second << " iter1: " << iter1->second << endl;
            	}
            }
      
        }
    If not, please suggest a better solution.

    thanks

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    I would use a different approach: instead of storing the name as the key, use the grade as the associated value (what you call "the element"). Alternatively, use the unique(), remove_if() etc. algorithms to ensure uniqueness of the map's elements.
    Danny Kalev

Similar Threads

  1. STL map
    By ami in forum C++
    Replies: 1
    Last Post: 12-05-2006, 04:35 PM
  2. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  3. Replies: 2
    Last Post: 09-21-2001, 09:26 AM
  4. Map String to unique int
    By Orly in forum Java
    Replies: 0
    Last Post: 06-25-2001, 04:06 AM
  5. AVL Tree element index
    By John in forum Java
    Replies: 2
    Last Post: 04-29-2001, 05:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center