Better/prettier way to copy from one set to another?

J

Jim Langston

I normally do my own iterations using containers, but have been striving to
learn the standard algorithms and use them. One one point in my code I
simply want to elements from one set into another. This is what I was able
to piece together copying the elements from std::set<size_t> Triangles to
std::set<size_t> SelectedSet:

std::copy( Triangles.begin(), Triangles.end(),
std::insert_iterator<std::set<size_t> >( SelectedSet,
SelectedSet.begin() ) );

std::insert_iterator<std::set<size_t> >( SelectedSet, SelectedSet.begin() )
is a mouthful. Is there a simpler way?
 
W

werasm

I normally do my own iterations using containers, but have been striving to
learn the standard algorithms and use them. One one point in my code I
simply want to elements from one set into another. This is what I was able
to piece together copying the elements from std::set<size_t> Triangles to
std::set<size_t> SelectedSet:

std::copy( Triangles.begin(), Triangles.end(),
std::insert_iterator<std::set<size_t> >( SelectedSet,
SelectedSet.begin() ) );

std::insert_iterator<std::set<size_t> >( SelectedSet, SelectedSet.begin() )
is a mouthful. Is there a simpler way?

std::inserter( SelectedSet, SelectedSet.begin() );

//...
#include <set>
#include <iterator>

int main()
{
std::set<int> s1;
std::set<int> s2;

std::copy(
s1.begin(), s1.end(),
std::inserter( s2, s2.begin() ) );
return 0;
}

Regards,

Werner
 
J

Jim Langston

werasm said:
std::inserter( SelectedSet, SelectedSet.begin() );

//...
#include <set>
#include <iterator>

int main()
{
std::set<int> s1;
std::set<int> s2;

std::copy(
s1.begin(), s1.end(),
std::inserter( s2, s2.begin() ) );
return 0;
}

Ahh, thanks. I had tried std::back_inserter and was getting an error that
set didn't have .push_back. So many inserters. Thanks again.
 
J

Jeff F

Jim Langston said:
I normally do my own iterations using containers, but have been striving to
learn the standard algorithms and use them. One one point in my code I
simply want to elements from one set into another. This is what I was able
to piece together copying the elements from std::set<size_t> Triangles to
std::set<size_t> SelectedSet:

std::copy( Triangles.begin(), Triangles.end(),
std::insert_iterator<std::set<size_t> >( SelectedSet,
SelectedSet.begin() ) );

std::insert_iterator<std::set<size_t> >( SelectedSet,
SelectedSet.begin() )
is a mouthful. Is there a simpler way?

std::set<std::size_t> SelectedSet( Triangles );

Jeff Flinn
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top