I'm currently programming my own PriorityQueue data structure in C++ which I have made into a template class with typename T.
The toString() member function of my class is defined as:
/**
* @brief Gives a std::string representation of this queue structure
*
* The priority queue is returned as a string in the following format:
*
* \code{.cpp}
* Data Item Priority
* [Item] [P1]
* [Item] [P2]
* [Item] [P3]
* \endcode
*
* where P1 < P2 < P3.
*
* @return String representation of this priority queue
*/
std::string toString() const {
std::string tempString = "";
// initialise temporary node to front of priority queue
PQNode<T>* tempNode = front;
// append string with headers
tempString += "Data Item \t\t Priority\n";
// while tempNode is not null, continue appending queue items to string
while (tempNode != nullptr) {
tempString += std::to_string(tempNode->head) + "\t\t\t " + std::to_string(tempNode->priority) + "\n";
// shift tempNode to tail of current tempNode (moving along queue)
tempNode = tempNode->tail;
}
return tempString;
}
How could I code this such that std::to_string() is used within this method if the type of the template is a primitive such as an int or double and .toString() (calling the toString method of the passed class) is called if the type of the template is a class with its own toString member function?
I presume there is a way to do it with #ifndef clauses however I don't have much experience using these.
Aucun commentaire:
Enregistrer un commentaire