std::map and case insensitive search

B

bb

Hi,

void fun(const std::map<std::string,int>& m1) {
// How to make a case insensitive search of this map without
making a copy?
}

cheers.
 
R

red floyd

bb said:
Hi,

void fun(const std::map<std::string,int>& m1) {
// How to make a case insensitive search of this map without
making a copy?
}

cheers.


Don't use std::map<string, int>. Use std::map<string, int, predicate>.
 
J

John Harrison

bb said:
Hi,

void fun(const std::map<std::string,int>& m1) {
// How to make a case insensitive search of this map without
making a copy?
}

cheers.

Essentially what you're asking for is a std::map with two different
keys, (one case sensitive, one case insensitive) which is impossible.

You need to rework your problem. You could try

std::map<case_insensitive_string, int>

or

std::map<std::string, int, case_insensitive_comparator>

or

you could convert all strings to one case before inserting into the map.

If you really need both case sensitive and case insensitive searches on
the same map, you are going to have to come up with a more complex data
structure yourself. You can't expect the standard library to do
everything for you.

john
 
J

Juha Nieminen

bb said:
void fun(const std::map<std::string,int>& m1) {
// How to make a case insensitive search of this map without
making a copy?
}

The map data structure is organized with the default < operator
in this case. It's simply physically impossible, using this map
instance only, to perform a O(log n) search when the comparator
is different because the items simply aren't organized that way.

You can perform a linear search, though (in this case it's not
possible to achieve anything faster). Simply traverse from begin()
to end() and compare keys.
 
J

Juha Nieminen

John said:
Essentially what you're asking for is a std::map with two different
keys, (one case sensitive, one case insensitive) which is impossible.

Actually he didn't impose any speed limitations on the search.
Nothing stops him from making a linear search through they elements
of the map.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top