A
A B
Hello,
I am trying to do a "set difference" operation, to trying to take some
elements (objects) out of a set. I wrote a small sample program to
test it.
--------------------------------------
#include <iostream>
#include <cstdlib>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
class A {
public:
A();
~A();
};
A::A() {
cout << "Constructing A\n";
}
A::~A() {
cout << "Destructing A\n";
}
int main() {
A a;
A a2;
A a3;
A* ptr_a = &a;
A* ptr_a2 = &a2;
A* ptr_a3 = &a3;
set<A*> set_a;
set_a.insert(ptr_a); set_a.insert(ptr_a2); set_a.insert(ptr_a3);
set<A*> set2_a;
set2_a.insert(ptr_a); set2_a.insert(ptr_a2);
cout << "Before erase\n";
set_a.erase(set2_a.begin(), set2_a.end());
cout << "End of main\n";
return 0;
}
--------------------------------------
The idea is that set_a should contain only ptr_a3 after the "erase"
operation. Instead, what I get is:
Constructing A
Constructing A
Constructing A
Before erase
Segmentation fault
What am I missing? According to the STL documentation for "set",
applying "erase" to an iterator-delimited range should be a valid
operation.
Regards,
Andrey
I am trying to do a "set difference" operation, to trying to take some
elements (objects) out of a set. I wrote a small sample program to
test it.
--------------------------------------
#include <iostream>
#include <cstdlib>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
class A {
public:
A();
~A();
};
A::A() {
cout << "Constructing A\n";
}
A::~A() {
cout << "Destructing A\n";
}
int main() {
A a;
A a2;
A a3;
A* ptr_a = &a;
A* ptr_a2 = &a2;
A* ptr_a3 = &a3;
set<A*> set_a;
set_a.insert(ptr_a); set_a.insert(ptr_a2); set_a.insert(ptr_a3);
set<A*> set2_a;
set2_a.insert(ptr_a); set2_a.insert(ptr_a2);
cout << "Before erase\n";
set_a.erase(set2_a.begin(), set2_a.end());
cout << "End of main\n";
return 0;
}
--------------------------------------
The idea is that set_a should contain only ptr_a3 after the "erase"
operation. Instead, what I get is:
Constructing A
Constructing A
Constructing A
Before erase
Segmentation fault
What am I missing? According to the STL documentation for "set",
applying "erase" to an iterator-delimited range should be a valid
operation.
Regards,
Andrey