gpt4 book ai didi

c++ - 2 deque的并集和交集(异常段错误)

转载 作者:太空宇宙 更新时间:2023-11-04 13:10:59 25 4
gpt4 key购买 nike

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

v 是并集 vector ,而 intersec 是交集 vector 。下面是查找并集和交集的代码。如果有人有更简单的解决方案,请分享。最后我想要并集 - 交集。

下面是给出异常段错误的代码。

  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 中的问题。

最佳答案

我认为 set_union() 有一个错误:

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

正确的方式应该是:

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

我想这是一个错字。

关于c++ - 2 deque的并集和交集(异常段错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39951890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com