set insert problem

A

Angus

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? Any idea
on a workaround if it is a compiler limitation?
 
F

Francesco

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?  Any idea
on a workaround if it is a compiler limitation?

That's fine, and works for me (GCC).

HTH,
Francesco
 
F

Francesco

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?  Any idea
on a workaround if it is a compiler limitation?

Sorry, overlooked this part. You could write an explicit loop to do
the insertion, eventually localizing it in a function.

You could get better help on this specific issue on a compiler
specific group.

Cheers,
Francesco
 
J

James Kanze

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() ) ) ;
 
B

Balog Pal

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: ...
Is this a compiler limitation or am I doing something wrong? Any idea
on a workaround if it is a compiler limitation?

The code is fine, MSVC 6.0 is a pre-standard beast wich you should not use,
or at least with std:: stuff. If you absolutely must, at least check out
the replacement standard libs, possibly that would accept this code. IIRC
Dincumware offered a lib upgrade for ~$50. The normal solution is
obviously to switch to VS2008 (free ed. available)
 
A

Angus

The code is fine, MSVC 6.0 is a pre-standard beast wich you should not use,
or at least with std:: stuff.   If you absolutely must, at least check out
the replacement standard libs, possibly that would accept this code.  IIRC
Dincumware offered a lib upgrade for ~$50.   The normal solution is
obviously to switch to VS2008 (free ed. available)

I use this compiler at work ... so I need to know the limitations.

Anyway the only workaround I could find was to iterate thru the list
elements and insert each one :)

I wasn't aware of $50 upgrade. I will enquire.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top