std::map with multi values

Discussion in 'C++' started by Philipp Kraus, Aug 16, 2010.

  1. Hi,

    I'm using a std::map and I need two values in it. The key is a
    std::string value and the value of the std::map must have two values
    (named lower and upper).
    Should I use a struct like
    struct range {
    T lower;
    T upper;

    range( const T& a, const T& b) :
    lower(a), upper(b) {}
    };

    std::map< std::string, range > x

    Is there a more efficent way?
    Thanks for help

    Phil
     
    Philipp Kraus, Aug 16, 2010
    #1
    1. Advertising

  2. On 8/16/2010 6:20 AM, Philipp Kraus wrote:
    > I'm using a std::map and I need two values in it. The key is a
    > std::string value and the value of the std::map must have two values
    > (named lower and upper).
    > Should I use a struct like
    > struct range {
    > T lower;
    > T upper;
    >
    > range( const T& a, const T& b) :
    > lower(a), upper(b) {}


    Apparently you need your type for something else, not just to store the
    two values, yes? Otherwise you could just use std::pair<T,T>...

    > };
    >
    > std::map< std::string, range > x
    >
    > Is there a more efficent way?


    Efficient in what way? IOW, what's inefficient about your design?

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 16, 2010
    #2
    1. Advertising

  3. On 8/16/2010 9:15 AM, Leigh Johnston wrote:
    >
    > "Philipp Kraus" <> wrote in message
    > news:i4bda6$inc$...
    >> I had tried the std::pair<T,T>, but I can't compile it.
    >> on
    >> for(std::map<std::string, std::pair<T,T> >::iterator it =
    >> m_initvalues.begin(); it != m_initvalues.end(); ++it)
    >> (m_initvalues is declared std::map<std::string, std::pair<T,T> >
    >> m_initvalues;) I get:
    >>
    >> error: dependent-name 'std::map<std::basic_string<char,
    >> std::char_traits<char>, std::allocator<char>
    >> >,std::pair<_ForwardIterator,

    >> _ForwardIterator>,std::less<std::basic_string<char,
    >> std::char_traits<char>, std::allocator<char> >
    >> >,std::allocator<std::pair<const std::basic_string<char,

    >> std::char_traits<char>, std::allocator<char> >,
    >> std::pair<_ForwardIterator, _ForwardIterator> > > >::iterator' is
    >> parsed as a non-type, but instantiation yields a type
    >> worker.hpp:134: note: say 'typename std::map<std::basic_string<char,
    >> std::char_traits<char>, std::allocator<char>
    >> >,std::pair<_ForwardIterator,

    >> _ForwardIterator>,std::less<std::basic_string<char,
    >> std::char_traits<char>, std::allocator<char> >
    >> >,std::allocator<std::pair<const std::basic_string<char,

    >> std::char_traits<char>, std::allocator<char> >,
    >> std::pair<_ForwardIterator, _ForwardIterator> > > >::iterator' if a
    >> type is meant

    >
    > Don't post using HTML: most of us don't like it and it makes replying to
    > your post a PITA.
    >
    > Try std::map<std::string, typename std::pair<T,T> >


    You probably meant to suggest to try

    for (typename std::map<..>::iterator it = m_initvalues.begin();..

    And it's so much easier if the OP declared a typedef with that 'map'
    thing, like

    typedef std::map<std::string,std::pair<T,T> > mymap;

    because the we wouldn't be lost in those colons. Clean:

    for (typename mymap::iterator it = m_initvalues.begin() ...

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 16, 2010
    #3
  4. On 2010-08-16 15:15:31 +0200, Leigh Johnston said:

    > "Philipp Kraus" <> wrote in message
    > news:i4bda6$inc$...
    >> I had tried the std::pair<T,T>, but I can't compile it.
    >> on
    >> for(std::map<std::string, std::pair<T,T> >::iterator it =
    >> m_initvalues.begin(); it != m_initvalues.end(); ++it)
    >> (m_initvalues is declared std::map<std::string, std::pair<T,T> >
    >> m_initvalues;) I get:
    >>
    >> error: dependent-name 'std::map<std::basic_string<char,
    >> std::char_traits<char>, std::allocator<char>
    >> >,std::pair<_ForwardIterator,

    >> _ForwardIterator>,std::less<std::basic_string<char,
    >> std::char_traits<char>, std::allocator<char> >
    >>> ,std::allocator<std::pair<const std::basic_string<char,

    >> std::char_traits<char>, std::allocator<char> >,
    >> std::pair<_ForwardIterator, _ForwardIterator> > > >::iterator' is
    >> parsed as a non-type, but instantiation yields a type
    >> worker.hpp:134: note: say 'typename std::map<std::basic_string<char,
    >> std::char_traits<char>, std::allocator<char>
    >> >,std::pair<_ForwardIterator,

    >> _ForwardIterator>,std::less<std::basic_string<char,
    >> std::char_traits<char>, std::allocator<char> >
    >>> ,std::allocator<std::pair<const std::basic_string<char,

    >> std::char_traits<char>, std::allocator<char> >,
    >> std::pair<_ForwardIterator, _ForwardIterator> > > >::iterator' if a
    >> type is meant

    >
    > Don't post using HTML: most of us don't like it and it makes replying
    > to your post a PITA.


    sorry, I don't see the HTML code
     
    Philipp Kraus, Aug 16, 2010
    #4
  5. On 2010-08-16 15:49:45 +0200, Christian Hackl said:

    > Philipp Kraus ha scritto:
    >
    >> I had tried the std::pair<T,T>, but I can't compile it.
    >>
    >> [...]
    >> worker.hpp:134: note: say 'typename std::map<std::basic_string<char,
    >> std::char_traits<char>, std::allocator<char>
    >>> ,std::pair<_ForwardIterator,

    >> _ForwardIterator>,std::less<std::basic_string<char,
    >> std::char_traits<char>, std::allocator<char> >
    >>> ,std::allocator<std::pair<const std::basic_string<char,

    >> std::char_traits<char>, std::allocator<char> >,
    >> std::pair<_ForwardIterator, _ForwardIterator> > > >::iterator' if a
    >> type is meant

    >
    > If you do what the error message suggests, then everything should be
    > OK. It's telling you to put a "typename" in front of the iterator
    > variable.
    >
    > template <class T>
    > void f()
    > {
    > std::map<std::string, std::pair<T, T> > x;
    >
    > for (typename std::map<std::string, std::pair<T, T> >::iterator it =
    > x.begin(); it != x.end(); ++it) ;
    > }


    Thanks, the "typename" in front of std::map works
     
    Philipp Kraus, Aug 16, 2010
    #5
  6. Philipp Kraus

    James Kanze Guest

    On Aug 16, 1:48 pm, Victor Bazarov <> wrote:
    > On 8/16/2010 6:20 AM, Philipp Kraus wrote:


    > > I'm using a std::map and I need two values in it. The key is a
    > > std::string value and the value of the std::map must have two values
    > > (named lower and upper).
    > > Should I use a struct like
    > > struct range {
    > > T lower;
    > > T upper;


    > > range( const T& a, const T& b) :
    > > lower(a), upper(b) {}


    > Apparently you need your type for something else, not just to
    > store the two values, yes? Otherwise you could just use
    > std::pair<T,T>...


    Supposing that first and second had the correct semantic
    implications for what he's doing. In practice, that's rarely
    the case, and std::pair is almost useless.

    --
    James Kanze
     
    James Kanze, Aug 17, 2010
    #6
  7. On 2010-08-17 11:58:30 +0200, James Kanze said:

    > On Aug 16, 1:48 pm, Victor Bazarov <> wrote:
    >> On 8/16/2010 6:20 AM, Philipp Kraus wrote:

    >
    >>> I'm using a std::map and I need two values in it. The key is a
    >>> std::string value and the value of the std::map must have two values
    >>> (named lower and upper).
    >>> Should I use a struct like
    >>> struct range {
    >>> T lower;
    >>> T upper;

    >
    >>> range( const T& a, const T& b) :
    >>> lower(a), upper(b) {}

    >
    >> Apparently you need your type for something else, not just to
    >> store the two values, yes? Otherwise you could just use
    >> std::pair<T,T>...

    >
    > Supposing that first and second had the correct semantic
    > implications for what he's doing. In practice, that's rarely
    > the case, and std::pair is almost useless.


    std::pair works now and it's usefull, because I need std::map to save a
    range in it.
    like mymax["key"] = [lowerbound, upperbound] so my lower value will be
    the "first" and
    "upper" the second.

    Thanks
     
    Philipp Kraus, Aug 18, 2010
    #7
  8. Philipp Kraus

    Jorgen Grahn Guest

    On Wed, 2010-08-18, Philipp Kraus wrote:
    > On 2010-08-17 11:58:30 +0200, James Kanze said:
    >
    >> On Aug 16, 1:48 pm, Victor Bazarov <> wrote:
    >>> On 8/16/2010 6:20 AM, Philipp Kraus wrote:

    >>
    >>>> I'm using a std::map and I need two values in it. The key is a
    >>>> std::string value and the value of the std::map must have two values
    >>>> (named lower and upper).
    >>>> Should I use a struct like
    >>>> struct range {
    >>>> T lower;
    >>>> T upper;

    >>
    >>>> range( const T& a, const T& b) :
    >>>> lower(a), upper(b) {}

    >>
    >>> Apparently you need your type for something else, not just to
    >>> store the two values, yes? Otherwise you could just use
    >>> std::pair<T,T>...

    >>
    >> Supposing that first and second had the correct semantic
    >> implications for what he's doing. In practice, that's rarely
    >> the case, and std::pair is almost useless.

    >
    > std::pair works now and it's usefull, because I need std::map to save a
    > range in it.
    > like mymax["key"] = [lowerbound, upperbound] so my lower value will be
    > the "first" and
    > "upper" the second.


    I guess what James meant was he'd want convenient things like:

    - ways to print a Range as "M--N", "[M .. N]" or whatever
    - guarantees that a Range is valid
    - operations like Range::size() const

    So you have other reasons to make Range a distinct class rather than
    a std::pair of ints.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Aug 19, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,359
    Ivan Vecerina
    Mar 17, 2005
  2. Marcus
    Replies:
    2
    Views:
    599
    Marcus
    Dec 9, 2005
  3. Replies:
    1
    Views:
    436
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,034
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    2,034
    James Kanze
    Dec 21, 2008
Loading...

Share This Page