std::map with multi values

P

Philipp Kraus

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
 
V

Victor Bazarov

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
};

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

Is there a more efficent way?

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

V
 
V

Victor Bazarov

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
 
P

Philipp Kraus

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::pair<_ForwardIterator,
_ForwardIterator>,std::less<std::basic_string<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
 
J

James Kanze

On 8/16/2010 6:20 AM, Philipp Kraus wrote:
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.
 
P

Philipp Kraus

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
 
J

Jorgen Grahn

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
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top