What would I want to use instead of NULL if I have an unassigned pair in C++?
As an example, suppose I have (pseudo)code like the following:
pair<int,int> bestPair; //Global variable
updateBestPair(vector<int> a, vector<int> b) {
bestPair = NULL;
for (/* loop through a and b */) {
if (/* pair(a,b) is better than bestPair and better than some baseline */)
bestPair = make_pair(a,b);
}
if (bestPair != NULL) //Found an acceptable best pair
function(bestPair);
else
cout<<"No acceptable pairs found"<<endl;
}
The C and C++ languages have a null character (NUL), a null pointer (NULL), and a null statement (just a semicolon (;)). The C NUL is a single character that compares equal to 0. The C NULL is a special reserved pointer value that does not point to any valid data object.
You could however make something like an empty pair using Boost. Optional. Then you would either use a boost::optional<std::pair<...>> giving you the option of returning either a pair or an empty state or use std::pair<boost::optional<...>, boost::optional<...>> for a pair where either object could be empty.
There isn't one. std::pair is a template, and C doesn't have anything similar to templates. struct pair_int_double { int first; double second; };
The default constructor of std::pair would value-initialize both elements of the pair, that means for pair<int, int> res; , its first and second would be initialized to 0 . That's the only way you can check for a default-constructed std::pair , if they're guaranteed to be non-zero after the assignment.
Is there a NULL equivalent for pairs in C++?
No.
What would I want to use instead of NULL if I have an unassigned pair in C++?
Here are a few options:
you can use a pointer to a pair, which can be set to NULL; This is probably not the best solution (since you are clearly not requiring a pointer)
you can use a boost::optional<std::pair<int,int>>
;
you can (and probably should) rewrite your code not to use a global variable.
you can restructure your control flow to avoid checking for a valid pair as a separate step:
pair<int,int> bestPair; //Global variable
updateBestPair(vector<int> a, vector<int> b) {
// not needed
// bestPair = NULL;
//loop through a and b
if (/* pair(a,b) is better than bestPair and ... */)
{
bestPair = make_pair(a,b);
function(bestPair);
}
else
cout<<"No acceptable pairs found"<<endl;
}
you can choose an artificial value to represent "invalid pair value":
// use as constant, wherever you used NULL before
const auto invalid_pair = std::make_pair(
std::numeric_limits<int>::max(),
std::numeric_limits<int>::max());
you can use a boolean flag:
pair<int,int> bestPair; //Global variable
updateBestPair(vector<int> a, vector<int> b) {
bool initialized = false;
//loop through a and b
if (/* pair(a,b) is better than bestPair and ... */)
{
bestPair = make_pair(a,b);
initialized = true;
}
if(initialized)
function(bestPair);
else
cout<<"No acceptable pairs found"<<endl;
}
you can use a custom solution (similar to boost::optional wrapper or not)
No. C++ objects cannot be "NULLed".
(Even pointers, which are objects, cannot be "NULLed"! This is confusing because their value may be set to a null pointer value, which we sometimes in the past obtained with a macro named NULL
; however, this has never been the same as "NULLing" the pointer itself. Er, anyway…)
I recommend either boost::optional
, or rethink the idea of having a global variable that can be "has a useful value" or "does not have a useful value". What's the point in it existing if it has no useful value?
No, that is not possible. You could use an additional variable to indicate the validity of the pair (that you have a pair).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With