还是贴个代码吧,意思就是无论什么容器,只要形式上具有push_back函数,就可以用back_inserter,下面代码我自己继承并修改了map,就OK了#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void init_vec( vector<pair<int,string> >& m );
class MyMap:public map< int , string > {
public:
void push_back( const pair<int,string>& n ) {
(*this)[n.first] = n.second;
}
void show() const{
typedef const_iterator CIT;
for( CIT it = begin(); it != end(); ++it ) {
cout << it->first<<":"<<it->second<<endl;
}
}
};
int main() {
vector<pair<int,string> > x;
init_vec(x);
MyMap m;
copy(x.begin(), x.end(), back_inserter(m));
m.show();
}
void init_vec( vector<pair<int,string> >& m ) {
m.push_back(make_pair(1,string("aaa")));
m.push_back(make_pair(2,string("bbb")));
m.push_back(make_pair(3,string("ccc")));
}
|