C++ copy a vector to to a set gives a compiler error

I would like to know how to fix the commented out code at line 20 so that I can std::copy a vector into a set. (I know I can copy the vector into the set using for-loops.) If this cannot be done, could you please explain why I am getting the error shown below.
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
   cout << "Hello World" << endl; 
   vector<int> v1(9,12), v1a(9,14);
   v1[0] = 99;
   
   copy(v1.begin(), v1.end(), v1a.begin() ); // no problem copy vector to vector
   cout << v1[0]  << " " << v1[1] << endl;
   cout << v1a[0] << " " << v1a[1] << " " << *(v1.begin()) << endl;

   set<int> s2( v1.begin(), v1.end() );
   set<int> s3;
//   copy( v1.begin(), v1.end(), s3.begin() );  // Compile error. How to std::copy from vector to std::set?
   
   set<int>::iterator sit = s2.begin(); // can increment this set iterator to get good results
   cout << s2.size() << " " << v1.size() <<  "   "  << *sit << "  " << *++sit << "  " << endl;
   
   return 0;
}

Open in new window


/* OUTPUT
Hello World
99 12
99 12 99
2 9   12  99  
*/

Open in new window


Even if you can fix the above error, would you mind interpreting the below error.
$g++ -o main *.cpp
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from main.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h: In instantiation of ‘static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = int*; _OI = std::_Rb_tree_const_iterator<int>]’:
/usr/include/c++/7/bits/stl_algobase.h:386:44:   required from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = int*; _OI = std::_Rb_tree_const_iterator<int>]’
/usr/include/c++/7/bits/stl_algobase.h:422:45:   required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _OI = std::_Rb_tree_const_iterator<int>]’
/usr/include/c++/7/bits/stl_algobase.h:455:8:   required from ‘_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _OI = std::_Rb_tree_const_iterator<int>]’
main.cpp:19:43:   required from here
/usr/include/c++/7/bits/stl_algobase.h:324:18: error: assignment of read-only location ‘__result.std::_Rb_tree_const_iterator<int>::operator*()’
        *__result = *__first;
        ~~~~~~~~~~^~~~~~~~~~

Open in new window

LVL 33
phoffricAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
User std::inserter

http://en.cppreference.com/w/cpp/iterator/inserter

#include <iostream>
#include <vector>
#include <set>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
   cout << "Hello World" << endl; 
   vector<int> v1(9,12), v1a(9,14);
   v1[0] = 99;
   
   copy(v1.begin(), v1.end(), v1a.begin() ); // no problem copy vector to vector
   cout << v1[0]  << " " << v1[1] << endl;
   cout << v1a[0] << " " << v1a[1] << " " << *(v1.begin()) << endl;

   set<int> s2( v1.begin(), v1.end() );
   set<int> s3;
   copy( v1.begin(), v1.end(), std::inserter(s3, s3.begin()) );  // Compile error. How to std::copy from vector to std::set?
   
   set<int>::iterator sit = s2.begin(); // can increment this set iterator to get good results
   cout << s2.size() << " " << v1.size() <<  "   "  << *sit << "  " << *++sit << "  " << endl;
   
   return 0;
}

Open in new window


The problem is that copy requires the destination to have a random access iterator type, which std::set doesn't provide. The insert iterator gets around this problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phoffricAuthor Commented:
Thanks for the quick response. I didn't know about std::inserter.
0
evilrixSenior Software Engineer (Avast)Commented:
To put it another way, you can't copy directly from a non-associative container into an associative container as order needs to be preserved. Using the insert iterator you are essentially inserting the data into the set and ordering is preserved, whereas copying the data wouldn't preserver associative ordering.

I hope that makes sense :)
1
phoffricAuthor Commented:
>> I hope that makes sense :)
Thanks for the intuitive way of thinking about this issue. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.