Sign in to follow this  

C++ Multimap container and case insensitive key sort

This topic is 4302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hm... map is case sensitive, but i need it case insensitive only in the key sort, while still retaining it's sensitivity.
and I don't know how to manipulate classes ( i believe they're called inheritence? )

and I'm assuming it's not part of the STL.
this sucks....

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
He probably did, but I don't think he speaks French :D. IOW, he wants you to do the work for him.

Share this post


Link to post
Share on other sites
yes, I did read your comment.
but I don't quite understand

Quote:
Use a multimap with a custom comparison function


What is a custom comparison function?
Is making a custom comparison function have something to do with inheritance?

Share this post


Link to post
Share on other sites
No, it has nothing to do with inheritance. Multimaps allow the use of a custom comparison function, which has a prototype of bool (*)(const T& a, const T& b) and which returns true IFF you want a < b to be true. If I remember correctly, this function would be supplied as a constructor argument when you create the multimap.

If you want the sort to be case insensitive, convert all strings to the same case before comparing them lexicographically, and encapsulate this in a comparison function which you will provide to the multimap.

If you want to keep equality comparisons case-sensitive, you can make the order relationship above complete by using a normal, case-sensitive comparison if the two compared case-insensitive strings are equal.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
If I remember correctly, this function would be supplied as a constructor argument when you create the multimap.
Yep, it's the third template parameter (which defaults to std::less< KeyType >).

To the OP: I recommend you read through the SGI STL reference to understand the concepts behind the STL.


jfl.

Share this post


Link to post
Share on other sites

code seemed good, and was removed from board.


Any more revisions to be made?
code works fine. What do you all think?
and another question, do multimaps have reverse methods like the vector, whereas i can reverse the abc order to be listed in reverse?

And as always, I don't understand a thing that going on with the upper code.
All I did was copy and paste, is this normal?

[Edited by - Tradone on March 1, 2006 1:08:28 PM]

Share this post


Link to post
Share on other sites

int main()
{
multimap<const char*, int, ltstr> m;

m.insert(pair<const char* const, int>("aBCC", 5));
m.insert(pair<const char* const, int>("ab", 1));
m.insert(pair<const char* const, int>("c", 2));
m.insert(pair<const char* const, int>("B", 3));
m.insert(pair<const char* const, int>("b", 4));
m.insert(pair<const char* const, int>("aB", 5));
m.insert(pair<const char* const, int>("B", 6));
m.insert(pair<const char* const, int>("aBc", 5));


cout << "Number of elements with key a: " << m.count("a") << endl;
cout << "Number of elements with key b: " << m.count("b") << endl;
cout << "Number of elements with key c: " << m.count("c") << endl;

cout << "Elements in m: " << endl;
for (multimap<const char*, int, ltstr>::iterator it = m.begin();
it != m.end();
++it){
cout << " [" << (*it).first << ", " << (*it).second << "]" << endl;
}


is there a way to set up initialize the iterators in a way such that I would only get the 2nd~ 5th element inside multimap?

Share this post


Link to post
Share on other sites
Well, a map isn't random-access, so you don't really have direct access to "iterator to the nth element". But you can always advance n times from the beginning of the container. Look up std::distance and std::advance.

Also, you don't need to specify the template arguments when you make pair objects - the library provides a helper function "make_pair" to handle this for you. The thing is that class constructors can't deduce the template types from input arguments, but free functions *can*. So you just do for example:


m.insert(make_pair("aBCC", 5));


There are several similar helpers defined in the standard library (for example back_inserter(), which creates back_insert_iterators).

Share this post


Link to post
Share on other sites

This topic is 4302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this