473,796 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 destroyDataAtIn dex(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 4098
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 destroyDataAtIn dex(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
3377
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; if (ccu->GetCutoff() >= high_cutoff) { good_list.erase(it); } }
10
4588
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 we have to write v.erase(v.end() - 1)? Or there exists something else? -- Alex Vinokur
9
5215
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); vector<double>::iterator i=x.begin()+2, j=x.begin()+6; x.erase(i,j); // i<j, ok, erases 4 elements. x.erase(j,i); // j>i, no error, just inserts 4 elements.
3
2367
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. What's the _fastest_ way to erase element numbers less than 1008000 and element numbers greater than 11088000. Current approach. typedef std::vector < int INT_VEC ;
5
2055
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 <iostream> #include <vector> using namespace std; #define SIZE 2
13
7494
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); //A } in line A, if element by "iter" is erased, will "iter" point to the
3
1883
by: subramanian100in | last post by:
Consider vector<stringv; If we call, v.erase(v.end()) this invokes undefined behaviour. But, if we call
1
1833
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 from an int gets the iterator?
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10453
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.