473,385 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Initialize a std::set with keys from a std::map

A portion of my program needs to initialize a std::set<intto have
all the keys in a std::map<int, double>. The code I've pasted below
works, but I'm wondering if there's a more elegant way to do it (an
STL algorithm, maybe?). I'm a bit of an STL noob, so any advice
people can give would be greatly appreciated.
typedef std::map<int, doubleintDoubleMap;
typedef std::set<intintSet;

intSet v;
// map is an intDoubleMap* and has been initialized with some values
for (intDoubleMap::const_iterator it = map->begin();
it != map->end();
it++) {
int i = it->first;
v.insert(i);
}
Thanks,
Casey
Jan 16 '08 #1
6 10168
<br*****@gmail.comwrote in message
news:2d**********************************@m34g2000 hsf.googlegroups.com...
typedef std::map<int, doubleintDoubleMap;
typedef std::set<intintSet;

intSet v;
// map is an intDoubleMap* and has been initialized with some values
for (intDoubleMap::const_iterator it = map->begin();
it != map->end();
it++) {
int i = it->first;
v.insert(i);
}
Looks reasonable to me. If I were being picky, I'd change it++ to ++it
(because there is no reason to copy the iterator and then throw the original
value away) and I'd collapse

int i = it-first;
v.insert(i);

into

v.insert(it->first);

but aside from that, I can't think offhand of a way of doing it that
wouldn't be much harder to understand (and without any obvious compensating
gain).

I might be missing something -- it wouldn't be the first time -- but there
is virtue in being straightforward.
Jan 16 '08 #2
br*****@gmail.com wrote:
A portion of my program needs to initialize a std::set<intto have
all the keys in a std::map<int, double>. The code I've pasted below
works, but I'm wondering if there's a more elegant way to do it (an
STL algorithm, maybe?). I'm a bit of an STL noob, so any advice
people can give would be greatly appreciated.

transform( map->begin(), map->end(), inserter( v, v.begin() ),
select1st<intDoubleMap::value_type>() );
Note, "select1st" is part of the STL, but not part of the standard
library. It's easy to implement though and is generally useful:

template <typename Pair>
struct select1st : unary_function<Pair, typename Pair::first_type>
{
const typename Pair::first_type& operator()(const Pair& x) const {
return x.first;
}
};
Or you can use the boost lambda library:

transform( map->begin(), map->end(), inserter( v, v.begin() ),
bind(&intDoubleMap::value_type::first, _1 ) );
typedef std::map<int, doubleintDoubleMap;
typedef std::set<intintSet;

intSet v;
// map is an intDoubleMap* and has been initialized with some values
for (intDoubleMap::const_iterator it = map->begin();
it != map->end();
it++) {
int i = it->first;
v.insert(i);
}
Jan 16 '08 #3
In article <2d8e0a59-0448-44d1-95dc-
eb**********@m34g2000hsf.googlegroups.com>, br*****@gmail.com says...
A portion of my program needs to initialize a std::set<intto have
all the keys in a std::map<int, double>.
Why? Is there a reason you can't just leave the data in the map, and
temporarily ignore the associated data?
The code I've pasted below
works, but I'm wondering if there's a more elegant way to do it (an
STL algorithm, maybe?). I'm a bit of an STL noob, so any advice
people can give would be greatly appreciated.
Andrew Koenig has already pointed out a couple of minor changes. In
theory, I think you could probably create a special iterator type that
returns whatever.first when you dereference it, but (quite frankly) I
think that's more trouble than it's worth unless you're doing things
like this in quite a few different places. If memory serves, boost has
some iterators that can do that sort of thing for a boot tuple, and
similar (maybe even identical) code would probably work for std::pair as
well (though I haven't looked at it, so I'm not sure -- for that matter,
my memory may just be bad, and no such thing exists at all...)

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 16 '08 #4
Why? Is there a reason you can't just leave the data in the map, and
temporarily ignore the associated data?
I was just thinking about that. I know the set is used in a
std::set_union algorithm downstream, so I'll have to figure if that
algorithm works with maps (and hash_maps) tomorrow. I checked the
set_union docs on sgi.com, but it's cryptic enough that I can't really
tell.

Daniel, your suggestion looks neat. If I can't avoid making the set
altogether I'll give that a try.
Jan 16 '08 #5
It sounds like the risk of introducing a new bug into the program
isn't worth changing from the current implementation (aside from
Andrew's style suggestions), considering that I'm relatively
inexperienced with custom iterators and custom comparison functions.

Though I am curious about the performance impact of skipping the
std::set creation and performing the set_union on a map instead - and
whether any performance increase from doing so would outweigh the
requirement that I use a std::map instead of a hash_map.

If I do manage to implement one of the techniques suggested here, I'll
profile it and report the results.

Thanks for the help,
Casey
Jan 17 '08 #6
In article <fe18334f-f096-4251-a077-
f0**********@e10g2000prf.googlegroups.com>, ja*********@gmail.com
says...

[ ... ]
I checked in the standard, and it isn't really clear either. I
think some of the requirements are missing: what happens if the
two input iterators have different value_type, for example?
It is a bit strange. I'm tempted to move this part of the discussion
over to comp.std.c++. There are really two outstanding questions, at
least in my mind. The first is what is really required in the current
standard. The second, and perhaps more important point, is whether any
omissions can be fixed for the next standard. Its description of
set_union doesn't seem to have changed a lot from the current one.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 18 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: cppaddict | last post by:
Hi, I'm confused about what the comparison operator in a map template is: In particular, I want to know why something like the following doesn't work: bool pointCompare(POINT p1, POINT p2)...
0
by: Erik Arner | last post by:
Hi, let's say I have a std::map<std::string,int> and I want to search the map for all keys that start with "foo". The regexp equivalent is to search for "foo*", or perhaps "^foo*". At present...
5
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
1
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003)...
10
by: danibe | last post by:
I never had any problems storing pointers in STL containers such std::vector and std::map. The benefit of storing pointers instead of the objects themselves is mainly saving memory resources and...
16
by: Cory Nelson | last post by:
Does anyone know how std::set prevents duplicates using only std::less? I've tried looking through a couple of the STL implementations and their code is pretty unreadable (to allow for different...
3
by: digz | last post by:
This is a very simplified version of something I am trying to understand. The State object holds the strings and maps , and I pass a reference to State to the process Function which manipulates it...
2
by: mathieu | last post by:
hi there, I would like to know if the following piece of code is garantee to work. I am afraid that the cstring address I am using in the std::map found from a request in std::set is not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.