I am trying to learn how to use priority_queue vs queue. I have this code for a priority_queue that's supposed to get input from users. The input is a chore and the priority number of the chore. They can enter as many as they want and it's supposed to output them in order. My problem is that it's not outputting them in order. I'm not sure if it's in my main or in my overloaded < operator function. Here is my Chore.h code:
#include<iostream>
#include<fstream>
using namespace std;
//#ifndef CHORE_H;
//#define CHORE_H;
class Chore{
public:
Chore(){priorityNum=0;choreName="";}
std::string getName (Chore c1)const{return choreName;}
int getPriorityNum(Chore c1)const{return priorityNum;}
bool operator <(const Chore c1)const;
std::ostream& output(std::ostream& cout,const Chore c)const;
std::istream& input(std::istream& cin);
private:
std::string choreName;
int priorityNum;
};
//#endif
Here's my Chore.cc code:
#include"Chore.h"
using namespace std;
bool Chore::operator <(const Chore c1)const{
Chore c2;
int c1Num=c1.getPriorityNum(c1);
int c2Num=c2.getPriorityNum(c2);
return c1Num<c2Num;
/*if(c1Num<c2Num)
return true;
else if(c1Num==c2Num)
return true;
else
return false;*/
}
std::ostream& Chore::output(std::ostream& cout,const Chore c)const{
cout<<endl<<"Chore: "<<getName(c)<<endl;
cout<<"chore Priority: "<<getPriorityNum(c);
}
std::istream& Chore::input(std::istream& cin){
cout<<endl<< "Enter chore name:";
cin >>choreName;
cout<<endl<<"Enter priority number:";
cin >>priorityNum;
}
my main is below:
#include<queue>
#include<iostream>
#include"Chore.h"
using namespace std;
int main(){
std::priority_queue<Chore>myChores;
Chore tmp;
bool enterAnother=true;
//input loop
while(enterAnother){
char c;//checks if they want to continue
tmp.input(cin);
myChores.push(tmp);
cout<<endl<<"Want to enter another chore?(y for yes, n for no)";
cin >>c;
if(c=='y'|| c=='Y')
enterAnother=true;
else
enterAnother=false;
}
//output loop
while(!myChores.empty()){
tmp = myChores.top();
myChores.top().output(cout,tmp);
myChores.pop();
}
}
Any help would be greatly appreciated.
Related
So I've been trying to figure out the bool function for a while now.
I'm trying to make it so after a specific value is entered, the bool will become true or false.
#include <iostream>
using namespace std;
int a;
bool dead;
void Life() {
if (dead == true) {
cout << "You ded.";
}
else {
cout << "You not ded.";
}
}
int main()
{
cin >> a;
if (a == 10)
{
bool dead = true;
}
Life();
return 0;
}
This is what I currently have, however it isn't changing the value of the bool. Any ideas?
You're redeclaring the variable 'dead' in main's if block. So now you have two 'bool dead', a global one, and a local one in main's if block. The statement 'bool dead = true' sets the local one, but the function 'Life()' uses the global one. Just remove the 'bool' from the latter and you will always be using the global one.
#include <iostream>
using namespace std;
int a;
bool dead;
void Life() {
if (dead == true) {
cout << "You ded.";
}
else {
cout << "You not ded.";
}
}
int main()
{
cin >> a;
if (a == 10)
{
dead = true;
}
Life();
return 0;
}
There are two variables called dead.
- a global
- a local which lifetime is the if block
You set only the local variable inside the if block. So you do not change the global variable. Remove the type inside the if block and your program may run as you intended. And don't forget to initialize your global dead variable. Depending on the OS and state your global variable may start with a true value.
I have example code and classs:
class a{
int x;
a(){
this->x = 335; /* example number*/
}
public:
void operator=(int);
};
void a::operator=(int source){
this->x = source;
}
main(){
int i = 100;
a example_class;
example_class = i; //works fine!
i = example_class; /*this is what I want to do.*/
}
the problem with this hole thing is that I can't
make the operator= be a friend function
therefore the command: "i = example_class"
can't be done because I can't create a function in //the the int class like I normally would with my own classes.
Finally:
How can I complete the command:
"i = example_class" when the
operator= can't have more than 1
parameter?
notes:
I know the code doesn't do anything.
And is only an example. The point
Is what actually matters.
Also, I need to make it clear that I
cannot create any functions in the
Target class(in this case int). Only in
the source class(in this case a).
I also want to make clear that I know
that it's impossible to declare the
operator= as a friend function.
I know that I could just create a function
to get a reference to int x or make
int x public but I didn't want to do that
because the real code involves complex
functions for converting between types
so it's vary important to me to be able
to write: "i = example_class;".
Thanks,
Ronen.
Working example.
#include <iostream>
class a {
int x = 355;
public:
void operator=(int);
operator int();
};
void a::operator=(int source){
x = source;
}
a::operator int() {
return x;
}
int main(int, char**) {
int i = 100;
a example_class;
example_class = i;
int j = example_class;
std::cout << j << std::endl;
}
I am writing a program for insertion sort.I am creating a class to read print and sort a vector of integers.I have created a vector of class and I want to call functions read,sort and print from vector of class created.How to do that ?
Thanks,
#include <iostream>
#include <vector>
using namespace std;
class sorting
{
private:
vector<int>arr;
public:
void read();
void sortt();
void print();
};
void sorting :: read()
{
int n;
cin>>n;
for(int i=0; i<n; i++)
{
int t;
cin>>t;
arr.push_back(t);
}
}
void sorting :: sortt()
{
int j,temp;
for(unsigned int i=0; i<arr.size(); i++)
{
temp=arr[i];
j=i;
while(temp<arr[j-1] && j>0)
{
arr[j]=arr[j-1];
j=j-1;
}
arr[j]=temp;
}
}
void sorting :: print()
{
for(unsigned int k=0; k<arr.size(); k++)
{
cout<<arr[k]<<"\t";
}
cout<<endl;
arr.clear();
}
int main()
{
vector<sorting>s;
s.read(); // giving an error
s.sortt(); // giving an error
return 0;
}
It should be sorting s; and not vector<sorting>. You defined those methods read() amd sortt() defined in the class sorting.
Thanks,
I got this answer searching in google accidentally in different websites,
It is like vectors(100)
So I can call
s[i].sortt()
s[i].print()
for objects of vector
A smart pointer clears the memory if the pointer gets out of scope. I wanted to adapt this to a file descriptor, like a socket. There you need a user defined deleter, because close() is the function to free the file descriptor (fd) resources.
I found this useful page, unfortunately, most approaches did not work for me. Below is a working solution I found up to now, which is a little nasty. Because uniqu_ptr expects a pointer I created int *fd to store the fd value, therefore, I had to close(*fd) and delete fd in my custom deleter.
(1) Is there a better way?
Options A and B, which are based on the hints provided by the mentioned web page, are much nicer but causing odd compiler errors.
(2) Does anyone know how to correctly use these alternatives?
I'm using Qt Creator 3.0.1 with CONFIG += c++11 option and gcc version 4.8.2
#include "ccommhandler.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <memory>
#include <qdebug.h>
//for Option A and B
struct CloseHandleDeleter {
typedef int pointer;
void operator()(int handle) const
{
}
};
//custom deleter, working
class MyComplexDeleter
{
public:
MyComplexDeleter() {}
void operator()(int* ptr) const
{
qDebug() << "Deleting ";
close(*ptr);
delete ptr;
}
};
CCommHandler::CCommHandler()
{
//Option A doesn't work
//std::unique_ptr<int, CloseHandleDeleter> file( socket(AF_INET, SOCK_STREAM, 0) );
//Option B doesn't work
//std::unique_ptr<int, int()(int)> filePtr( socket(AF_INET, SOCK_STREAM, 0) , close);
MyComplexDeleter deleter;
int *fd = new int;
*fd = socket(AF_INET, SOCK_STREAM, 0);
std::unique_ptr<int, MyComplexDeleter> p( fd , deleter);
}
Edit:
The posted answer by Nevin is right, it solves my initial problem.
The comment of learnvst caused to rethink my problem, and I have to say I may made it much more complex than needed, because the following simple class should also solve my problem of auto-free the memory of a resource or as in my case, to close the file descriptor:
class SocketHandler
{
int _fd;
public:
SocketHandler(int FD):_fd(FD){}
~SocketHandler() { if(_fd!=-1) close(_fd); }
operator int() const { return _fd; }
};
Because fd isn't a pointer, I wouldn't try to pigeonhole it into unique_ptr. Instead, create a custom class whose interface is based on unique_ptr, as in (caution: totally untested):
class unique_fd
{
public:
constexpr unique_fd() noexcept = default;
explicit unique_fd(int fd) noexcept : fd_(fd) {}
unique_fd(unique_fd&& u) noexcept : fd_(u.fd_) { u.fd_ = -1; }
~unique_fd() { if (-1 != fd_) ::close(fd_); }
unique_fd& operator=(unique_fd&& u) noexcept { reset(u.release()); return *this; }
int get() const noexcept { return fd_; }
operator int() const noexcept { return fd_; }
int release() noexcept { int fd = fd_; fd_ = -1; return fd; }
void reset(int fd = -1) noexcept { unique_fd(fd).swap(*this); }
void swap(unique_fd& u) noexcept { std::swap(fd_, u.fd_); }
unique_fd(const unique_fd&) = delete;
unique_fd& operator=(const unique_fd&) = delete;
// in the global namespace to override ::close(int)
friend int close(unique_fd& u) noexcept { int closed = ::close(u.fd_); u.fd_ = -1; return closed; }
private:
int fd_ = -1;
};
This is my CArray.h:
struct CComplexNumber
{
int rpart, ipart;
};
class CArray
{
protected:
CComplexNumber* a;
int size;
public:
CArray* next;
void updateElement(int rp, int ip);
};
And my CArray.cpp:
#include <iostream>
using namespace std;
#include "CArray.h"
void CArray::updateElement(int rp, int ip)
{
a->rpart = rp;
a->ipart = ip;
}
And this is a line in main.cpp
CArray* first = new CArray();
CArray* cur = first;
cur->updateElement(1,2); //=> Here is the line that causes the bug
When I debugged, the cmd crassed. I have to exit and debug each line. When I reached the line above, the compiler stop and appear:
Unhandled exception at 0x00b1155b in Section03.exe: 0xC0000005:
Access violation writing location 0x00000000.
Please fix my code and explain why I can't replace the rpart with rp?