Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I return NULL for vector<double> function? [duplicate]

Tags:

c++

null

vector

I have the following function:

/* Calculate if there is an intersection with given intial position and 
  direction */
vector<double> intersection(vector<double> startPos, vector<double> direction)
{
   if(there is intersection)
      return (intersection coordinates);
   else {
      return NULL;
   }
} 

Could I do this and check against NULL if an intersection exists:

vector<double> v = intersection(pos, dir);
if(v == NULL)
   /* Do something */
else
   /* Do something else */

If this is not allowed/bad coding practice, what is another way I may go about this?

like image 507
David Avatar asked Jun 14 '17 18:06

David


People also ask

How do you return a null vector in C++?

But practically there is no such term as a null vector but we can return a null vector in C++. Include necessary header files. Declare using std::cout and std::vector. Declare function with vector type return type which accepts vector as a variable. Then return null vector inside function body.

How to set a Vector3 value type to null?

You cannot set a value type to null (which includes data types like String, int, float, char, and also includes structs). But that will require casting into a normal Vector3 for use. Setting it to zero is not the same as setting it to null. As null it doesn't exist, as zero it's at a specific value. McGravity, EDartBlade and pgomes like this.

Can a function return NULL if all arguments are null?

In most cases, a function will return NULL if any of its arguments are NULL (but there are exceptions). Many built-in functions can return NULL even if none of the arguments are NULL. In fact, that's the whole point of the NULLIF function.

Why can't I return the remainder of a vector?

A std::vector<int> is always a vector, it is never NULL (or equal to nullptr ). The reason you can't return remainderResult.push_back (numbers [i]); is because the result of push_back is nothing, not the vector. You will need to push_back then return.


1 Answers

NULL really is only a concept of pointers. Since we have a container we can check something else, namely, whether or not the container is empty. If it is then we know we have no elements and if it is not then we know there is stuff to process. That lets you write code like

vector<double> intersection(vector<double> startPos, vector<double> direction)
{
    if(there is intersection)
        return (intersection coordinates);
    else {
        return {}; // this means return a default constructed instance
    }
} 

and then you can use it like

vector<double> v = intersection(pos, dir);
if(v.empty())
    /* Do something */
else
    /* Do something else */

Also note that if you want to get a set intersection you can use std::set_intersection and use it like

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
    std::vector<int> v1{1,2,3,4,5,6,7,8};
    std::vector<int> v2{        5,  7,  9,10};
    std::sort(v1.begin(), v1.end());
    std::sort(v2.begin(), v2.end());     
    std::vector<int> v_intersection;     
    std::set_intersection(v1.begin(), v1.end(),
                          v2.begin(), v2.end(),
                          std::back_inserter(v_intersection));
    for(int n : v_intersection)
        std::cout << n << ' ';
}

Output:

5 7
like image 93
NathanOliver Avatar answered Oct 15 '22 13:10

NathanOliver