473,756 Members | 4,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector iterators

Hi, i have the following code which updates the vector of strings(
p_vector ) each time it goes into the loop

for the first iteration we have only one string in the vector. But in
the while loop we are updating the vector and adding few more strings
at the end.

I'm expecting that In the second iteration the "it" should point to the
updated vector, but it is pointing to the "NULL".
for(vector<stri ng>::iterator it = p_vector.begin( ); it !=
p_vector.end(); it++)
{
LogInfo(("\nNex t directory %s\n",it->c_str()));
pDirStream = opendir(it->c_str());
LogInfo(("\n%s Directory
opened\n",it->c_str()));
while(pDirEntry = readdir(pDirStr eam) )
{
if( pDirEntry->d_type == DT_DIR )
{
count++;
if(count>2)
{
l_Name.append("/");

l_Name.append(s tring(pDirEntry->d_name));
LogInfo(("\n\nT he
subpath is: %s\n",l_Name.c_ str()));

p_vector.push_b ack(l_Name);
l_Name.assign(p _Name);
}
}
}
closedir(pDirSt ream);
count = 0;
}
Please explain why the "it" pointing to NULL and how can i point to the
updated vector in the second and subsequent iterations of the for loop.

Thanks,

Oct 24 '06 #1
3 1830
edu.mvk wrote:
Hi, i have the following code which updates the vector of strings(
p_vector ) each time it goes into the loop

for the first iteration we have only one string in the vector. But in
the while loop we are updating the vector and adding few more strings
at the end.

I'm expecting that In the second iteration the "it" should point to the
updated vector, but it is pointing to the "NULL".
for(vector<stri ng>::iterator it = p_vector.begin( ); it !=
p_vector.end(); it++)
{
LogInfo(("\nNex t directory %s\n",it->c_str()));
pDirStream = opendir(it->c_str());
LogInfo(("\n%s Directory
opened\n",it->c_str()));
while(pDirEntry = readdir(pDirStr eam) )
{
if( pDirEntry->d_type == DT_DIR )
{
count++;
if(count>2)
{
l_Name.append("/");

l_Name.append(s tring(pDirEntry->d_name));
LogInfo(("\n\nT he
subpath is: %s\n",l_Name.c_ str()));

p_vector.push_b ack(l_Name);
l_Name.assign(p _Name);
}
}
}
closedir(pDirSt ream);
count = 0;
}
Above code is:
a) badly formatted, and
b) non-compilable.
Please try to avoid these issues in future.
Please explain why the "it" pointing to NULL and how can i point to the
updated vector in the second and subsequent iterations of the for loop.
After inserting into a vector, your iterators may become invalid
(especially if the vector needs to re-allocate). You should rethink your
algorithm. See the following example:

#include <vector>
#include <string>

using namespace std;

int main ()
{
vector<stringp_ vector;
p_vector.push_b ack ("Test");
for(vector<stri ng>::iterator it = p_vector.begin( );
it != p_vector.end(); it++)
{
if (p_vector.size () < 10)
{
// Determine the position of the iterator inside
// the vector, so that we can set the iterator
// to the same position after inserting.
int Offset = it - p_vector.begin ();
p_vector.push_b ack("Test2");
it = p_vector.begin () + Offset;
}
}
return 0;
}

It may be more suitable to leave out iterators altogether and use an
index variable for direct access.

Regards,
Stuart

Oct 24 '06 #2
edu.mvk wrote:
Hi, i have the following code which updates the vector of strings(
p_vector ) each time it goes into the loop

for the first iteration we have only one string in the vector. But in
the while loop we are updating the vector and adding few more strings
at the end.

I'm expecting that In the second iteration the "it" should point to the
updated vector, but it is pointing to the "NULL".
for(vector<stri ng>::iterator it = p_vector.begin( ); it !=
p_vector.end(); it++)
{
LogInfo(("\nNex t directory %s\n",it->c_str()));
pDirStream = opendir(it->c_str());
LogInfo(("\n%s Directory
opened\n",it->c_str()));
while(pDirEntry = readdir(pDirStr eam) )
{
if( pDirEntry->d_type == DT_DIR )
{
count++;
if(count>2)
{
l_Name.append("/");

l_Name.append(s tring(pDirEntry->d_name));
LogInfo(("\n\nT he
subpath is: %s\n",l_Name.c_ str()));

p_vector.push_b ack(l_Name);
This push_back() operation invalidates all iterators to the vector
p_vector() whenever p_vector needs to reallocate its data to accommodate
for the increased number of elements.
l_Name.assign(p _Name);
}
}
}
closedir(pDirSt ream);
count = 0;
}
Please explain why the "it" pointing to NULL and how can i point to the
updated vector in the second and subsequent iterations of the for loop.
The most easy way to cope with your problem might be to rewrite the loop
using an index instead of an iterator:

for(vector<stri ng>::size_type i = 0; i < p_vector.size() ; ++i ) {
LogInfo(("\nNex t directory %s\n", p_vector[i].c_str()));
pDirStream = opendir(p_vecto r[i].c_str());
LogInfo(("\n%s Directory opened\n",p_vec tor[i].c_str()));
while(pDirEntry = readdir(pDirStr eam) ) {
if( pDirEntry->d_type == DT_DIR ) {
count++;
if(count>2) {
l_Name.append("/");

l_Name.append(s tring(pDirEntry->d_name));
LogInfo(("\n\nT he subpath is: %s\n",l_Name.c_ str()));

p_vector.push_b ack(l_Name);
l_Name.assign(p _Name);
}
}
}
closedir(pDirSt ream);
count = 0;
}
Best

Kai-Uwe Bux

Oct 24 '06 #3

edu.mvk wrote:
Hi, i have the following code which updates the vector of strings(
p_vector ) each time it goes into the loop

for the first iteration we have only one string in the vector. But in
the while loop we are updating the vector and adding few more strings
at the end.

I'm expecting that In the second iteration the "it" should point to the
updated vector, but it is pointing to the "NULL".
for(vector<stri ng>::iterator it = p_vector.begin( ); it !=
p_vector.end(); it++)
{
LogInfo(("\nNex t directory %s\n",it->c_str()));
pDirStream = opendir(it->c_str());
LogInfo(("\n%s Directory
opened\n",it->c_str()));
while(pDirEntry = readdir(pDirStr eam) )
{
if( pDirEntry->d_type == DT_DIR )
{
count++;
if(count>2)
{
l_Name.append("/");

l_Name.append(s tring(pDirEntry->d_name));
LogInfo(("\n\nT he
subpath is: %s\n",l_Name.c_ str()));

p_vector.push_b ack(l_Name);
l_Name.assign(p _Name);
}
}
}
closedir(pDirSt ream);
count = 0;
}
Please explain why the "it" pointing to NULL and how can i point to the
updated vector in the second and subsequent iterations of the for loop.
It doesn't matter, you are invoking undefined behaviour. One does not
modify a container while you are iterating through it - the iterators
become invalid (which is a good thing - what if vector resized itself
to somewhere else in memory?).
There is a simple solution, however. make a new vector before the loop
and load that while you iterate through the loop, then push_back the
new data once you have left the loop.

Oct 24 '06 #4

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

Similar topics

11
2904
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any circumstances in which the STL will move a vector, or invalidate iterators to elements in the vector, if you don't insert or remove elements? My actual problem seems to be as follows: I have class X, which contains an STL vector. The constructor...
3
2924
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5 kinds of iterators on say a vector. OR is it that any iterator declared on Vector is Random Iterator which has the functionality of all the others.
13
20148
by: zaineb | last post by:
Hi, This is a follow-up of sort of this thread: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/de12af6539e0d645/662cab752779f1fa?lnk=st&q=c%2B%2B+vector+vs+map&rnum=1&hl=en#662cab752779f1fa I've been chewing on this problem for a while. When I came across the above thread, I figured that maybe other members have a better idea on how to approach this. I'm currently trying to decide between which one of maps or...
23
6542
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: vector<string> tokenize(string s){
2
2352
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
9
4620
by: Christian Chrismann | last post by:
Hi, I've a runtime problem with STL vectors. Here is the simplified version of the code: template <class Tclass A { ... private: vector<T*myvector; typename vector<T*>::itarator mIt;
2
3298
by: ernesto | last post by:
Hi: I want to create my own vector class; I want to provide methods like: class Vector { public: void add(const Object* aVal); void remove(const Object* aVal); };
12
4913
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back instead I don't need to supply the iterator. But what
16
5823
by: xyz | last post by:
I have to run the simulation of a trace file around (7gb contains 116million entries)... presently i am using vector iterators to check the conditions in my program..... it is taking 2 days to finish whole simulation..... my question are the map iterators are faster than vector iterators..... does it improve the performance.... thanks to all
0
1413
by: subramanian100in | last post by:
Suppose I have vector<intc; for (int i = 0; i < 10; ++i) c.push_back(i); vector<int>::iterator it = find(c.begin(), c.end(), 5); If I do, c.insert(c.begin(), 10);
0
9455
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
9271
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
10031
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
9869
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7242
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
5140
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
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3354
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.