for_each and mem_fun_ref trouble - c++

I use for_each and mem_fun_ref as a example ,but there are some error in compile ,what's the problem
#include<iostream>
#include<algorithm>
#include<set>
#include<iterator>
using namespace std;
class Tst
{
public:
Tst(int a, string b):n(a),s(b)
{}
bool operator<(const Tst& t)const
{
return this->n < t.n;
}
int GetN()const
{
return n;
}
string GetS()const
{
return s;
}
void SetN(int a)
{
n = a;
}
void SetName(string name)
{
s = name;
}
void Print(void)
{
cout <<"n is:" << n <<"\ts is:" << s << endl;
}
private:
int n;
string s;
};
int main(void)
{
typedef set<Tst> TstSet;
TstSet tst;
tst.insert(Tst(10, "abc"));
tst.insert(Tst(1, "def"));
for_each(tst.begin(), tst.end(), mem_fun_ref(&Tst::Print));
return true;
}
:4200: 错误:对‘(std::mem_fun_ref_t) (const Tst&)’的调用没有匹配,是什么原因

std::set's contained objects are const, so you can only call const functions on them. Your Print function should be marked const.

The function should be const, because std::set only works with const objects
void Print(void)const
{
}

Related

Arithmatic operator overloading in c++

#include<iostream>
using namespace std;
class money
{
int rs;
int p;
public:
void setdata (int x , int y)
{rs=x; p=y;}
void show()
{ cout <<rs <<"." <<p; }
money operator += (int a) {
money temp;
temp.rs=rs+a.rs;
temp.p=p+a.p;
return (temp);
}
};
int main() {
money c1,c2;
c1.setdata(8,2);
c2=c1.operator+=(4);
c2.show();
}
Can someone tell me why the operator += overloading doesn't work?
My desiring output is 12.2 but the output i got is 16.2 .
I am sending 4 as argument and i want this argument is added in r (ruppee)
part
#include<iostream>
using namespace std;
class money
{
int rs;
int p;
public:
void setdata (int x , int y)
{rs=x; p=y;}
void show()
{ cout <<rs <<"." <<p; }
money& operator+=(int a)
{ rs += a; return *this; }
};
int main() {
money c1,c2;
c1.setdata(4,2);
c2=c1+=(4); //c2=c1.operator+=(4);
c2.show();
}
Try to use constructor correctly.
For example:
#include <iostream>
using namespace std;
class Example
{
public:
int x;
Example(int a)
{
x=a;
}
Example operator+(Example obj)
{
Example ans(0);
ans=x+obj.x;
return ans;
}
};
int main()
{
Example a(10),b(20);
Example ans=a+b;
cout<<ans.x<<endl;
return 0;
}

Issues creating a vector of class object in c++

I created the following class
#include "cliques.h"
#include "vector"
#include <iostream>
using namespace std;
cliques::cliques(){
}
cliques::cliques(int i) {
clique.push_back(i);
clique_prob = 1;
mclique_prob = 1;
}
cliques::cliques(const cliques& orig) {
}
cliques::~cliques() {
}
void cliques::addvertex(int i) {
clique.push_back(i);
}
double cliques::getclique_prob() const {
return clique_prob;
}
double cliques::getMaxclique_prob() const {
return mclique_prob;
}
void cliques::showVertices() {
for (vector<int>::const_iterator i = clique.begin(); i !=clique.end(); ++i)
cout << *i << ' ';
cout << endl;
}
vector<int> cliques::returnVector() {
return clique;
}
void cliques::setclique_prob(double i) {
clique_prob = i;
}
void cliques::setMaxclique_prob(double i) {
mclique_prob = i;
}
Here's the header file
#include "vector"
#ifndef CLIQUES_H
#define CLIQUES_H
class cliques {
public:
void addvertex(int i);
cliques();
cliques(int i);
cliques(const cliques& orig);
virtual ~cliques();
double getclique_prob() const;
double getMaxclique_prob() const;
void showVertices();
std::vector<int> returnVector();
void setclique_prob(double i);
void setMaxclique_prob(double i);
private:
float clique_prob;
float mclique_prob;
std::vector <int> clique;
};
#endif /* CLIQUES_H */
I want to create a vector of these objects in order to implement a heap
int main(int argc, char** argv) {
cliques temp(1);
cliques temp1(2);
temp.setclique_prob(0.32);
temp.setclique_prob(0.852);
temp.showVertices();
temp1.showVertices();
vector <cliques> max_heap;
max_heap.push_back(temp);
max_heap.push_back(temp1);
double x =max_heap.front().getclique_prob();
cout<<"prob "<<x<<endl;
cliques y = max_heap.front();
y.showVertices();
//make_heap (max_heap.begin(),max_heap.end(),max_iterator());
//sort_heap (max_heap.begin(),max_heap.end(),max_iterator());
return 0;
}
For reasons unknown to me none of my class functions work properly after i create my vector, meaning that while the following function works as intended
temp.showVertices()
the next one doesn't,
y.showVertices()
You miss implementation for
cliques::cliques(const cliques& orig) {
}
STL vector uses copy constructor inside when you add values to it. As your cliques class does not allocate any memory, you can just remove the copy constructor from the code and compiler will generate one for you.

C++ What's wrong with my DFS code?

I have tried to code the DFS algorithm as given in CLRS. Here's the code below. When I run it I got an error as "Your program stopped unexpectedly." When I debugged the code I got this line in the call stack "msvcrt!malloc()" and "operator new(unsigned int)". I'm using CodeBlocks. Where am I wrong?
#include<iostream>
#include<cstdlib>
#include<vector>
#include<list>
#include<utility>
#include<algorithm>
#include<string>
using namespace std;
struct prop
{
int p;
int value;
int d;
int f;
string color;
};
vector<prop>v;
prop make_prop(int a,int b,int c,int d,string e)
{
prop p = {a,b,c,d,e};
return p;
}
class Dfs
{
public:
int time;
vector<list<int> >adj;
Dfs(int nv)
{
v.resize(nv);
adj.resize(nv);
for(int i=0;i<nv;i++)
{
v[i].value = i;
v[i].p = -1;
v[i].color = "WHITE";
}
}
void addinput()
{
adj[0].push_back(1);
adj[0].push_back(2);
adj[0].push_back(3);
adj[1].push_back(0);
adj[1].push_back(3);
adj[2].push_back(0);
adj[2].push_back(3);
adj[3].push_back(0);
adj[3].push_back(1);
adj[3].push_back(2);
}
void dfs();
void dfsvisit(prop);
};
void Dfs::dfs()
{
time = 0;
for(int i=0;i<v.size();i++)
{
if(v[i].color == "WHITE")
{
dfsvisit(v[i]);
}
}
}
void Dfs::dfsvisit(prop m)
{
time++;
m.d = time;
m.color = "GRAY";
int val = m.value;
for(auto it = adj[val].begin();it != adj[val].end();it++)
{
if(v[*it].color == "WHITE")
{
v[*it].p = val;
dfsvisit(v[*it]);
}
}
m.color = "BLACK";
cout<<m.value;
time++;
m.f = time;
}
int main()
{
Dfs d(4);
d.addinput();
d.dfs();
return 0;
}
void Dfs::dfsvisit(prop m) // should be prop&
dfsvisit(prop m) will make a copy of the property while dfsvisit(prop& m) receives a reference, working directly on the property you passed to the function
the stack will overflow!
In function dfsvisit,you pass parameter by value,which will never change the actual parameter.You should pass parameter by reference.
void dfsvisit(prop& m);

no matching function for call to 'MyLinSearch::AddEnumerator(MySeqInFileEnumerator*)'|

*EDITED*Thanks for the previous helps.Got this problem now, doesnt like the pr.AddEnumerator(&t); line of the code.But its the same like in the sample task.
The AddEnumerator() is a method in procedure.hpp :
void AddEnumerator(Enumerator<Item>* en){ enor = en;}.
Procedure is the base class.
Also in seqinfileenumerator.hpp:
class SeqInFileEnumerator : public Enumerator<Item>
and in linsearch.hpp:
class LinSearch : public Procedure<Item>
#include <iostream>
#include "linsearch.hpp"
#include "seqinfileenumerator.hpp"
using namespace std;
struct MyPair
{
int azon;
int osszeg;
friend ifstream& operator>>(ifstream& f, MyPair& df);
};
ifstream& operator>>(ifstream& f, MyPair& df)
{
f >> df.azon >> df.osszeg;
return f;
}
class MyLinSearch: public LinSearch <int, true>
{
bool Cond(const int& e) const
{
return e<=-100000;
}
};
class MySeqInFileEnumerator: public SeqInFileEnumerator <MyPair>
{
public:
MySeqInFileEnumerator(char const * p) : SeqInFileEnumerator<MyPair>(p) { }
void Next()
{
MyPair dx;
f >> dx;
df.azon=dx.azon;
df.osszeg=dx.osszeg;
while(dx.azon==df.azon)
{
dx.osszeg+=df.osszeg;
f >> dx;
}
}
};
int main()
{
MyLinSearch pr;
MySeqInFileEnumerator file_enum("input.txt");
pr.AddEnumerator(&file_enum);
pr.Run();
if (pr.Found())
{
cout << "false " << endl;
}
else cout << "true" << endl;
return 0;
}

Priority queue for user-defined types

I have the below struct:
struct node {
float val;
int count;
}
I have several objects of this struct. Now, I want to insert these objects into a priority queue of STL such that the priority queue orders the items by count. Any idea on how to do so? Preferably a min-heap is preferred. I know how to do the above for primitive data types, not structs
Overload the < operator:
bool operator<(const node& a, const node& b) {
return a.count > b.count;
}
I have reversed the comparison to achieve min heap without passing extra arguments to the priority queue.
Now you use it like this:
priority_queue<node> pq;
...
Edit: take a look at this post which seems to be almost exact duplicate: STL Priority Queue on custom class
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Boxer{
public:
string name;
int strength;
};
struct Comp{
bool operator()(const Boxer& a, const Boxer& b){
return a.strength<b.strength;
}
};
int main(){
Boxer boxer[3];
boxer[0].name="uday", boxer[0].strength=23;
boxer[1].name="manoj", boxer[1].strength=33;
boxer[2].name="rajiv", boxer[2].strength=53;
priority_queue< Boxer, vector<Boxer>, Comp> pq;
pq.push(boxer[0]);
pq.push(boxer[1]);
pq.push(boxer[2]);
Boxer b = pq.top();
cout<<b.name;
//result is Rajiv
return 0;
}
Using greater as comparison function you can use priority queue as min heap,
#include <bits/stdc++.h>
using namespace std;
int main()
{
priority_queue<int,vector<int>,greater<int> >pq;
pq.push(1);
pq.push(2);
pq.push(3);
while(!pq.empty())
{
int r = pq.top();
pq.pop();
cout << r << " ";
}
return 0;
}
Inserting value by changing their sign (using minus (-) for positive number and using plus (+) for negative number we can use priority queue in reversed order.
int main()
{
priority_queue<int>pq2;
pq2.push(-1); //for +1
pq2.push(-2); //for +2
pq2.push(-3); //for +3
pq2.push(4); //for -4
while(!pq2.empty())
{
int r = pq2.top();
pq2.pop();
cout << -r << " ";
}
return 0;
}
For custom data types or classes we need a to tell priority queue a way of knowing on which order it will sort our data.
struct compare
{
bool operator()(const int & a, const int & b)
{
return a>b;
}
};
int main()
{
priority_queue<int,vector<int>,compare> pq;
pq.push(1);
pq.push(2);
pq.push(3);
while(!pq.empty())
{
int r = pq.top();
pq.pop();
cout << r << " ";
}
return 0;
}
For custom structure or class you can use priority_queue in any order. Suppose, we want to sort people in descending order according to their salary and if tie then according to their age.
struct people
{
int age,salary;
};
struct compare {
bool operator()(const people & a, const people & b)
{
if(a.salary==b.salary)
{
return a.age>b.age;
} else {
return a.salary>b.salary;
}
}
};
int main()
{
priority_queue<people,vector<people>,compare> pq;
people person1,person2,person3;
person1.salary=100;
person1.age = 50;
person2.salary=80;
person2.age = 40;
person3.salary = 100;
person3.age=40;
pq.push(person1);
pq.push(person2);
pq.push(person3);
while(!pq.empty())
{
people r = pq.top();
pq.pop();
cout << r.salary << " " << r.age << endl;
}
Same result can be obtained by operator overloading :
struct people
{
int age,salary;
bool operator< (const people & p) const
{
if(salary==p.salary)
{
return age>p.age;
} else {
return salary>p.salary;
}
}
};
In main function :
priority_queue<people> pq;
people person1,person2,person3;
person1.salary=100;
person1.age = 50;
person2.salary=80;
person2.age = 40;
person3.salary = 100;
person3.age=40;
pq.push(person1);
pq.push(person2);
pq.push(person3);
while(!pq.empty())
{
people r = pq.top();
pq.pop();
cout << r.salary << " " << r.age << endl;
}
You need to provide operator< for that struct. Something like:
bool operator<(node const& x, node const& y) {
return x.count < y.count;
}
Now you can use a priority queue from the standard library.
Since C++11, you can write
auto comparer = [](const auto& a, const auto& b) {
return a.priority < b.priority;
};
std::priority_queue<Item, std::vector<Item>, decltype(comparer)> queue(comparer);
We can define user defined comparator class:
Code Snippet :
#include<bits/stdc++.h>
using namespace std;
struct man
{
string name;
int priority;
};
class comparator
{
public:
bool operator()(const man& a, const man& b)
{
return a.priority<b.priority;
}
};
int main()
{
man arr[5];
priority_queue<man, vector<man>, comparator> pq;
for(int i=0; i<3; i++)
{
cin>>arr[i].name>>arr[i].priority;
pq.push(arr[i]);
}
while (!pq.empty())
{
cout<<pq.top().name<<" "<<pq.top().priority;
pq.pop();
cout<<endl;
}
return 0;
}
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Person
{
public:
string name;
int age;
Person(string str,int num)
{
name = str;
age = num;
}
};
// FUNCTOR
class compare
{
public:
bool operator()(Person a,Person b)
{
cout << "Comparing " << a.age << " with " << b.age << endl;
return a.age < b.age;
}
};
int main()
{
int n;
cin >> n;
priority_queue <Person, vector<Person> , compare> pq;
for(int i=1;i<=n;i++)
{
string name;
int x;
cin >> name;
cin >> x;
Person p(name,x);
pq.push(p);
}
int k = 3;
for(int i=0;i<k;i++)
{
Person p = pq.top();
pq.pop();
cout << p.name << " " << p.age << endl;
}
return 0;
}
Operator() is also commonly overloaded to implement functors or function object. For example we have a structure Person which have some default ways of searching and sorting a person by age but we want our customized ways with some other parameter like weight so we may use our own custom functor. Priority queue is one such container which accepts a functor so it knows how to sort the objects of custom data types. Each time a comparison has to be done, a object is instantiated of class compare, and it is passed two objects of person class for comparison.