2d vector check if an index equal to null problem

how to check if a specific index equal to null
when i try to implement it, it gives an error
for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vector < vector <double> > v;
    v[0].push_back(0);
    v[0].push_back(1);
    v[0].push_back(2);
    v[0].push_back(3);

    v[1].push_back(10);
    v[1].push_back(11);
    v[1].push_back(12);
    v[1].push_back(13);

    if(v[0][4]==NULL)
    {
        cout<<"empty"<< endl;
    }
A double can't be null. Only pointers can.

v[0][4] is out of bounds so it invokes undefined behaviour which mean there are no guarantees what will happen.

If you want to check if the vector has a fifth element check the size of the vector instead.

1
2
3
4
if(v[0].size() >= 5)
{
	cout<<"v[0][4] exists!"<< endl;
}
Last edited on
Topic archived. No new replies allowed.