i'm coding a trie structure in C++ with a template snippet:
pair<char,T>
I have a method void empty() in which I want to check, whether the root's second value is not set (value is equal to default constructor value or in detail: values would store other pair types.). I know that the default constructor would apply 0 for the data type int, but how can i check this inside the template ?
Another post mentioned this: return root == new Trie<char,T>() (failed duo to unknown == operator)
Thanks in advance
A std::pair
s member cannot be "not set".
You can use std::optional
to model an optional value.
"...value is equal to default constructor..."
Of course you can check if the current value equals the initial one:
std::pair<int,int> x;
if (x.first == 0) {
std::cout << "value of x.first is that of a default constructed std::pair<int,int>";
}
Or for the second member of a std::pair<int,T>
:
if (x.second == T{} ) {
std::cout << "second has same value as a default constructed T";
}
Complete example:
#include <iostream>
#include <utility>
template <typename T>
bool check_if_default_second(const std::pair<int,T>& p) {
return p.second == T{};
}
int main() {
std::pair<int,int> p;
std::cout << check_if_default_second(p);
}
I changed the initial question. I can't change the code. Basically the pair.second holds other pairs , which creates a tree. So i want to check if the second value has no pair (or other value) besides the created one from the constructor. In other words. I want to check if the second.vales have ever been touched by code.
and i have to check the second value, not the first, which is T.
@SebyKiunke just added a part that specifically adresses your last edit. Wether it is
first
orsecond
doesn't change much@SebyKiunke fwiw, I know that this does not adress the question you actually want to ask. Thats to some large extend because the question is not quite clear. Sounds like a XY problem. Perhaps open another question where you ask about your actual problem. What makes you think that you need to check if a pairs member has been set? You mention something here, but it is unclear
@SebyKiunke what dont you understand? It compares the second member of the pair to a default constructed
T
. Thats what you want if I understood the question correctly