473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Is this a valid used of STL vector::erase()

Hi all,

I recently came across the following code:

// some data
struct Data { // ... some data };

// a list to hold the data
vector<Data> dataList;

// a function to erase an element inside the datalist
void destroyDataAtIndex(int index)
{
dataList.erase( &dataList[index] ); //(a)
}

Is line (a) valid? If so, how does that work since the function
declaration of erase is erase(iterator iter)? Does it automatically
converts the address of the element to an iterator? or it makes the
assumption that an iterator is a typedef of pointer?

Also, I created a toy program with the above code snipplet, from the
test runs, some times the program will crashed. Is it because of the
way we used erase in (a)?

Thanks,
Kaede
Jul 22 '05 #1
1 4074
On 22 Nov 2003 21:16:45 -0800, ka************@hotmail.com (kaede) wrote:
Hi all,

I recently came across the following code:

// some data
struct Data { // ... some data };

// a list to hold the data
vector<Data> dataList;

// a function to erase an element inside the datalist
void destroyDataAtIndex(int index)
{
dataList.erase( &dataList[index] ); //(a)
}

Is line (a) valid? If so, how does that work since the function
declaration of erase is erase(iterator iter)? Does it automatically
converts the address of the element to an iterator? or it makes the
assumption that an iterator is a typedef of pointer?


No, it's not valid. However, it might work on some compilers that use
pointers as iterators for vectors. It will fail on others.

--
Be seeing you.
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: BCC | last post by:
I have the following code, where good_list is a vector of CUnits: int high_cutoff = 10; vector<CUnit>::iterator it; for (it = good_list.end(); it != good_list.begin(); --it) { CUnit* ccu = it;...
10
by: Alex Vinokur | last post by:
vector<int> v; erase() requires (as input parameter) and returns vector<int>::iterator, rbegin() returns vector<int>::reverse_iterator. So, a compiler doesn't accept v.erase(v.rbegin()). Do...
9
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
3
by: ma740988 | last post by:
For discussion purposes, assume the vector ivec contains 67108864 (67 million elements) elements. Lets futher assume nstart and end equal 1008000 and 11088000 respectively. The question. ...
5
by: Anil | last post by:
I am facing problem while erasing an elemet from stl vector when its size is 2. It works fine when SIZE 2. Can anybody help me in this?? Following is the sample code which i tried. #include...
13
by: thomas | last post by:
suppose I will delete an element pointed to by "iter". like this: vector<ints; for(vector<int>::iterator iter=s.begin(); iter!=s.end(); iter++){ if(*iter==3) s.erase(iter); ...
3
by: subramanian100in | last post by:
Consider vector<stringv; If we call, v.erase(v.end()) this invokes undefined behaviour. But, if we call
1
mickey0
by: mickey0 | last post by:
Hello I have a vector and an index 'i'; I need something like: for (int i = vec.size() - 1 ;i >= 0; i--) { if (something(i) ) vec.removeAt(i); } using c++ standard library; is it possibile...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.