473,406 Members | 2,273 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,406 software developers and data experts.

map to vector



How do I copy all the elements of a map to a vector
efficiently? The vector is empty initially and needs
to be populated with the map elements in sorted order.

Thanks,
--j

Jul 23 '05 #1
9 6550
John wrote:
How do I copy all the elements of a map to a vector
efficiently? The vector is empty initially and needs
to be populated with the map elements in sorted order.


(a) Reserve the space in the vector based on the size of the map.
See 'std::vector::reserve' member.

(b) Use std::copy and std::back_inserter.

Would you like to figure the code out yourself?

V
Jul 23 '05 #2

Damn Victor,

Sorry for being lazy, k will write it ;)

Thanks,
--j

Jul 23 '05 #3
v.reserve(tmap.size());
copy(tmap.begin(), tmap.begin() + tmap.size(), v.begin());

but my tmap is map<K,I> and my vector v is vector<K>

Is this copy right?

Jul 23 '05 #4


This wont work , I need a tranform and a functor, anyone can help?
I want to copy all the elements of map<A,B> to vector<A>

John wrote:
v.reserve(tmap.size());
copy(tmap.begin(), tmap.begin() + tmap.size(), v.begin());

but my tmap is map<K,I> and my vector v is vector<K>

Is this copy right?


Jul 23 '05 #5
John wrote:
v.reserve(tmap.size());
copy(tmap.begin(), tmap.begin() + tmap.size(), v.begin());

but my tmap is map<K,I> and my vector v is vector<K>

Is this copy right?


I don't think that std::copy is appropriate here. The value_type of
your map is std::pair<K, I>, while the value_type of your vector is just
K. The former cannot be assigned to the latter. What you are looking
for is std::transform, which let's you provide a function that will
transform the pair into the appropriate value. Here is an example,
using types K and I as you did. Note that select1st and select2nd are
based on the descriptions given here:
http://www.sgi.com/tech/stl/table_of_contents.html

-Alan

#include <iostream>
#include <map>
#include <vector>
#include <iterator>
#include <algorithm>

template <typename Pair>
struct select1st
{
typedef Pair argument_type ;
typedef typename Pair::first_type result_type ;

const result_type &
operator()(const argument_type &p) const
{
return p.first ;
}
} ;

template <typename Pair>
struct select2nd
{
typedef Pair argument_type ;
typedef typename Pair::second_type result_type ;

const result_type &
operator()(const argument_type &p) const
{
return p.second ;
}
} ;

int main()
{
typedef const char * K ;
typedef int I ;

std::map<K, I> m ;
std::vector<K> vk ;
std::vector<I> vi ;

// Put some values into the map.
m["key1"] = 1 ;
m["key2"] = 2 ;
m["key3"] = 3 ;
m["key4"] = 4 ;

// Copy the keys to the vector.
vk.reserve(m.size()) ;
std::transform(m.begin(), m.end(), std::back_inserter(vk),
select1st<std::map<K, I>::value_type>()) ;

// Print the vector.
std::copy(vk.begin(), vk.end(),
std::ostream_iterator<K>(std::cout, "\n")) ;

// Copy the values to the vector.
vi.reserve(m.size()) ;
std::transform(m.begin(), m.end(), std::back_inserter(vi),
select2nd<std::map<K, I>::value_type>()) ;

// Print the vector.
std::copy(vi.begin(), vi.end(),
std::ostream_iterator<I>(std::cout, "\n")) ;

return 0 ;
}
Jul 23 '05 #6


Victor Bazarov wrote:
John wrote:
How do I copy all the elements of a map to a vector
efficiently? The vector is empty initially and needs
to be populated with the map elements in sorted order.


(a) Reserve the space in the vector based on the size of the map.
See 'std::vector::reserve' member.

(b) Use std::copy and std::back_inserter.

Would you like to figure the code out yourself?

V


Hi,
- Is 'reserve' better than 'resize' (in performance perspective)?

Jul 23 '05 #7
Prawit Chaivong wrote:

Victor Bazarov wrote:
John wrote:
How do I copy all the elements of a map to a vector
efficiently? The vector is empty initially and needs
to be populated with the map elements in sorted order.


(a) Reserve the space in the vector based on the size of the map.
See 'std::vector::reserve' member.

(b) Use std::copy and std::back_inserter.

Would you like to figure the code out yourself?

V

Hi,
- Is 'reserve' better than 'resize' (in performance perspective)?


reserve (potentially) changes the _capacity_ of the vector, while resize
changes the _size_ of the vector. Think of capacity as the number of
elements the vector space to hold before it needs to allocate more
space, whereas size would be the number of things it is actually holding.

Consider the following code fragment:

std::vector<SomeType> v ;
v.resize(3) ;

This actually creates 3 new objects of type SomeType (and calls their
constructors when they are created). At this point, v[0], v[1], and
v[2] are all valid expressions.

Now consider doing the same thing with reserve:

std::vector<SomeType> v ;
v.reserve(3) ;

This doesn't create any new objects, it just causes the vector to
allocate (at least) enough space to hold 3 objects of type SomeType
before it has to do any reallocating. v[0], v[1], and v[2] are NOT
valid expressions, as we still need to actually put some objects in it,
via some method such as push_back, or as Victor suggested, with a
back_insertion_iterator.

-Alan
Jul 23 '05 #8
John wrote:

This wont work , I need a tranform and a functor, anyone can help?
I want to copy all the elements of map<A,B> to vector<A>


You need a vector<pair<A,B> > instead of a vector<A>. The elements of a
map<A,B> are of type pair<A,B>.

--
Mike Smith
Jul 23 '05 #9
Prawit Chaivong wrote:
Victor Bazarov wrote:
John wrote:
How do I copy all the elements of a map to a vector
efficiently? The vector is empty initially and needs
to be populated with the map elements in sorted order.


(a) Reserve the space in the vector based on the size of the map.
See 'std::vector::reserve' member.

(b) Use std::copy and std::back_inserter.

Would you like to figure the code out yourself?

V


Hi,
- Is 'reserve' better than 'resize' (in performance perspective)?


I am not sure what you mean by "better". They do different things.
'resize' resizes. 'reserve' doesn't change anything except if the
current storage is smaller, reallocates the storage. 'resize' adds
to the vector. 'reserve' only affects the future 'push_back' or
'insert'.

V
Jul 23 '05 #10

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

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
16
by: Martin Jørgensen | last post by:
Hi, I get this using g++: main.cpp:9: error: new types may not be defined in a return type main.cpp:9: note: (perhaps a semicolon is missing after the definition of 'vector') main.cpp:9:...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.