473,322 Members | 1,188 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,322 software developers and data experts.

STL vectors and delete

Hey everybody. I understand that erasing elements in the vector invalidates the iterators. If you consider the following code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.   vector<int> vec, vec2;
  8.  
  9.   vec.push_back(1);
  10.   vec.push_back(2);
  11.   vec.push_back(3);
  12.   vec.push_back(4);
  13.   vector<int>::iterator it;
  14.   vec2 = vec;
  15.   it = vec.begin();
  16.   for(;;)
  17.   {
  18.  
  19.     vec.erase(vec.begin());
  20.     vec = vec2;
  21.     cout << *it << endl;
  22.   }
  23.  
  24.   return 0;
  25. }
  26.  
It works for me if I run it, but theoretically speaking, would it be possible for the iterator it to ever become invalidated, and cause an error such as the segmentation fault? The reason I ask is because I believe I have this problem in one of my serious programs. Ideas would be cool.
Jul 19 '07 #1
2 1170
ravenspoint
111 100+
It looks like this will never stop, since you are not incrementing the iterator,
Jul 19 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
but theoretically speaking, would it be possible for the iterator it to ever become invalidated, and cause an error such as the segmentation fault?
You said it yourself.

Use an invalid iterator and all bets are off. Maybe it works. Maybe it doesn't.
Jul 19 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
12
by: Dave Theese | last post by:
Hello all, I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old...
7
by: joevandyk | last post by:
Below, I have a class Container that contains a vector. The vector contains pointers to a Base class. When the Container destructor is called, I would like to delete all the objects that the...
2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
7
by: enki | last post by:
What I want to do is create sprites and put them in a vector. I know that this specific example dosn't realy fit in this group but my sprite are ponters. vector<*sprite>spvec; sprite *...
4
by: mp | last post by:
I am doing pairwise comparisons between 2 vectors of chars and permuting one vector and storing the resulting calculations in a vector<float> then I find a p-value among other stats. I have to do...
2
by: rainjack | last post by:
hi, This function is used to store some data into the stl vector, after store it using push_back, and i will delete the allocated memory using delete , but did it also delete the data i have...
5
by: Jim | last post by:
Hi, Just wondering which is better vector<record *r; r.push_back(new record(x,y)); or vector<recordr; r.push_back(record(x,y));
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.