1
5,10,15,20,25  // first deque
50,40,30,20,10 // second deque

v is union vector while intersec is intersection vector. Below is the code for finding union and intersection. In case if anyone have more easy solution, pl do share. Finally I want union - intersection.

Below is the code which give unusual segmentation fault.

  deque<int> first = {5,10,15,20,25};
  deque<int> second = {50,40,30,20,10};
  vector<int> v(first.size()+second.size());                      
  vector<int>::iterator it;
  sort (first.begin(),first.end());     //  5 10 15 20 25
  sort (second.begin(),second.end());   // 10 20 30 40 50

  //union
  //line 79
  it=set_union (first.begin(), first.end(), second.begin(), second.end(), v.begin());
  v.resize(it-v.begin());

  // now finding intersection

  vector<int> intersec(first.size()+second.size());                      
  it=set_union (first.begin(), first.end(), first.begin(), second.end(), intersec.begin());
  intersec.resize(it-intersec.begin()); 

Now when compiled with -g flag, I used gdb and following is the backtrace stack

0x000000000040a010 in std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m<std::_Deque_iterator<int, int&, int*>, int*> (__first=<error reading variable: Cannot access memory at address 0x0>, __last=0, __result=0x623ef0) at /usr/include/c++/5/bits/stl_algobase.h:340
340           *__result = *__first;
(gdb) bt
#0  0x000000000040a010 in std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m<std::_Deque_iterator<int, int&, int*>, int*> (
    __first=<error reading variable: Cannot access memory at address 0x0>, __last=0, __result=0x623ef0) at /usr/include/c++/5/bits/stl_algobase.h:340
#1  0x0000000000409156 in std::__copy_move_a<false, std::_Deque_iterator<int, int&, int*>, int*> (__first=0, __last=0, __result=0x623d04)
    at /usr/include/c++/5/bits/stl_algobase.h:402
#2  0x00000000004077d2 in std::__copy_move_a2<false, std::_Deque_iterator<int, int&, int*>, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > >
    (__first=0, __last=0, __result=0) at /usr/include/c++/5/bits/stl_algobase.h:438
#3  0x0000000000405c99 in std::copy<std::_Deque_iterator<int, int&, int*>, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > > (__first=0, 
    __last=0, __result=0) at /usr/include/c++/5/bits/stl_algobase.h:472
#4  0x0000000000404020 in std::__set_union<std::_Deque_iterator<int, int&, int*>, std::_Deque_iterator<int, int&, int*>, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__ops::_Iter_less_iter> (__first1=0, __last1=0, __first2=0, __last2=0, __result=0, __comp=...)
    at /usr/include/c++/5/bits/stl_algo.h:4965
#5  0x0000000000402b8c in std::set_union<std::_Deque_iterator<int, int&, int*>, std::_Deque_iterator<int, int&, int*>, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > > (__first1=5, __last1=0, __first2=5, __last2=0, __result=5) at /usr/include/c++/5/bits/stl_algo.h:5011
#6  0x0000000000401b3b in main () at test.cpp:79

I am not able to understand the problem in bt.

timrau
  • 22,578
  • 4
  • 51
  • 64
Mohit Kumar
  • 500
  • 6
  • 24
  • 3
    typo: `it=set_union (first.begin(), first.end(), first.begin(), second.end(), intersec.begin());` should be `it=set_union (first.begin(), first.end(), second.begin(), second.end(), intersec.begin());`. Also you are not actually calling `set_intersection` there. –  Oct 10 '16 at 05:51
  • Oh, dayum , Did the work, working perfectly now, Thanks. You made my day – Mohit Kumar Oct 10 '16 at 05:59

1 Answers1

0

I think that there is a mistake in set_union() :

it=set_union (first.begin(), first.end(), first.begin(), second.end(), intersec.begin());

Correct way Should be :

it=set_union (first.begin(), first.end(), second.begin(), second.end(), intersec.begin());

I guess it is a typo.

adisticated
  • 469
  • 2
  • 10