Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a NULL equivalent for pairs in C++?

Tags:

c++

std-pair

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;
}
like image 986
user1634426 Avatar asked Sep 24 '15 10:09

user1634426


People also ask

Is there a null value in C?

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.

How do I return an empty pair?

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.

Are there pairs in C?

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; };

How do you check if a pair is empty?

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.


3 Answers

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)

like image 185
utnapistim Avatar answered Oct 14 '22 06:10

utnapistim


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?

like image 38
Lightness Races in Orbit Avatar answered Oct 14 '22 04:10

Lightness Races in Orbit


No, that is not possible. You could use an additional variable to indicate the validity of the pair (that you have a pair).

like image 32
Marius Bancila Avatar answered Oct 14 '22 04:10

Marius Bancila