Coyping data from a std::mapint,something to a std::vectorsomething
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
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?
i have a std::map<int,something> and a std::vector<something> and i want to copy the something part of the map...
Do you want to keep the same indexes in the map and the vector, or does the order not matter?
i need the same indices...and i don't know how to iterrate through a map using an iterrator ...
regards,
m4gnus
regards,
m4gnus
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]
Wow...thanks for the fast responses. Irt works now.
This is almost like chatting :)
regards,
m4gnus
This is almost like chatting :)
regards,
m4gnus
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:
regards,
m4gnus
@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
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 )"
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 )"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement