|
Boost Users : |
Subject: [Boost-users] shared_ptr to object in stl set container?
From: Galen (gjwilkerson_at_[hidden])
Date: 2011-12-12 22:23:51
Hi,
-- I'm trying to use shared_ptr in a stl set, but am having trouble with find()
Any help? Documentation on this seems hard to find.
-- Also, are the overloaded < and == useful or correct here?
thanks!
Output is: ------------------
0 adding neighbor 1
0 adding neighbor 2
1 adding neighbor 0
2 adding neighbor 1
0's neighbors
1 has 1 neighbors
0
2 has 1 neighbors
1
NOT WORKING
is 1 a neighbor of 0? 0
is 0 a neighbor of 1? 0
Code below: -------------------
#include<set>
#include<iostream>
#include<boost/shared_ptr.hpp>
using namespace std;
//use typedef instead in actual code
#define NodePtr boost::shared_ptr<Node>
// TODO: should this extend shared_ptr?
class Node {
private:
int id;
set<NodePtr> neighbors;
public:
Node() {
}
Node(int newID) {
id = newID;
}
~Node() {
}
int getID() {
return id;
}
int addNeighbor(NodePtr newNode) {
int newID;
newID = newNode->id;
// if not already a neighbor, add
if (!isNeighbor(newID))
neighbors.insert(newNode);
}
set<NodePtr> getNeighbors() {
return neighbors;
}
bool isNeighbor(int searchID) {
set<NodePtr>::iterator it;
// check if this neighbor already in list
NodePtr searchNode(new Node(searchID));
it = neighbors.find(searchNode);
// if not found, add to neighbors
if (it == neighbors.end()) {
return false;
}
//else
return true;
}
//overload == for find()
bool operator== (NodePtr otherNode) {
cout << "==: " << otherNode->id << " " << id << endl;
return (otherNode->id == id);
}
//overload < in case we want to sort
bool operator< (NodePtr otherNode) {
return (id < otherNode->id);
}
};
int main() {
// the nodes are pointers to Node objects
NodePtr n0(new Node(0));
NodePtr n1(new Node(1));
NodePtr n2(new Node(2));
cout << n0->getID() << " adding neighbor " << n1->getID() << endl;
n0->addNeighbor(n1);
cout << n0->getID() << " adding neighbor " << n2->getID() << endl;
n0->addNeighbor(n2);
cout << n1->getID() << " adding neighbor " << n0->getID() << endl;
n1->addNeighbor(n0);
// cout << n2->getID() << " adding neighbor " << n0->getID() <<
endl;
// n2->addNeighbor(n0);
cout << n2->getID() << " adding neighbor " << n1->getID() << endl;
n2->addNeighbor(n1);
set<NodePtr> neighbors = n0->getNeighbors();
set<NodePtr>::iterator NB;
int id = n0->getID();
cout << id << "'s neighbors" << endl;
for (NB = neighbors.begin(); NB != neighbors.end() ; NB++) {
set<NodePtr> neighborsList = (*NB)->getNeighbors();
cout << (*NB)->getID() << " has " << neighborsList.size() << "
neighbors" << endl;
cout << (*neighborsList.begin())->getID() << endl;
}
// http://www.engr.sjsu.edu/wbarrett/SortFind.htm
cout << "NOT WORKING" << endl;
cout << "is 1 a neighbor of 0? " << n0->isNeighbor(1) << endl;
cout << "is 0 a neighbor of 1? " << n1->isNeighbor(0) << endl;
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net