std::map question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Razmig K

    #1

    std::map question

    Dear mates,

    Is there an elegant way to ensure the existence of a certain key among
    the keys of a map variable ?

    Thank you.

    //rk
  • Dave Theese

    #2
    Re: std::map question


    Could you please clarify the question a little further?

    I'm tempted to say "just insert a pair that has the key you desire to be
    present", but somehow I think that's probably missing your intended point...


    "Razmig K" <strontium90@po stmaster.co.uk> wrote in message
    news:3cad08b8.0 308251407.2f18d 77b@posting.goo gle.com...[color=blue]
    > Dear mates,
    >
    > Is there an elegant way to ensure the existence of a certain key among
    > the keys of a map variable ?
    >
    > Thank you.
    >
    > //rk[/color]


    Comment

    • Mike Wahler

      #3
      Re: std::map question


      Razmig K <strontium90@po stmaster.co.uk> wrote in message
      news:3cad08b8.0 308251407.2f18d 77b@posting.goo gle.com...[color=blue]
      > Dear mates,
      >
      > Is there an elegant way to ensure the existence of a certain key among
      > the keys of a map variable ?[/color]

      if(the_map.find (key) == the_map.end())
      the_map[key];

      -Mike



      Comment

      • Kevin Goodsell

        #4
        Re: std::map question

        Razmig K wrote:
        [color=blue]
        > Dear mates,
        >
        > Is there an elegant way to ensure the existence of a certain key among
        > the keys of a map variable ?
        >[/color]

        I'm not sure I understand. If you want to make sure a certain key will
        exist in the map, you need only insert it. As long as you don't remove
        it later, this ensures that the key will exist "among the keys in the
        map variable".

        If you meant to ask how one would check for the existence of a
        particular key, the answer is to use the 'find()' function.

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        Working...