Re: set insert problem
On Sep 13, 8:39 pm, Angus <anguscom...@gmail.com> wrote:
Should I not be able to do this:
std::set<int> myset;
std::list<int> intlist;
intlist.push_back(1);
intlist.push_back(2);
intlist.push_back(3);
intlist.push_back(4);
myset.insert(intlist.begin(), intlist.end());
in MSVC v6 I get:
C:\Support\CPP\Set_Test.cpp(32) : error C2664: 'class
std::_Tree<int,int,struct std::set<int,struct std::less<int>,class
std::allocator<int> >::_Kfn,struct std::less<int>,class
std::allocator<int> >::iterator __thiscall std::set<int,struct
std::less
<int>,class std::allocator<int> >::insert(class
std::_Tree<int,int,struct std::set<int,struct std::less<int>,class
std::allocator<int> >::_Kfn,struct std::less<int>,class
std::allocator<int> >::iterator,const int &)' : cannot convert
parameter 1 fro
m 'class std::list<int,class std::allocator<int> >::iterator' to
'class std::_Tree<int,int,struct std::set<int,struct
std::less<int>,class std::allocator<int> >::_Kfn,struct
std::less<int>,class std::allocator<int> >::iterator'
No constructor could take the source type, or constructor
overload resolution was ambiguous
Is this a compiler limitation or am I doing something wrong?
You're doing something wrong. Mainly, using a very outdated
compiler. The above is perfectly legal, and works with up to
date versions of VC++.
Any idea on a workaround if it is a compiler limitation?
If for some reason you cannot change the compiler, then you have
to use std::copy and an insertion iterator:
std::copy( intlist.begin(), intlist.end(),
std::inserter( myset, myset.end() ) ) ;
--
James Kanze