map内存分配的问题
map < CString, vector < T > > MainList;
这样的东西能预先分配内存吗?怎么做呢?
[解决办法]
map/list等节点型容器不需要预分配内存,也不应该需要。
[解决办法]
底层操作不需要你理会,
只要插入元素就可以了 ~
[解决办法]
insert
Syntax:
#include <map>
iterator insert( iterator i, const TYPE& pair );
void insert( input_iterator start, input_iterator end );
pair <iterator,bool> insert( const TYPE& pair );
The function insert() either:
* inserts pair after the element at pos (where pos is really just a suggestion as to where pair should go, since sets and maps are ordered), and returns an iterator to that element.
* inserts a range of elements from start to end.
* inserts pair, but only if pair doesn 't already exist. The return value is an iterator to the element inserted, and a boolean describing whether an insertion took place.
For example, the following code uses the insert() function to insert some data into a map:
map <const char*, int> m;
m.insert( make_pair( "test ",5) );
对内存的分配等细节在 map 内部已经封装完毕,
使用 map 的方法完成操作即可~