C
CupOfWater
Hi, I'm getting some errors using remove_if that I can not fix at all.
Let me paste my code and also the error here. I went on IRC and
people were mostly no help. The compiler is g++ 3.2.3. Thanks in
advance.
/usr/include/c++/3.2.3/bits/stl_algo.h: In function `_OutputIter
std::remove_copy_if(_InputIter, _InputIter, _OutputIter,
_Predicate) [with
_InputIter = std::_Rb_tree_iterator<std:air<Algo489::node_t,
Algo489::node_t>, const std:air<Algo489::node_t,
Algo489::node_t>&, const
std:air<Algo489::node_t, Algo489::node_t>*>, _OutputIter =
std::_Rb_tree_iterator<std:air<Algo489::node_t, Algo489::node_t>,
const
std:air<Algo489::node_t, Algo489::node_t>&, const
std:air<Algo489::node_t, Algo489::node_t>*>, _Predicate =
Algo489::Graph::equal_edge_cmp]':
/usr/include/c++/3.2.3/bits/stl_algo.h:1085: instantiated from
`_ForwardIter std::remove_if(_ForwardIter, _ForwardIter, _Predicate)
[with _ForwardIter = std::_Rb_tree_iterator<std:air<Algo489::node_t,
Algo489::node_t>, const std:air<Algo489::node_t, Algo489::node_t>&,
const std:air<Algo489::node_t, Algo489::node_t>*>, _Predicate =
Algo489::Graph::equal_edge_cmp]'
algo.h:116: instantiated from here
/usr/include/c++/3.2.3/bits/stl_algo.h:1017: passing `const
std:air<Algo489::node_t, Algo489::node_t>' as `this' argument of
`
std:air<Algo489::node_t, Algo489::node_t>&
std:air<Algo489::node_t,
Algo489::node_t>:perator=(const std:air<Algo489::node_t,
Algo489::node_t>&)' discards qualifiers
// Header
#ifndef __ALGO489__H_
#define __ALGO489__H_
#include <netinet/in.h>
#include <sys/types.h>
#include <iostream>
#include <iterator>
#include <vector>
#include <functional>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
namespace Algo489 {
struct node_t {
node_t(){}
node_t(std::string ip, u_short port, char type):ip_(ip),
port_(port), type_(type) {}
std::string ip_;
u_short port_;
char type_;
};
bool operator==(const node_t& lhs, const node_t& rhs);
struct Graph {
typedef std:air<node_t, node_t> edge_t;
struct less_node_cmp : public std::binary_function<node_t, node_t,
bool> {
bool operator() (const node_t& f, const node_t& s) const
{
if(f==s)
return false;
else {
return strcmp(f.ip_.c_str(), s.ip_.c_str());
}
}
};
struct equal_edge_cmp {
equal_edge_cmp(const edge_t& e) : edge(e) {}
bool operator() (const edge_t& e) const
{
return ((e.first == edge.first && e.second == edge.second) ||
(e.first == edge.second && e.second == edge.first));
}
edge_t edge;
};
struct less_edge_cmp : public std::binary_function<node_t, node_t,
bool> {
bool operator() (const edge_t& f, const edge_t& s) const
{
bool lessthan;
equal_edge_cmp e(f);
if(e(s)) {
return false;
}
else {
return strcmp(f.first.ip_.c_str(), s.first.ip_.c_str());
}
}
};
struct print_node {
void operator() (const node_t& e) const
{
std::cout << e.ip_ << " " << e.port_ << " " << e.type_ <<
std::endl;
}
};
struct print_conn {
void operator() (const edge_t& e) const
{
std::cout << e.first.ip_ << " " << e.first.port_ << " " <<
e.first.type_;
std::cout << " connected to: ";
std::cout << e.second.ip_ << " " << e.second.port_ << " " <<
e.second.type_;
std::cout << std::endl;
}
};
typedef std::set<node_t, less_node_cmp> nodes_t;
typedef std::set<edge_t, less_edge_cmp> vertices_t;
// functions
node_t addentry(const node_t& e)
{
return *nodes_.insert(e).first;
}
virtual bool rmentry(const node_t& e)
{
for(Graph::nodes_t::iterator i = nodes_.begin(); i != nodes_.end();
++i) {
if(*i == e) {
print_node p;
std::cout << "removing node: "; p(*i);
nodes_.erase(i);
return true;
}
}
return false;
}
// remove the links that contain that node
/*
Graph::vertices_t::iterator i = links_.begin();
while(i != links_.end()) {
Graph::edge_t tmp = *i++;
Graph::disconnect(tmp.first, e);
Graph::disconnect(tmp.second, e);
}
*/
virtual bool connect(const node_t& f, const node_t& s)
{
bool found1=false, found2=false;
for(Graph::nodes_t::iterator i = nodes_.begin(); i != nodes_.end();
++i) {
if(*i == f) found1=true;
if(*i == s) found2=true;
if(found1 && found2) {
print_node p;
links_.insert(std::make_pair(f,s));
return true;
}
}
std::cout << "one or more nodes are not found\n";
return false;
}
virtual bool disconnect(const node_t& f, const node_t& s)
{
edge_t tmp = std::make_pair(f,s);
vertices_t::iterator end = std::remove_if(links_.begin(),
links_.end(), equal_edge_cmp(tmp));
if(end == links_.end())
return false;
links_.erase(end, links_.end());
return true;
}
virtual void print() const
{
for_each(nodes_.begin(), nodes_.end(), print_node());
std::cout << "********************************************************************************\n";
for_each(links_.begin(), links_.end(), print_conn());
std::cout << "********************************************************************************\n";
}
protected:
nodes_t nodes_;
vertices_t links_;
};
void algotest();
}
#endif
//CPP
#include "algo.h"
namespace Algo489 {
bool operator==(const node_t& lhs, const node_t& rhs)
{
return !strcmp(lhs.ip_.c_str(), rhs.ip_.c_str()) && lhs.port_ ==
rhs.port_ && lhs.type_ == rhs.type_;
}
void algotest()
{
Graph db;
std::vector<node_t> incoming;
incoming.push_back(node_t("141.213.12.4", 21448, 'r'));
incoming.push_back(node_t("141.212.12.4", 2447, 'h'));
incoming.push_back(node_t("141.211.12.4", 1448, 'r'));
incoming.push_back(node_t("141.210.12.4", 148, 'h'));
incoming.push_back(node_t("141.209.12.4", 21448, 'h'));
incoming.push_back(node_t("141.208.12.4", 1445, 'r'));
incoming.push_back(node_t("141.207.12.4", 21444, 'h'));
incoming.push_back(node_t("141.206.12.4", 1445, 'h'));
incoming.push_back(node_t("141.205.12.4", 2445, 'r'));
incoming.push_back(node_t("141.204.12.4", 438, 'r'));
incoming.push_back(node_t("141.203.12.4", 21449, 'r'));
for(std::vector<node_t>::iterator i = incoming.begin();
i!=incoming.end(); ++i) {
db.addentry(*i);
}
db.print();
std::cout << "**********************************************\n";
db.connect(
node_t("141.203.12.4", 21449, 'r'),
node_t("121.203.12.4", 21449, 'r')
);
db.connect(
incoming[0],
incoming[1]
);
db.connect(
incoming[1],
incoming[0]
);
db.connect(
incoming[0],
incoming[1]
);
db.connect(
incoming[2],
incoming[4]
);
db.connect(
incoming[6],
incoming[7]
);
db.print();
db.disconnect(
incoming[1],
incoming[0]
);
db.print();
//db.connect(e2, e);
db.rmentry(incoming[0]);
std::cout << "after remove" << std::endl;
db.print();
}
}
Let me paste my code and also the error here. I went on IRC and
people were mostly no help. The compiler is g++ 3.2.3. Thanks in
advance.
/usr/include/c++/3.2.3/bits/stl_algo.h: In function `_OutputIter
std::remove_copy_if(_InputIter, _InputIter, _OutputIter,
_Predicate) [with
_InputIter = std::_Rb_tree_iterator<std:air<Algo489::node_t,
Algo489::node_t>, const std:air<Algo489::node_t,
Algo489::node_t>&, const
std:air<Algo489::node_t, Algo489::node_t>*>, _OutputIter =
std::_Rb_tree_iterator<std:air<Algo489::node_t, Algo489::node_t>,
const
std:air<Algo489::node_t, Algo489::node_t>&, const
std:air<Algo489::node_t, Algo489::node_t>*>, _Predicate =
Algo489::Graph::equal_edge_cmp]':
/usr/include/c++/3.2.3/bits/stl_algo.h:1085: instantiated from
`_ForwardIter std::remove_if(_ForwardIter, _ForwardIter, _Predicate)
[with _ForwardIter = std::_Rb_tree_iterator<std:air<Algo489::node_t,
Algo489::node_t>, const std:air<Algo489::node_t, Algo489::node_t>&,
const std:air<Algo489::node_t, Algo489::node_t>*>, _Predicate =
Algo489::Graph::equal_edge_cmp]'
algo.h:116: instantiated from here
/usr/include/c++/3.2.3/bits/stl_algo.h:1017: passing `const
std:air<Algo489::node_t, Algo489::node_t>' as `this' argument of
`
std:air<Algo489::node_t, Algo489::node_t>&
std:air<Algo489::node_t,
Algo489::node_t>:perator=(const std:air<Algo489::node_t,
Algo489::node_t>&)' discards qualifiers
// Header
#ifndef __ALGO489__H_
#define __ALGO489__H_
#include <netinet/in.h>
#include <sys/types.h>
#include <iostream>
#include <iterator>
#include <vector>
#include <functional>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
namespace Algo489 {
struct node_t {
node_t(){}
node_t(std::string ip, u_short port, char type):ip_(ip),
port_(port), type_(type) {}
std::string ip_;
u_short port_;
char type_;
};
bool operator==(const node_t& lhs, const node_t& rhs);
struct Graph {
typedef std:air<node_t, node_t> edge_t;
struct less_node_cmp : public std::binary_function<node_t, node_t,
bool> {
bool operator() (const node_t& f, const node_t& s) const
{
if(f==s)
return false;
else {
return strcmp(f.ip_.c_str(), s.ip_.c_str());
}
}
};
struct equal_edge_cmp {
equal_edge_cmp(const edge_t& e) : edge(e) {}
bool operator() (const edge_t& e) const
{
return ((e.first == edge.first && e.second == edge.second) ||
(e.first == edge.second && e.second == edge.first));
}
edge_t edge;
};
struct less_edge_cmp : public std::binary_function<node_t, node_t,
bool> {
bool operator() (const edge_t& f, const edge_t& s) const
{
bool lessthan;
equal_edge_cmp e(f);
if(e(s)) {
return false;
}
else {
return strcmp(f.first.ip_.c_str(), s.first.ip_.c_str());
}
}
};
struct print_node {
void operator() (const node_t& e) const
{
std::cout << e.ip_ << " " << e.port_ << " " << e.type_ <<
std::endl;
}
};
struct print_conn {
void operator() (const edge_t& e) const
{
std::cout << e.first.ip_ << " " << e.first.port_ << " " <<
e.first.type_;
std::cout << " connected to: ";
std::cout << e.second.ip_ << " " << e.second.port_ << " " <<
e.second.type_;
std::cout << std::endl;
}
};
typedef std::set<node_t, less_node_cmp> nodes_t;
typedef std::set<edge_t, less_edge_cmp> vertices_t;
// functions
node_t addentry(const node_t& e)
{
return *nodes_.insert(e).first;
}
virtual bool rmentry(const node_t& e)
{
for(Graph::nodes_t::iterator i = nodes_.begin(); i != nodes_.end();
++i) {
if(*i == e) {
print_node p;
std::cout << "removing node: "; p(*i);
nodes_.erase(i);
return true;
}
}
return false;
}
// remove the links that contain that node
/*
Graph::vertices_t::iterator i = links_.begin();
while(i != links_.end()) {
Graph::edge_t tmp = *i++;
Graph::disconnect(tmp.first, e);
Graph::disconnect(tmp.second, e);
}
*/
virtual bool connect(const node_t& f, const node_t& s)
{
bool found1=false, found2=false;
for(Graph::nodes_t::iterator i = nodes_.begin(); i != nodes_.end();
++i) {
if(*i == f) found1=true;
if(*i == s) found2=true;
if(found1 && found2) {
print_node p;
links_.insert(std::make_pair(f,s));
return true;
}
}
std::cout << "one or more nodes are not found\n";
return false;
}
virtual bool disconnect(const node_t& f, const node_t& s)
{
edge_t tmp = std::make_pair(f,s);
vertices_t::iterator end = std::remove_if(links_.begin(),
links_.end(), equal_edge_cmp(tmp));
if(end == links_.end())
return false;
links_.erase(end, links_.end());
return true;
}
virtual void print() const
{
for_each(nodes_.begin(), nodes_.end(), print_node());
std::cout << "********************************************************************************\n";
for_each(links_.begin(), links_.end(), print_conn());
std::cout << "********************************************************************************\n";
}
protected:
nodes_t nodes_;
vertices_t links_;
};
void algotest();
}
#endif
//CPP
#include "algo.h"
namespace Algo489 {
bool operator==(const node_t& lhs, const node_t& rhs)
{
return !strcmp(lhs.ip_.c_str(), rhs.ip_.c_str()) && lhs.port_ ==
rhs.port_ && lhs.type_ == rhs.type_;
}
void algotest()
{
Graph db;
std::vector<node_t> incoming;
incoming.push_back(node_t("141.213.12.4", 21448, 'r'));
incoming.push_back(node_t("141.212.12.4", 2447, 'h'));
incoming.push_back(node_t("141.211.12.4", 1448, 'r'));
incoming.push_back(node_t("141.210.12.4", 148, 'h'));
incoming.push_back(node_t("141.209.12.4", 21448, 'h'));
incoming.push_back(node_t("141.208.12.4", 1445, 'r'));
incoming.push_back(node_t("141.207.12.4", 21444, 'h'));
incoming.push_back(node_t("141.206.12.4", 1445, 'h'));
incoming.push_back(node_t("141.205.12.4", 2445, 'r'));
incoming.push_back(node_t("141.204.12.4", 438, 'r'));
incoming.push_back(node_t("141.203.12.4", 21449, 'r'));
for(std::vector<node_t>::iterator i = incoming.begin();
i!=incoming.end(); ++i) {
db.addentry(*i);
}
db.print();
std::cout << "**********************************************\n";
db.connect(
node_t("141.203.12.4", 21449, 'r'),
node_t("121.203.12.4", 21449, 'r')
);
db.connect(
incoming[0],
incoming[1]
);
db.connect(
incoming[1],
incoming[0]
);
db.connect(
incoming[0],
incoming[1]
);
db.connect(
incoming[2],
incoming[4]
);
db.connect(
incoming[6],
incoming[7]
);
db.print();
db.disconnect(
incoming[1],
incoming[0]
);
db.print();
//db.connect(e2, e);
db.rmentry(incoming[0]);
std::cout << "after remove" << std::endl;
db.print();
}
}