checking index vector if = to null

Dec 2, 2014 at 6:40pm
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 <int> > 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;
    }
Last edited on Dec 2, 2014 at 6:52pm
Dec 2, 2014 at 6:48pm
How can a double be NULL?
Dec 2, 2014 at 6:54pm
I change it in to an int ,but also even if it int ,it have the same problem?
Dec 2, 2014 at 7:37pm
any solution please ??
Dec 2, 2014 at 9:33pm
Well, how can an int be NULL? Do you know what NULL is?
Dec 2, 2014 at 10:16pm
> Well, how can an int be NULL?

NULL can be an int. In C++98, NULL was a literal zero

All that the IS requires is that NULL be "an implementation-defined C ++ null pointer constant".
Either std::nullptr_t or a literal zero.
http://coliru.stacked-crooked.com/a/ee86c579a5205524
http://rextester.com/VOSB56107

To check if the vector contains a value at a particular position v[i][j], do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>

int main()
{
    std::vector< std::vector<int> > v ; // this is an empty vector
    v.resize(2) ; // now it contains two elements; v[0] and v[1]

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

    std::size_t i = 0, j = 4 ;

    // is v[i][j] present?
    if( i < v.size() && j < v[i].size() ) // i, j are valid
    {
        std::cout << "v[" << i << "][" << j << "] == " << v[i][j] << '\n' ;
    }
    else
    {
        std::cout << "there is no v[" << i << "][" << j << "]\n" ;
    }
}

http://coliru.stacked-crooked.com/a/bb2a3205738e33c5
Last edited on Dec 2, 2014 at 10:19pm
Topic archived. No new replies allowed.