5,10,15,20,25 // first deque
50,40,30,20,10 // second deque
v
是联合向量,intersec
而是交叉向量。下面是查找并集和交集的代码。如果有人有更简单的解决方案,请分享。最后我想要联合 - 交叉点。
下面是给出异常分段错误的代码。
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());
现在,当使用 -g 标志编译时,我使用了 gdb,以下是回溯堆栈
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
我无法理解 bt 中的问题。