map of vectors in STL?

Viewed 86231

I want to have a map of vectors, (but I don't want to use pointer for the internal vector), is it possible?

// define my map of vector
map<int, vector<MyClass> > map;

// insert an empty vector for key 10. # Compile Error
map.insert(pair<int, vector<MyClass> >(10, vector<MyClass>)); 

I know that if I have used pointer for vector, as follows, it would be fine, but I wonder if I can avoid using pointer and use the above data structure (I don't want to manually delete)

// define my map of vector
map<int, vector<MyClass>* > map;

// insert an empty vector for key 10.
map.insert(pair<int, vector<MyClass>* >(10, new vector<MyClass>)); 
9 Answers
// define my map of vector
map<int, vector<MyClass> > map;
MyClass *ptr = new MyClass();
map[0].push_back(ptr);

pushing objects into vector of type MyClass

//usage; map[0][vector_index]
map[0][0]->MyClassMember;

accessing member functions using vector_index

Related