473,325 Members | 2,872 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,325 software developers and data experts.

can not dynamically add element to vector in going over the vector

Hi,
I just found out that in looping the vector, you can not push_back()
to the vector even in some "safe" cases. Here is the code snippet,

vector<pair<UINT32, UINT32> >::iterator itr;
for (itr = vec.begin(); itr< vec.end(); ++itr){

pair<UINT32, UINT32> p1, p2;
... assign value of p1 and p2 ...

if (..in some condition...){
vec.push_back(p1);
vec.push_back(p2);
vec.erase(itr);
return 0;
}
}

I think this is a safe dynamic addition to the vector as you can see
once something is added to the vector, I return from the loop. But from
the debugger I see that after the first push_back(), the iterator, itr,
changes. I just can not image why this happens.

Thanks!
Jul 22 '05 #1
4 1711
John Black wrote:
I just found out that in looping the vector, you can not push_back()
to the vector even in some "safe" cases. Here is the code snippet,

vector<pair<UINT32, UINT32> >::iterator itr;
for (itr = vec.begin(); itr< vec.end(); ++itr){

pair<UINT32, UINT32> p1, p2;
... assign value of p1 and p2 ...

if (..in some condition...){
vec.push_back(p1);
vec.push_back(p2);
vec.erase(itr);
Here 'itr' is invalid, most likely.
return 0;
}
}

I think this is a safe dynamic addition to the vector as you can see
once something is added to the vector, I return from the loop. But from
the debugger I see that after the first push_back(), the iterator, itr,
changes. I just can not image why this happens.


Because it most likely cannot be kept the same.

Victor
Jul 22 '05 #2

"John Black" <bl***@eed.com> wrote in message
news:40***************@eed.com...
Hi,
I just found out that in looping the vector, you can not push_back()
to the vector even in some "safe" cases. Here is the code snippet,

vector<pair<UINT32, UINT32> >::iterator itr;
for (itr = vec.begin(); itr< vec.end(); ++itr){

pair<UINT32, UINT32> p1, p2;
... assign value of p1 and p2 ...

if (..in some condition...){
vec.push_back(p1);
vec.push_back(p2);
vec.erase(itr);
return 0;
}
}

I think this is a safe dynamic addition to the vector as you can see
once something is added to the vector, I return from the loop. But from
the debugger I see that after the first push_back(), the iterator, itr,
changes. I just can not image why this happens.

Thanks!


There's a simple solution, just use integers instead of iterators.

john
Jul 22 '05 #3


John,

When you push_back an item in the vector, the iterators potentially
will be invalid, think of it, the vector might need to reallocate the
internal storage for the elements, so any pointers/iterators now are
invalid.
Perhaps if you "reserve" up front you might not see the invalidation of
the iterators, but its not something I'd do in production code.

dave
"John Black" <bl***@eed.com> wrote in message
news:40***************@eed.com...
Hi,
I just found out that in looping the vector, you can not push_back()
to the vector even in some "safe" cases. Here is the code snippet,

vector<pair<UINT32, UINT32> >::iterator itr;
for (itr = vec.begin(); itr< vec.end(); ++itr){

pair<UINT32, UINT32> p1, p2;
... assign value of p1 and p2 ...

if (..in some condition...){
vec.push_back(p1);
vec.push_back(p2);
vec.erase(itr);
return 0;
}
}

I think this is a safe dynamic addition to the vector as you can see
once something is added to the vector, I return from the loop. But from
the debugger I see that after the first push_back(), the iterator, itr,
changes. I just can not image why this happens.

Thanks!

Jul 22 '05 #4

"John Black" <bl***@eed.com> wrote in message
news:40***************@eed.com...
Hi,
I just found out that in looping the vector, you can not push_back()
to the vector even in some "safe" cases. Here is the code snippet,
Then they are not safe cases.

vector<pair<UINT32, UINT32> >::iterator itr;
for (itr = vec.begin(); itr< vec.end(); ++itr){
'itr < vec.end()' is not portable. Use 'itr != vec.end()' Iterators are not
pointers.
pair<UINT32, UINT32> p1, p2;
... assign value of p1 and p2 ...

if (..in some condition...){
Reverse the order and do the erase first.
vec.erase(itr);
You should be ok, as long as you do in fact exit the loop here. Otherwise
you'd 'itr = vec.erase(itr);' and remove the ++itr from the above for
statement and do the increment explicitly in an else clause.
vec.push_back(p1);
vec.push_back(p2);
return 0;
}
}

I think this is a safe dynamic addition to the vector as you can see
once something is added to the vector, I return from the loop. But from
the debugger I see that after the first push_back(), the iterator, itr,
changes. I just can not image why this happens.


A much better approach is:

typedef vector<pair<UINT32, UINT32> tVec;

tVec::iterator lItr = std::find_if( vec.begin(), vec.end(), condition_fnc );

if( lItr != vec.end() )
{
// some calcs

vec.erase( lItr );

vec.push_back( ... );
}

Jeff F
Jul 22 '05 #5

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

Similar topics

6
by: Jeff Williams | last post by:
Ok, everyone loves to talk about dynamic arrays and ptr's etc, they provide endless conversation :) so here goes: Will this leak memory (my intuition says yes): void foo(vector<int*>& vec) {...
7
by: linq936 | last post by:
Hi, The following is a psudo code to describe my question: vector<int> ints; vector<int>::iterator itr; while (itr != ints.end()){ int j = some_function(*i); if (j>0){ ints.push_back(j); }
7
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
4
by: Bin Chen | last post by:
Hi, I don't know what's wrong with my program, it causes SIGSEGV: #include <vector> using namespace std; int main(void) {
10
by: The Cool Giraffe | last post by:
I got a hint recently (guess where, hehe) and was directly pointed to the vector class. Now, i have no issues using that way but i'm concerned about the performance issue. Which is fastest...
10
by: Jess | last post by:
Hello, I have a program that stores dynamically created objects into a vector. #include<iostream> #include<vector> using namespace std;
8
by: Ray D. | last post by:
I'm trying to write a C function to compute the compressed row storage (CRS) vectors of a 2-d matrix. This is used primarily for increasing computing efficiency of matrices whose main elements are...
3
by: Samant.Trupti | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++) //print each...
3
by: sherman | last post by:
string ***dat_array; int t; dat_array = new string **; for (i=0; i<t; i++) { dat_array = new string *; for (j=0; j<5; j++) dat_array = new string ; } // dat_array = new string **;
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
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...
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: 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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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.