Writing a similar function as ComboBox SelectString
-
Hello, I have a STL map and string values as the key values However recently, i want to search this map as it is like auto-complete ComboBox. For example,I have keys in the map as such Apple, Pearl, Orange. When i enter 'A' i want to return Apple from the map. I know that i can iterate thorough each map object and compare its key, but i think that would inefficient. Any ideas ? Bekir.
-
Hello, I have a STL map and string values as the key values However recently, i want to search this map as it is like auto-complete ComboBox. For example,I have keys in the map as such Apple, Pearl, Orange. When i enter 'A' i want to return Apple from the map. I know that i can iterate thorough each map object and compare its key, but i think that would inefficient. Any ideas ? Bekir.
Use
std::map::lower_bound
. For example, the program below gives you a pair of iterators defining the range containing keys starting with 'A':#include <string>
#include <map>
#include <iostream>int main(int, char**)
{
std::map<std::string, int> a;
a.insert(std::make_pair("Apple", 1));
a.insert(std::make_pair("Banana", 1));
a.insert(std::make_pair("Pear", 1));
a.insert(std::make_pair("Apricot", 1));
a.insert(std::make_pair("Plum", 1));std::map<std::string, int>::const_iterator low = a.lower_bound("A");
std::map<std::string, int>::const_iterator hi = a.lower_bound("B");for(std::map<std::string, int>::const_iterator it=low;it!=hi;++it)
{
std::cout << it->first << std::endl;
}
}Note that the sample key for the second
lower_bound
call is just the key you're looking for with the last character incremented by one.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Use
std::map::lower_bound
. For example, the program below gives you a pair of iterators defining the range containing keys starting with 'A':#include <string>
#include <map>
#include <iostream>int main(int, char**)
{
std::map<std::string, int> a;
a.insert(std::make_pair("Apple", 1));
a.insert(std::make_pair("Banana", 1));
a.insert(std::make_pair("Pear", 1));
a.insert(std::make_pair("Apricot", 1));
a.insert(std::make_pair("Plum", 1));std::map<std::string, int>::const_iterator low = a.lower_bound("A");
std::map<std::string, int>::const_iterator hi = a.lower_bound("B");for(std::map<std::string, int>::const_iterator it=low;it!=hi;++it)
{
std::cout << it->first << std::endl;
}
}Note that the sample key for the second
lower_bound
call is just the key you're looking for with the last character incremented by one.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Use
std::map::lower_bound
. For example, the program below gives you a pair of iterators defining the range containing keys starting with 'A':#include <string>
#include <map>
#include <iostream>int main(int, char**)
{
std::map<std::string, int> a;
a.insert(std::make_pair("Apple", 1));
a.insert(std::make_pair("Banana", 1));
a.insert(std::make_pair("Pear", 1));
a.insert(std::make_pair("Apricot", 1));
a.insert(std::make_pair("Plum", 1));std::map<std::string, int>::const_iterator low = a.lower_bound("A");
std::map<std::string, int>::const_iterator hi = a.lower_bound("B");for(std::map<std::string, int>::const_iterator it=low;it!=hi;++it)
{
std::cout << it->first << std::endl;
}
}Note that the sample key for the second
lower_bound
call is just the key you're looking for with the last character incremented by one.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Doesn't this require the elements to be sorted?
-
Doesn't this require the elements to be sorted?
It's a
std::map
- by definition, it's sorted.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
It's a
std::map
- by definition, it's sorted.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
You are right. maps do it.