Jump to content
  • Advertisement
Sign in to follow this  
m4gnus

Coyping data from a std::mapint,something to a std::vectorsomething

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi The topic says what i want to do... You might think now: "Ha that's eays just do a for loop and do vector=map" that's what i thought first too....but for some reason this doesn't seem to work :/ can somebody tell me a way to copy all the data? regards, m4gnus

Share this post


Link to post
Share on other sites
Advertisement
std::maps have two template parameters: a key and a value. vectors have only one template parameter: the held type. What part of the map do you want to copy into the vector?

Share this post


Link to post
Share on other sites
i have a std::map<int,something> and a std::vector<something> and i want to copy the something part of the map...

Share this post


Link to post
Share on other sites
i need the same indices...and i don't know how to iterrate through a map using an iterrator ...

regards,
m4gnus

Share this post


Link to post
Share on other sites
Quote:
Original post by m4gnus
i need the same indices...and i don't know how to iterrate through a map using an iterrator ...

regards,
m4gnus


This is simple.

#include <map>
#include <vector>
using namespace std;

int main () {
typedef int data_type;

map< int , data_type > a_map;
//...fill map with data...

vector< data_type > a_vector;

/* resize the vector to map's highest key/index value. Since map items are
* sorted by key, the last item will have the highest key (note: I'm
* assuming no custom sorting method has been provided. If there is one,
* we might as well just resize whenever we find a bigger key in the loop.
* see the commented out line in the loop for how to do that).
*
* The key/value pairs are stored in std::pairs as first/last.
*/

a_vector.resize( a_map.last()->first + 1 );

for ( map< int , data_type >::iterator i = a_map.begin() ; i != a_map.end() ; ++i ) {
int key = i->first;
const data_type & value = i->second;

//if ( a_vector.size() < key ) a_vector.resize( key + 1 );
a_vector[ key ] = value;
}
}






[Edited by - MaulingMonkey on August 22, 2005 6:22:49 PM]

Share this post


Link to post
Share on other sites
Wow...thanks for the fast responses. Irt works now.
This is almost like chatting :)

regards,
m4gnus

Share this post


Link to post
Share on other sites
Ok tht was too fast...compiler error: f:\Programmiertes\nGin\ngMesh.cpp(194): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'complVertex' (or there is no acceptable conversion)

@Vector[key]=value;

Edit: My Code:

Verts.resize(VertexMap.end()->first);

//for(int i=0;i<VertexMap.size();i++)Verts=VertexMap.find(i);
for(std::map<int,complVertex>::iterator MapIt=VertexMap.begin();MapIt!=VertexMap.end();++MapIt)
{
int key=MapIt->first;
complVertex & value = MapIt->second;
Verts=VertexMap;
Verts[key]=value;
}



regards,
m4gnus

Share this post


Link to post
Share on other sites
Verts=VertexMap; //<-- this one is commented out right? You don't even have an I variable anymore!

Verts[key]=value; //<-- this one should work if Verts is-a "std::vector< complVertex >" and complVertex has an appropriate assignment operator (if you've defined any custom ones, there should be a function: "complVertex & complVertex::operator=( const complVertex & copyee )"

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!