map case insensitive

Hey guys, I have a map called map<string,list> . i want to make case insensitive for map first element which is string

my case insensitive as follows, i want to call this for map string ?

1
2
3
4
5
6
7
8
9
10
11
bool display_sort(const std::string& first, const std:: string& second)  {
  unsigned int i=0;
  while((i<first.length()) && (i<second.length())) {
        if( tolower(first[i])< tolower(second[i]))
                return true;
        else if (tolower(first[i])>tolower(second[i]))
                return false;
        ++i;
  }
  return (first.length()<second.length());
}


Thanks in advance..
In my map first string contains

hello
Jhonny
Aug
cat
in display output it should like
Aug
cat
hello
Jhonny

Hopefully think that possibly with iterator first.. need some thoughts,,,
A std::map has a comparison functor as template argument. When you declare your map, give it the proper comparitor:

 
std::map <std::string, std::list, display_sort> my_map;

Thereafter, when you iterate over your map, it will be in your desired sort order:

1
2
for (const auto& item: my_map)
  std::cout << item.first << "\n";

Make sure to refer to the documentation for this kind of stuff:
http://www.cplusplus.com/reference/map/map/
http://www.cplusplus.com/reference/utility/pair/

Also http://www.cplusplus.com/faq/sequences/strings/ci-compare/
Alas. (I need to clean that page up to use some C++11 features, like auto.)

Hope this helps.
I read those links, i didn't play much with maps, my doubt is my map was already populated and i m unable to create a new map like

map<string,list, functor> my_map

how i can use with exsited map , map<string,list>my_map


Any wrongs, plz guide me

Thanks.
Last edited on
The comparison function is part of the type so you will have to change the map type everywhere you use it. To make it easier to change the type in the future you can create a typedef.

 
typedef map<string,list,functor> MyMapType;

Now you can use MyMapType everywhere instead of map<string,list,functor>. If you later want to change the type again you only need to change one line of code.

This is just for demonstration. You probably want a better descriptive name for your typedef than I used here.
Last edited on
If it is just to display things, create a temporary map that sorts the keys properly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <cctype>
#include <iostream>
#include <map>
#include <string>

std::string tolower( std::string s )
{
  for (char& c : s) 
    c = std::tolower( c );
  return s;
}

template <typename T>
void display_keys( const std::map <std::string, T> & m, std::ostream& outs = std::cout )
{
  std::multimap <std::string, std::string> keys;
  
  for (const auto& pair : m)
    keys.emplace( tolower( pair.first ), pair.first );
    
  for (const auto& pair : keys)
    outs << pair.second << "\n";
}

int main()
{
  std::map <std::string, int> m 
  {
    { "hello",  1 },
    { "Jhonny", 2 },
    { "Aug",    3 },
    { "cat",    4 }
  };
  
  display_keys( m );
}

Hope this helps.
Topic archived. No new replies allowed.