STL set_intersection运行时错误
STL set_intersection runtime error
我正在编写一个代码,以根据对象的 id 与两个对象向量相交。我收到运行时错误。我试图找出问题,但不知道为什么?你能在这里帮我吗?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Mclass
{
public:
uint64_t id;
string name;
Mclass(uint64_t _id, string _name)
{
id = _id;
name = _name;
}
bool operator<(const Mclass &info) const
{
return (this->id < info.id);
}
Mclass operator=(const Mclass &source)
{
this->id = source.id;
this->name = source.name;
return *this;
}
bool operator==(const Mclass &info) const
{
return (this->id > info.id);
}
};
int main()
{
vector <Mclass> v1;
vector <Mclass> v2;
vector <Mclass> v_in;
v1.push_back(Mclass(1, "a1"));
v1.push_back(Mclass(2, "a2"));
v1.push_back(Mclass(5, "a5"));
v2.push_back(Mclass(2, "b2"));
v2.push_back(Mclass(3, "b3"));
v2.push_back(Mclass(5, "b5"));
for(int i = 0; i < v1.size(); ++i)
{
cout << v1[i].id << ": " << v1[i].name << endl;
}
for(int i = 0; i < v2.size(); ++i)
{
cout << v2[i].id << ": " << v2[i].name << endl;
}
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v_in.begin(),
[](const Mclass& arg1, const Mclass& arg2)
{
return (arg1.id == arg2.id);
});
for(int i = 0; i < v_in.size(); ++i)
{
cout << v_in[i].id << ": " << v_in[i].name << endl;
}
return 0;
}
GDB 日志
(gdb) run
Starting program: /mnt/DATA/Dropbox/ExperimentalCode/CPP/a.out
1: a1
2: a2
5: a5
2: b2
3: b3
5: b5
Program received signal SIGSEGV, Segmentation fault.
0x08049641 in Mclass::operator=(Mclass const&) ()
(gdb) bt
#0 0x08049641 in Mclass::operator=(Mclass const&) ()
#1 0x0804949a in __gnu_cxx::__normal_iterator<Mclass*, std::vector<Mclass, std::allocator<Mclass> > > std::set_intersection<__gnu_cxx::__normal_iterator<Mclass*, std::vector<Mclass, std::allocator<Mclass> > >, __gnu_cxx::__normal_iterator<Mclass*, std::vector<Mclass, std::allocator<Mclass> > >, __gnu_cxx::__normal_iterator<Mclass*, std::vector<Mclass, std::allocator<Mclass> > >, main::{lambda(Mclass const&, Mclass const&)#1}>(__gnu_cxx::__normal_iterator<Mclass*, std::vector<Mclass, std::allocator<Mclass> > >, __gnu_cxx::__normal_iterator<Mclass*, std::vector<Mclass, std::allocator<Mclass> > >, __gnu_cxx::__normal_iterator<Mclass*, std::vector<Mclass, std::allocator<Mclass> > >, __gnu_cxx::__normal_iterator<Mclass*, std::vector<Mclass, std::allocator<Mclass> > >, main::{lambda(Mclass const&, Mclass const&)#1}, main::{lambda(Mclass const&, Mclass const&)#1}) ()
#2 0x08049114 in main ()
您正在尝试将对象写入空向量。您可能打算使用
std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(),
std::back_inserter(v_in),
[](const Mclass& arg1, const Mclass& arg2)
{
return (arg1.id < arg2.id);
});
请注意使用std::back_inserter()
添加到输出范围。此外,正如 Igor Tandetnik 在评论中指出的那样,谓词必须在对象上定义一个严格的弱顺序,而不是相等关系。请注意,输入需要排序(在您的示例中,您创建了按排序的输入)。
相关文章:
- 删除指向指针的指针是运行时错误吗
- c++中的指针和运行时错误
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 对单向链表进行排序时出现运行时错误
- 为什么此代码存在运行时错误?
- 你能解释一下什么运行时错误是如何解决它的吗?
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 运行时错误:引用绑定到类型为"int"的空指针
- 为什么当 vector 为空时会显示运行时错误?
- C++运行时错误与快速排序算法抛出堆栈转储错误
- 运行时错误:矢量下标超出范围:正在检查空集
- 分配给gslice_array会导致运行时错误
- cout 新创建的对象引发运行时错误
- C++在使用std::multimap时出现运行时错误的几率很小
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 试图找出为什么我会收到运行时错误?
- 迭代二维矢量时发生运行时错误
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误