I have problem with passing a char array by value to a priority_queue. I want to pass a unique value to the constructor, but I can only assign the type in the priority queue to char*. This causes a problem since the value passed changes through each iteration in the algorithm and then all the values in the priority queue (since every element is a pointer to task). Here is a code sample:
char task[100];
char priority;
pair<int, char*> temp;
priority_queue< int, vector<pair<int, char*>>, compare> queue;
printf("Define a priority and a task to be done (exit by pressing CTRL-D):\n\n");
do {
priority=getchar();
if(isdigit(priority)){
scanf("%s", task);
temp=make_pair(atoi(&priority), task); //I want to pass by value here not by reference, is there any solution to this?
queue.push(temp);
printf("%i %s\n", temp.first, temp.second);
}
} while(priority != EOF);
Is there any way that I can assign a unique string to every element of the priority queue?
stringorvector<char>by value. The C solution:struct foo { char task[100]; };and pass that struct by value.make_pair(..., std::string(task))and change the related types accordingly.