• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

if vector is empty

i'm using the std vector.h
i want to know how to prove if vector is empty.
because at certain stage i erase elements. i want to exit if the vector is emtpy.
sort of like if (vector == NULL)
that doesn't work.
thanks
also why can't i do this? and what's the alternative
for( i=0; ........)
if(points0[i] == points1[j])
{
points0.erase(i);
}
0
kencj
Asked:
kencj
  • 3
  • 2
1 Solution
 
colmsteaCommented:
There is a function called empty() that returns a boolean indicating whether the vector is empty..
ie. if (vector.empty()) return -1;

You cant call vector.erase(x) on the index because vectors store their values as pointers.. You need to pass the actual pointer value of the stored element to the erase function.
ie:
for (i=0; ....)
if (points0[i]==points1[j])
{
   points0.erase(&points0[i]);
}

hope that answers your question!

cliff
0
 
kencjAuthor Commented:
that helped. but i have this access violation. something to do with that.
template<class _II, class _OI> inline
     _OI copy(_II _F, _II _L, _OI _X)
     {for (; _F != _L; ++_X, ++_F)
          *_X = *_F;
     return (_X); }
          // TEMPLATE FUNCTION copy_backward

and also that empty function,
     if (points0.empty())
     {
          fprintf(p_points, "%3c\n", "end");
     }
this should work right? thanks. still learning so abit slow
0
 
colmsteaCommented:
That copy function assumes that the output vector already has enough elements pushed onto it to hold the elements being copied...  
So you should either have a FOR loop that pushes enough elements on right before calling the function OR rewrite the copy function to use vectors instead of iterators and just push_back each element onto vector X from vector _F.

Let me know if you need me to elaborate or provide code.

Also:
That last part with the empty() will not crash assuming p_points is an open file.  It will not write "end" to the file either though! (if thats your intention, you need to use fprintf(p_points, "%s\n", "end")  ) . let me know

cliff

0
 
colmsteaCommented:
That copy function assumes that the output vector already has enough elements pushed onto it to hold the elements being copied...  
So you should either have a FOR loop that pushes enough elements on right before calling the function OR rewrite the copy function to use vectors instead of iterators and just push_back each element onto vector X from vector _F.

Let me know if you need me to elaborate or provide code.

Also:
That last part with the empty() will not crash assuming p_points is an open file.  It will not write "end" to the file either though! (if thats your intention, you need to use fprintf(p_points, "%s\n", "end")  ) . let me know

cliff

0
 
kencjAuthor Commented:
great help!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now