Link to home
Start Free TrialLog in
Avatar of phoffric
phoffric

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

ASKER

Thanks for the quick response. I didn't know about std::inserter.
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 :)
>> I hope that makes sense :)
Thanks for the intuitive way of thinking about this issue. :)