C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Picking a mapping type's value_type

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jeffrey Yasskin
Guest





PostPosted: Tue Nov 28, 2006 10:10 am    Post subject: Picking a mapping type's value_type Reply with quote



I'm implementing a vector_map<Key,Value> backed by a
vector<pair<Key,Value>> which maintains its pairs in sorted order by
key and uses std::lower_bound for lookups, and I've run into a problem
with the Container requirement that vector_map::value_type and
vector_map::iterator::value_type be identical.

It looks like the only practical effects of value_type are the argument
to .insert() and the return type of operator*(). For
iterator::operator*(), I need to return something that lets people
change the value but not the key. The simplest choice is a proxy object
that contains a vector::iterator and has "const Key& key() const",
"Value& value()", and "const Value& value() const" methods (perhaps the
iterator type itself). For .insert(), I need to accept something that's
constructible from a Key and a Value and convertible to pair<Key,
Value>. pair<Key, Value> itself works fine, but isn't the same as the
result of operator*().

A way to make them identical without copying both the key and the value
on every dereference would be to put two references into the
value_type, but that's twice as big as iterator::value_type really
needs to be and feels wasteful.

The fact that it's this complicated to define what should be a simple
container leads me to suspect that I'm doing something wrong. How did
the committee intend people to define extra mapping containers? Is it
ok for vector_map::iterator::value_type to only be implicitly
convertible to vector_map::value_type rather than identical?

Thanks!
Jeffrey


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Seungbeom Kim
Guest





PostPosted: Wed Nov 29, 2006 9:40 am    Post subject: Re: Picking a mapping type's value_type Reply with quote



Greg Herlihy wrote:
Quote:
Programmers generally expect to be able to retrieve values in a map
directly - by specifying a key. It should not necessary to use an
iterator at all. A std::map, for example, offers both operator[] and
at() for accessing values based on their key:

my_map[ akey] = avalue;

Std::map offers operator[](), but not at().
It does offer lower_bound, upper_bound and equal_range, instead.

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Thu Nov 30, 2006 5:23 am    Post subject: Re: Picking a mapping type's value_type Reply with quote



Carl Barron ha scritto:
Quote:
see also n2134 at that site, the latest draft ansi version [n2135.pdf
appears to be the same but as an iso format] not real sure what the few
days produced. These are current draft standards. There is an at for
map in these documents.


The main difference I found between N2134 and N2135 is the introduction
of placeholder clauses for a lot of topics that are probably going to be
introduced, for example: thread local storage (3.7.2), garbage
collection (3.7.5), atomic operation library (2Cool and thread support
library (29). I don't know if there are substantial (if any) changes in
the other clauses, though.

Ganesh

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jeffrey Yasskin
Guest





PostPosted: Fri Dec 01, 2006 2:35 am    Post subject: Re: Picking a mapping type's value_type Reply with quote

On Nov 29, 4:24 am, "Maxim Yegorushkin" <maxim.yegorush...@gmail.com>
wrote:
Quote:
While being esthetically not pleasing, I think adding const on practise
does not do any harm:

std::pair<int, int> a;
std::pair<int const, int>& ra = reinterpret_cast<std::pair<int const,
int>&>(a);

And then I'd use std::pair<const Key, Value> as my value_type. Hmm.
[expr.reinterpret.cast] says the result is implementation-defined or
unspecified, but any implementation I care about probably does the
right thing... I'd still be surprised if this was the intended way to
define new mapping types though.

I may wind up using this anyway, thanks.

Jeffrey


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.