Implementing simple priority queue using vector in C++ - c++

The following gives an error for the code mentioned below.
Where I have gone wrong ?
error: ‘function’ is not a member of ‘std’
I want to make priority queue using C++ std lib queue, and min the queue is that IceCream which takes least time to prep. I have tried implementing this -> declaring a priority_queue in c++ with a custom comparator
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <iterator>
#include <string>
#include <sstream>
using namespace std;
class IceCream
{
public:
int time_to_prep;
IceCream(int flav) {
time_to_prep = flav;
}
};
bool Compare(IceCream a, IceCream b)
{
return a.time_to_prep > b.time_to_prep;
}
int main()
{
priority_queue<IceCream, vector<IceCream>, std::function<bool(IceCream a, IceCream b)> > my_pq(Compare);
my_pq.push(IceCream(4));
my_pq.push(IceCream(33));
my_pq.push(IceCream(9));
cout << my_pq.top() << endl;
return 0;
}

#include <functional>
You need this include to get access to std::function
See: http://en.cppreference.com/w/cpp/utility/functional/function

Related

Referee.cpp:10:18: error: no matching function for call to ‘Human::Human()’ Referee::Referee()

I'm new to c++ and was having problem with doing some inheritance, does any one know why im getting this error? (its the only error I'm getting when compiling).
I compiled with g++ -o. Also I'm so sorry in advance if I'm doing a lot of things wrong here, I'm very new to c++. >_<
Please let me know how I can make my code better or more efficient.
computer.h
#ifndef RPS_H
#define RPS_H
#include <iostream>
#include <string>
#include <stdio.h>
class Computer
{
public:
Computer(std::string);
~Computer();
char charc;
};
#endif
human.h
#ifndef HUMAN_H
#define HUMAN_H
#include <iostream>
#include <string>
#include <stdio.h>
class Human
{
public:
Human(std::string);
~Human();
char charh;
};
#endif
referee.h
#ifndef REFEREE_H
#define REFEREE_H
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
class Referee : public Human{
public:
Referee();
~Referee();
bool Winneris();
};
#endif
Computer.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "computer.h"
using namespace std;
Computer::Computer(string char_c)
{
}
Computer::~Computer()
{
}
Human.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
using namespace std;
Human::Human(string char_h){
char_h=charh;
cout<<"r/p/s?"<<endl;
cin>>charh;
}
Human::~Human()
{
}
Referee.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "referee.h"
using namespace std;
Referee::Referee(){
}
bool Referee::Winneris(){
if (charh=='r'){
cout<<"draw"<<endl;
}
else if(charh=='p'){
cout<<"Victory!"<<endl;
}
else if(charh=='s')
{
cout<<"Defeat"<<endl;
}
return 0;
}
Referee::~ReReferee(){
}
main.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
#include "computer.h"
#include "referee.h"
using namespace std;
string char_h;
string char_c;
// main program
int main()
{
Human *round1h;
round1h = new Human(char_h);
Computer *round1c;
round1c = new Computer(char_c);
Referee *round1r;
round1r = new Referee();
round1r -> Winneris();
}
When you have written the parameterized constructor in respective classes. You have created the class objects, which call the default constructor which takes no parameter.
You have to define the default constructor as well in your respective classes.
Human::Human()
{}
Computer::Computer()
{}
Referee::Referee()
{}
Constructor types

c++ unable to read memory with custom class when calling a function

I am having problems running functions within a custom class. Here is my code:
ROBOTSTRUCTURE.H
#pragma once
#include "math.h"
#include <fstream>
#include <string>
#include <thread>
#include <chrono>
#include <ctime>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
using namespace std::chrono;
class RobotStructure
{
bool faceTrackingEnabled;
public:
RobotStructure();
~RobotStructure();
bool testFunction(string input);
ROBOTSTRUCTURE.CPP
#include "RobotStructure.h"
RobotStructure::RobotStructure()
{
faceTrackingEnabled = true;
}
RobotStructure::~RobotStructure()
{
}
bool RobotStructure::testFunction(string input)
{
cout << input << endl; //THIS DOES NOT WORK, When using debugger shows that the entire class "Robot Structure" as unable to read memory
return true;
}
MAIN
#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <thread>
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
#include <fstream>
#include "RobotStructure.h"
using namespace std;
int main()
{
RobotStructure *mybot= new RobotStructure();
mybot->testFunction("test");
return 0;
}
If a set a breakpoint in main, my class is initialized correctly and everything is fine. As soon as a call the "test function" the class falls out of memory within the test function.
When it goes back to main just before return 0; the class is also out of memory. As if the pointer is deleted somehow. Can someone please explain what is happening and what did I do that is wrong?
You declared the function as bool testFunction(string input) but the function is not returning anything. This leads to undefined behavior in C++.
In addition your example wouldn't compile (you declared an argument input but you are using intput).
Your testFunction does not use any class members. If you compile this code with optimization enabled ("Release"-Configuration in VisualStudio, -O2 on gcc), the compiler might render some variables (like this) "undebuggable".

Invalid type of argument using pointer to struct array

I have a problem with the next code, I get the error: invalid type argument of unary '*' (have 'int'). If I write int *content that allows the code to run, but I have to write int=content and change the code *((ptab->content)+pC1+17), I tried but I can't fix the error.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
struct box{
int content;
};
struct box *ptab;
int pC1=5;
int main (){
ptab=new struct box[64];
if (*((ptab->content)+pC1+17)==0) {
pC1=pC1+17;
}
cout<<pC1<<endl;
}
I have to pass from pointers to poninters to struct arrays, this code is an example, because the original code has 23000 lines.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
int *box;
int pC1=5;
int main (){
box=new int[64];
if (*(box+pC1+17)==0){
pC1=pC1+17;
}
cout<<pC1<<endl;
}
With *((ptab->content)+pC1+17), an easier way to say it is ptab[pC1+17].content [the latter compiles and produces 22 as output]. Did you mean that or ptab->content + pC1 + 17 [which produces 5]?

Error: Expected constructor, destructor, type conversion before '<' token

the code is
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <iterator>
#include <queue>
#include <algorithm>
#include <string>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <new>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
using std::vector;
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/operation.hpp>
#include <boost/numeric/ublas/vector.hpp>
using namespace boost::numeric::ublas;
boost::numeric::ublas::matrix<double> A_MATRIX(A_MATRIX_ROWS,A_MATRIX_COLUMNS);
boost::numeric::ublas::matrix<double> Y_MATRIX(A_MATRIX_ROWS,1);
vector <double> GPSR_BB(boost::numeric::ublas::matrix<double> &f_Y_MATRIX,boost::numeric::ublas::matrix<double> &f_A_MATRIX,int f_tau,int f_tolA){
vector<double> objective(2);
//sth inside function
return objective;
}
int main(){
vector<double> objectives(maxiter+2);
objectives=GPSR_BB(Y_MATRIX,A_MATRIX,tau,tolA);
return 0;
}
in line
vector <double> GPSR_BB(boost::numeric::ublas::matrix<double> &f_Y_MATRIX,boost::numeric::ublas::matrix<double> &f_A_MATRIX,int f_tau,int f_tolA){
I receive error
error: expected constructor, destructor, or type conversion before ‘<’ token function
I guess, the problem is because of matrix data type, from boost library, which I have to pass to function, I don't think there is another way I can do for my specific problem.
Any help is greatly appreciated. Thank you
boost::numeric::ublas has vector as well as namespace std. Try removing using namespace std and using the appropriate namespace to refer to the correct type.
Try without a space between vector and <double>
vector<double> GPSR_BB

no member named 'set' in 'std::vector<int>'

I use QT creator and Xcode 4.63 (osx mav)
I just call .set(....) and got following error
no member named 'set' in 'std::vector<int, std::allocator<int> >
seem like something wrong with lib , since I'm new to QT creator i still got no idea how to make it right. (I'm new to c++)
#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "console.h"
#include "filelib.h"
#include "grid.h"
#include "gwindow.h"
#include "simpio.h"
#include "vector.h"
using namespace std;
int main() {
vector<int> myVector(10,0);
fstream mystream;
string getStream;
while(getline(mystream,getStream)) {
getline(mystream,getStream);
if(stringToInteger(getStream)>=0 && stringToInteger(getStream)<=9) {
myVector.set(0,(myVector[0]+1))
};
}
return 0;
};
The "set" method doesn't exist for vectors in STL. You could use the [] operator if you want to change a value:
myVector[0] = myVector[0] + 1;
or even
myVector[0]++;