Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL map to STL vector

Reply
Thread Tools

STL map to STL vector

 
 
Luca Risolia
Guest
Posts: n/a
 
      01-13-2014
Mike Copeland wrote:

> Is it possible to simply (single statement) move the data from one
> container type (the TeamMap, below) to a different type of container
> (the TeamVector, below)?


std::for_each(std::make_move_iterator(std::begin(T eamMap)),
std::make_move_iterator(std::end(TeamMap)),
[](decltype(TeamMap)::value_type&& v) {
TeamVector.push_back(std::move(v.second));
});

 
Reply With Quote
 
 
 
 
Mike Copeland
Guest
Posts: n/a
 
      01-14-2014
Is it possible to simply (single statement) move the data from one
container type (the TeamMap, below) to a different type of container
(the TeamVector, below)? My data resides in the map object, but since I
have to occasionally display the data in different orders (e.g.
teamName, teamTypeCode), I must copy the data to a vector and sort it
before producing my listings.
I know that I can copy the individual objects from the map to the
vector one-at-a-time, but I'm hoping that there's a technique that will
do it simply and quickly. Any thoughts? TIA


typedef map<char, int> ShirtStats;
struct TeamData
{
bool isAdded; // Added to container data
bool isValidTeam; // really is a team
char teamTypeCode; // Team type Code
int teamMembers1; // Count of Team Members-1
int teamMembers2; // Count of Team Members-2
int genderCounts[NMAXEVT][2]; // counts of Gender by Event
int shirtTotalMatrix[5][8];
string teamCode; // Team's Code (strTId)
string teamName; // Team's Name
ShirtStats shirtStats;
} extern teamWork;
typedef map<string, TeamData> TeamMap;
TeamMap::iterator tIter;
ShirtStats::iterator ssIter;
TeamMap teamMap;
typedef vector<TeamData> TeamVector;
typedef TeamVector::iterator TeamIter;
TeamVector teamVect;
TeamIter tvIter;

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-14-2014
Mike Copeland wrote:
> Is it possible to simply (single statement) move the data from one
> container type (the TeamMap, below) to a different type of container
> (the TeamVector, below)? My data resides in the map object, but since I
> have to occasionally display the data in different orders (e.g.
> teamName, teamTypeCode), I must copy the data to a vector and sort it
> before producing my listings.
> I know that I can copy the individual objects from the map to the
> vector one-at-a-time, but I'm hoping that there's a technique that will
> do it simply and quickly. Any thoughts? TIA


Construct the vector using the map's beginning and end:

TeamVector t( teamMap.begin(), teamMap.end() );

--
Ian Collins
 
Reply With Quote
 
woodbrian77@gmail.com
Guest
Posts: n/a
 
      01-14-2014
On Monday, January 13, 2014 6:06:47 PM UTC-6, Mike Copeland wrote:
> Is it possible to simply (single statement) move the data from one
> container type (the TeamMap, below) to a different type of container
>
> (the TeamVector, below)? My data resides in the map object, but since I
> have to occasionally display the data in different orders (e.g.
> teamName, teamTypeCode), I must copy the data to a vector and sort it
> before producing my listings.
> I know that I can copy the individual objects from the map to the
> vector one-at-a-time, but I'm hoping that there's a technique that will
> do it simply and quickly. Any thoughts? TIA
>


How about

http://www.boost.org/doc/libs/1_55_0...doc/index.html


That would help you avoid the copying.

Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      01-14-2014
On 2014-01-14, Ian Collins <(E-Mail Removed)> wrote:
> Mike Copeland wrote:
>> Is it possible to simply (single statement) move the data from one
>> container type (the TeamMap, below) to a different type of container
>> (the TeamVector, below)? My data resides in the map object, but since I
>> have to occasionally display the data in different orders (e.g.
>> teamName, teamTypeCode), I must copy the data to a vector and sort it
>> before producing my listings.
>> I know that I can copy the individual objects from the map to the
>> vector one-at-a-time, but I'm hoping that there's a technique that will
>> do it simply and quickly. Any thoughts? TIA

>
> Construct the vector using the map's beginning and end:
>
> TeamVector t( teamMap.begin(), teamMap.end() );


That won't work because the value type of the map is not the same as the
value type of the vector.
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      01-14-2014
On 2014-01-13, Luca Risolia <(E-Mail Removed)> wrote:
> Mike Copeland wrote:
>
>> Is it possible to simply (single statement) move the data from one
>> container type (the TeamMap, below) to a different type of container
>> (the TeamVector, below)?

>
> std::for_each(std::make_move_iterator(std::begin(T eamMap)),
> std::make_move_iterator(std::end(TeamMap)),
> [](decltype(TeamMap)::value_type&& v) {
> TeamVector.push_back(std::move(v.second));
> });


From Mike's description (snipped) it looks like he wants to copy the
data, not move it.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-14-2014
Ike Naar wrote:
> On 2014-01-14, Ian Collins <(E-Mail Removed)> wrote:
>> Mike Copeland wrote:
>>> Is it possible to simply (single statement) move the data from one
>>> container type (the TeamMap, below) to a different type of container
>>> (the TeamVector, below)? My data resides in the map object, but since I
>>> have to occasionally display the data in different orders (e.g.
>>> teamName, teamTypeCode), I must copy the data to a vector and sort it
>>> before producing my listings.
>>> I know that I can copy the individual objects from the map to the
>>> vector one-at-a-time, but I'm hoping that there's a technique that will
>>> do it simply and quickly. Any thoughts? TIA

>>
>> Construct the vector using the map's beginning and end:
>>
>> TeamVector t( teamMap.begin(), teamMap.end() );

>
> That won't work because the value type of the map is not the same as the
> value type of the vector.


Whoops, you're right. I was thinking of std::set, not map.

--
Ian Collins
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-14-2014
On 14.01.2014 08:30, Ian Collins wrote:
> Ike Naar wrote:
>> On 2014-01-14, Ian Collins <(E-Mail Removed)> wrote:
>>> Mike Copeland wrote:
>>>> Is it possible to simply (single statement) move the data from one
>>>> container type (the TeamMap, below) to a different type of container
>>>> (the TeamVector, below)? My data resides in the map object, but
>>>> since I
>>>> have to occasionally display the data in different orders (e.g.
>>>> teamName, teamTypeCode), I must copy the data to a vector and sort it
>>>> before producing my listings.
>>>> I know that I can copy the individual objects from the map to the
>>>> vector one-at-a-time, but I'm hoping that there's a technique that will
>>>> do it simply and quickly. Any thoughts? TIA
>>>
>>> Construct the vector using the map's beginning and end:
>>>
>>> TeamVector t( teamMap.begin(), teamMap.end() );

>>
>> That won't work because the value type of the map is not the same as the
>> value type of the vector.

>
> Whoops, you're right. I was thinking of std::set, not map.
>


The following little class comes in handy then:


[code]
template< class Iter, class Referent, class Remap_func >
struct Remap_iter
: Iter
{
typedef Iter Base;

Referent& operator* ()
{ return Remap_func()( Base:perator*() ); }

Referent const& operator* () const
{ return Remap_func()( Base:perator*() ); }

Remap_iter( Iter const& it )
: Base( it )
{}
};
[code]


It doesn't cover all the cases but one can just add to it. Good enough
for Mike's code with Visual C++. That is,


Code:
struct Map_to_vec
{
TeamData& operator()( TeamMap::value_type& v )
{ return v.second; }
};

int main()
{
typedef Remap_iter< TeamMap::iterator, TeamData, Map_to_vec > Vec_iter;

TeamMap         m;
TeamVector      v( Vec_iter( m.begin() ), Vec_iter( m.end() ) );
}

compiles nicely.

Disclaimer: haven't tested it with any data!


Cheers & hth.,

- Alf

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-14-2014
On 14.01.2014 10:49, Juha Nieminen wrote:
> Alf P. Steinbach <(E-Mail Removed)> wrote:
>> The following little class comes in handy then:

>
> Or instead you could do the sensible thing and use a 1-liner:
>
> for(auto& element: theMap) theVector.push_back(element.second);


I'm not sure it's always so sensible as a /general/ solution, which I
somewhat unthinkingly aimed at.

In particular, if you want to declare `theVector` as `const`, which is
usually a good idea since one can then be sure at a glance that it won't
change, then the above approach needs a lambda or named function:

vector<TeamData> theVector( [&]() -> vector<TeamData>
{
vector<TeamData> result;
for(auto& element: m) theVector.push_back(element.second);
return result;
}() );

Like, ouch. I think the "iterator conversion" approach stands a much
better chance of reducing that to something more simple &
straightforward, something without case-specific code. Perhaps even
something readable...

However, I agree that the loop is probably the best advice to Mike, the OP.

Dang, I didn't think of that.


Cheers,

- Alf

 
Reply With Quote
 
Seungbeom Kim
Guest
Posts: n/a
 
      01-14-2014
On 2014-01-13 15:39, Luca Risolia wrote:
> Mike Copeland wrote:
>
>> Is it possible to simply (single statement) move the data from one
>> container type (the TeamMap, below) to a different type of container
>> (the TeamVector, below)?

>
> std::for_each(std::make_move_iterator(std::begin(T eamMap)),
> std::make_move_iterator(std::end(TeamMap)),
> [](decltype(TeamMap)::value_type&& v) {
> TeamVector.push_back(std::move(v.second));
> });


(Assuming you want to move the contents)

Given the following declarations:
> struct TeamData;
> typedef std::map<std::string, TeamData> TeamMap; TeamMap teamMap;
> typedef std::vector<TeamData> TeamVector; TeamVector teamVect;


It should read like this:

std::for_each(std::make_move_iterator(std::begin(t eamMap)),
std::make_move_iterator(std::end(teamMap)),
[&](decltype(teamMap)::value_type&& v) {
teamVect.push_back(std::move(v.second));
});

By the way, I think it can be made simpler like this:

std::for_each(std::begin(teamMap),
std::end(teamMap),
[&](decltype(teamMap)::value_type& v) {
teamVect.push_back(std::move(v.second));
});

A function that takes an lvalue parameter and moves things out of it
is dangerous in general, but this is a lambda function used specifically
for moving the contents out of an existing container, so it is okay.
And whether you take an lvalue parameter or an rvalue parameter,
it becomes an lvalue inside the function and you need std::move anyway.

--
Seungbeom Kim
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
STL: Map of maps possible, but no multi-map of maps? Workarounds? Marcus C++ 2 12-09-2005 06:34 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments