Hi, there
Thanks for the help. I tried it in the way you suggested, but got all the
following errors (I am using gcc 3.3):
/mnt/eniac/home3/c/cis570/install/gcc-3.3/bin/g++ -c test_set.cc
/mnt/eniac/home3/c/cis570/install/gcc-3.3/include/c++/3.3.1/bits/stl_algo.h:
In
function `_OutputIter std::set_union(_InputIter1, _InputIter1,
_InputIter2,
_InputIter2, _OutputIter) [with _InputIter1 =
std::_Rb_tree_iterator<std::string, const std::string&, const
std::string*>,
_InputIter2 = std::_Rb_tree_iterator<std::string, const std::string&,
const
std::string*>, _OutputIter = std::_Rb_tree_iterator<std::string, const
std::string&, const std::string*>]':
test_set.cc:43: instantiated from here
/mnt/eniac/home3/c/cis570/install/gcc-3.3/include/c++/3.3.1/bits/stl_algo.h:
3667: error: passing
`const std::string' as `this' argument of `std::basic_string<_CharT,
_Traits, _Alloc>& std::basic_string<_CharT, _Traits,
_Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&)
[with
_CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>]' discards qualifiers
test_set.cc:43: instantiated from here
/mnt/eniac/home3/c/cis570/install/gcc-3.3/include/c++/3.3.1/bits/stl_algo.h:
3671: error: passing
`const std::string' as `this' argument of `std::basic_string<_CharT,
_Traits, _Alloc>& std::basic_string<_CharT, _Traits,
_Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&)
[with
_CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>]' discards qualifiers
/mnt/eniac/home3/c/cis570/install/gcc-3.3/include/c++/3.3.1/bits/stl_algo.h:
3675: error: passing
`const std::string' as `this' argument of `std::basic_string<_CharT,
_Traits, _Alloc>& std::basic_string<_CharT, _Traits,
_Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&)
[with
_CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>]' discards qualifiers
/mnt/eniac/home3/c/cis570/install/gcc-3.3/include/c++/3.3.1/bits/stl_algobas
e.h: In
function `_OutputIter std::__copy(_InputIter, _InputIter, _OutputIter,
std::input_iterator_tag) [with _InputIter =
std::_Rb_tree_iterator<std::string, const std::string&, const
std::string*>,
_OutputIter = std::_Rb_tree_iterator<std::string, const std::string&,
const
std::string*>]':
/mnt/eniac/home3/c/cis570/install/gcc-3.3/include/c++/3.3.1/bits/stl_algobas
e.h:260: instantiated from `_OutputIter std::__copy_aux2(_InputIter,
_InputIter, _OutputIter, __false_type) [with _InputIter =
std::_Rb_tree_iterator<std::string, const std::string&, const std::string*>,
_OutputIter = std::_Rb_tree_iterator<std::string, const std::string&, const
std::string*>]'
/mnt/eniac/home3/c/cis570/install/gcc-3.3/include/c++/3.3.1/bits/stl_algobas
e.h:303: instantiated from `_OutputIter std::__copy_ni2(_InputIter,
_InputIter, _OutputIter, __false_type) [with _InputIter =
std::_Rb_tree_iterator<std::string, const std::string&, const std::string*>,
_OutputIter = std::_Rb_tree_iterator<std::string, const std::string&, const
std::string*>]'
/mnt/eniac/home3/c/cis570/install/gcc-3.3/include/c++/3.3.1/bits/stl_algobas
e.h:323: instantiated from `_OutputIter std::__copy_ni1(_InputIter,
_InputIter, _OutputIter, __false_type) [with _InputIter =
std::_Rb_tree_iterator<std::string, const std::string&, const std::string*>,
_OutputIter = std::_Rb_tree_iterator<std::string, const std::string&, const
std::string*>]'
/mnt/eniac/home3/c/cis570/install/gcc-3.3/include/c++/3.3.1/bits/stl_algobas
e.h:349: instantiated from `_OutputIter std::copy(_InputIter, _InputIter,
_OutputIter) [with _InputIter = std::_Rb_tree_iterator<std::string, const
std::string&, const std::string*>, _OutputIter =
std::_Rb_tree_iterator<std::string, const std::string&, const
std::string*>]'
/mnt/eniac/home3/c/cis570/install/gcc-3.3/include/c++/3.3.1/bits/stl_algo.h:
3681: instantiated from `_OutputIter std::set_union(_InputIter1,
_InputIter1, _InputIter2, _InputIter2, _OutputIter) [with _InputIter1 =
std::_Rb_tree_iterator<std::string, const std::string&, const std::string*>,
_InputIter2 = std::_Rb_tree_iterator<std::string, const std::string&, const
std::string*>, _OutputIter = std::_Rb_tree_iterator<std::string, const
std::string&, const std::string*>]'
test_set.cc:43: instantiated from here
/mnt/eniac/home3/c/cis570/install/gcc-3.3/include/c++/3.3.1/bits/stl_algobas
e.h:228: error: passing
`const std::string' as `this' argument of `std::basic_string<_CharT,
_Traits, _Alloc>& std::basic_string<_CharT, _Traits,
_Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&)
[with
_CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>]' discards qualifiers
make: *** [test_set.o] Error 1
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:cdupb.94997$Tr4.257267@attbi_s03...
"Tong Wang" <wa******@seas.upenn.edu> wrote... Suppose I have two sets of strings:
set<string> set1;
set<string> set2;
And I want to compute the union of set1 and set2 and store the result
back into set1, how can I do this?
I tried to use the set_union algorithm in the following way, but it
didn't work for me:
set<string>::iterator f1 = set1.begin();
set<string>::iterator f2 = set2.begin();
set<string>::iterator o1 = set1.begin();
set_union(f1, set1.end(), f2, set2.end(), o1);
Can somebody tell me what's wrong with this? Thanks a lot.
Putting the result into one of the original sets has to be a separate
operation. 'set_union' has a requirement that the resulting range
shall not overlap with either of the original ranges. Try
set<string> newset;
set_union(set1.begin(), set1.end(),
set2.begin(), set2.end(),
newset.begin());
swap(set1, newset);
Victor