Solved

# if vector is empty

Posted on 2003-02-23
Medium Priority
318 Views
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
Question by:kencj
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 3
• 2

Accepted Solution

colmstea earned 80 total points
ID: 8005973
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]);
}

cliff
0

Author Comment

ID: 8025629
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

Expert Comment

ID: 8028879
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

Expert Comment

ID: 8030646
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

Author Comment

ID: 8031815
great help!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
###### Suggested Courses
Course of the Month9 days, 20 hours left to enroll

#### 762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.