Two Std::map, one is a, one is b,a,b inside all have elements, how to assign the elements in a to B, the first thought
Std::copy (A.begin (), A.end (), Std::back_inserter (B));
The compilation failed, and later, by looking at the original code of the C + + standard library, found the objects constructed inside the Back_inserter
TEMPLATE FUNCTION Back_inserter
Template<class _container> Inline
Back_insert_iterator<_container> Back_inserter (_container& _cont)
{//return a Back_insert_iterator
Return (std::back_insert_iterator<_container> (_cont));
}
Std::back_insert_iterator inside the operator= () that's what it says.
back_insert_iterator<_container>& operator= (
TypeName _container::const_reference _val)
{//Push value into container
Container->push_back (_val);
return (*this);
}
This requires that the incoming container must have a push_back () method, but the std::map does not provide such a method, a cup. Imitate the implementation of the standard library, rewrite the method, realize the use of std::copy to Std::map.
TEMPLATE CLASS Back_insert_iterator
Template<class _container>
Class Back_insert_iterator_map
: Public _outit
{//wrap pushes to back of container as output iterator
Public
typedef _container CONTAINER_TYPE;
typedef typename _container::reference Reference;
Explicit Back_insert_iterator_map (_container& _cont)
: Container (&_cont)
{//Construct with container
}
back_insert_iterator_map<_container>& operator= (
TypeName _container::const_reference _val)
{//Push value into container
Container->insert (_val);
return (*this);
}
back_insert_iterator_map<_container>& operator* ()
{//pretend to return designated value
return (*this);
}
back_insert_iterator_map<_container>& operator++ ()
{//pretend to Preincrement
return (*this);
}
back_insert_iterator_map<_container> operator++ (int)
{//pretend to Postincrement
return (*this);
}
Protected
_container *container; Pointer to container
};
TEMPLATE FUNCTION Back_inserter
Template<class _container> Inline
Back_insert_iterator_map<_container> Map_back_inserter (_container& _cont)
{//return a Back_insert_iterator
Return (back_insert_iterator_map<_container> (_cont));
}
A way to multimap the data in a duplicate
int main (int argc,char *argv[])
{
Std::map<std::string,std::string> Iodat;
iodat["1"] = "1";
iodat["2"] = "2";
Std::map<std::string,std::string> M_version_map;
M_version_map.insert (Std::make_pair ("1", "3"));
M_version_map.insert (Std::make_pair ("3", "3"));
Std::copy (Iodat.begin (), Iodat.end (), Map_back_inserter (M_version_map));
This sentence is not compiled but
Std::copy (Iodat.begin (), Iodat.end (), Std::back_inserter (M_version_map));
Has entered the high man instruction, the following this sentence actually also can achieve the effect, has adopted the Std::inseter the iterative device
Std::copy (Iodat.begin (), Iodat.end (), Std::inserter (M_version_map,m_version_map.begin ()));
After the expert guidance, the following sentence actually can achieve results, using the map of the Insert method
Iodat.insert (M_version_map.begin (), M_version_map.end ());
Std::map<std::string,std::string>::const_iterator it;
for (it = M_version_map.begin (); it!= m_version_map.end (); ++it)
{
std::cout<<it->first<< ":" <<it->second<<endl;
}
System ("pause");
}
In this way, when the call Std::copy is implemented, it is also applicable to Std::map. If you are only exchanging two map data, you can use the Swap method.
Of course, it is possible to use a loop to iterate through the values in the two map, one assignment.
The same Code,
VS2003 compilation in Windows takes time:
Std::copy+std::inserter in Linux using gcc3.3 compilation, time-consuming:
Std::copy+std::inserter < Std::map.insert < for (iterator ...) Realize
VS2005 compilation in Windows takes time:
Std::copy+std::inserter is slightly equal to Std::map.insert < for (iterator ...) Realize
gcc4.1 compilation in Linux takes time:
Std::map.insert < Std::copy+std::inserter < for (iterator ...) Realize
The implementation of the STL difference is a bit big