2

I would like to know if a vector of vectors exist

for example

vector<vector<int> > edges; 

Now I would like to check if edges[i] exists. I tried

if(edges[i] == NULL) 

But it did not seem to work. How this can be resolved?

Clarification: I tried checking the size. I am just querying if its if initialized or not.

2
  • 4
    There is no pointer here, hence NULL does not make sense. No offense, but you should learn the basics of objects, references and pointers in C++. Commented Feb 27, 2016 at 16:29
  • "I would like to know if a vector of vectors exist" - vector<vector<int> > edges simply exists within its scope. Take more time to express yourself. Commented Feb 27, 2016 at 16:34

5 Answers 5

8

You probably want to check if edges[i] is empty:

if (edges[i].empty()) 
Sign up to request clarification or add additional context in comments.

3 Comments

It's better to use .empty() if that's the condition you want to test for. Although for std::vector<> it won't make a difference, for other types like std::list<> it might.
Thanks , I tried that earlier but lets assume the edge[i] does not exist and its get queried. It will break.
It's not possible for it to not exist unless you're using std::vector<> incorrectly. You have to set an explicit size for the outer vector. Referencing edges[i] for i >= edges.size() is not valid.
5

I think you might have confused checking whether an object "exists" which is not relevant here with bounds checking. If you want to check whether i is a valid index, just check if it's within the size of the vector:

if (i < edges.size()) 

Comments

1

Use vector.empty():

if (edges[i].empty()){ } 

Or use vector.size():

if (edges[i].size() == 0){ } 

Comments

1
vector<vector<int> > edges; 

Now I would like to check if edges[i] existis.

It seems that std::vector is the wrong (outer) data type for you. Perhaps you should use std::map instead:

std::map<int, std::vector<int>> edges; if (edges.find(i) == edges.end()) { // does not exist } 

Comments

1

NULL (or more correctly, since you tagged C++11, nullptr) is used for checking pointers. You've declared a vector of concrete instances, so pointers don't apply, and if the test

if (i < edges.size()) 

then the vector is initialized - that is, constructed. You can check, next, whether it is populated:

if (i < edges.size() && !edges[i].empty()) 

however, this will not distinguish between uninitialized and currently empty, consider:

std::vector<int> v; v.push_back(1); v.pop_back(); // now it's empty again bool uninitialized = v.empty(); // lies 

It is empty, but it has been initialized and used, if that is your definition of initialized.

There is, however, one trick left, but it is tending towards undefined behavior.

if (i < edges.size() && edges[i].capacity() == 0) 

This will tell you if the vector has any memory allocated. In the worst case scenario, if your vector aggressively frees, it will be equivalent to empty(). But in most standard implementations, a vector that has been populated will have non-zero capacity, whilst a vector that has not yet been used will have no capacity.

Again: it's not perfect, there are ways to tell the vector to release any capacity it has.

1 Comment

If I understand your answer correctly then the only way you get a guaranteed behavior is, if you manage the state of the vector initialization yourself in a separate boolean variable?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.