Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Writing a similar function as ComboBox SelectString

Writing a similar function as ComboBox SelectString

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    beko
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • B beko

      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.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      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

      B T 2 Replies Last reply
      0
      • S Stuart Dootson

        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

        B Offline
        B Offline
        beko
        wrote on last edited by
        #3

        Thank you, this is what i was looking for :thumbsup: Bekir.

        1 Reply Last reply
        0
        • S Stuart Dootson

          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

          T Offline
          T Offline
          TinyDevices
          wrote on last edited by
          #4

          Doesn't this require the elements to be sorted?

          S 1 Reply Last reply
          0
          • T TinyDevices

            Doesn't this require the elements to be sorted?

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            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

            T 1 Reply Last reply
            0
            • S Stuart Dootson

              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

              T Offline
              T Offline
              TinyDevices
              wrote on last edited by
              #6

              You are right. maps do it.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups