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

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<string>::iterator it = p_vector.begin(); it !=
p_vector.end(); it++)
{
LogInfo(("\nNext directory %s\n",it->c_str()));
pDirStream = opendir(it->c_str());
LogInfo(("\n%s Directory
opened\n",it->c_str()));
while(pDirEntry = readdir(pDirStream) )
{
if( pDirEntry->d_type == DT_DIR )
{
count++;
if(count>2)
{
l_Name.append("/");

l_Name.append(string(pDirEntry->d_name));
LogInfo(("\n\nThe
subpath is: %s\n",l_Name.c_str()));

p_vector.push_back(l_Name);
l_Name.assign(p_Name);
}
}
}
closedir(pDirStream);
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 1805
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<string>::iterator it = p_vector.begin(); it !=
p_vector.end(); it++)
{
LogInfo(("\nNext directory %s\n",it->c_str()));
pDirStream = opendir(it->c_str());
LogInfo(("\n%s Directory
opened\n",it->c_str()));
while(pDirEntry = readdir(pDirStream) )
{
if( pDirEntry->d_type == DT_DIR )
{
count++;
if(count>2)
{
l_Name.append("/");

l_Name.append(string(pDirEntry->d_name));
LogInfo(("\n\nThe
subpath is: %s\n",l_Name.c_str()));

p_vector.push_back(l_Name);
l_Name.assign(p_Name);
}
}
}
closedir(pDirStream);
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_back ("Test");
for(vector<string>::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_back("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<string>::iterator it = p_vector.begin(); it !=
p_vector.end(); it++)
{
LogInfo(("\nNext directory %s\n",it->c_str()));
pDirStream = opendir(it->c_str());
LogInfo(("\n%s Directory
opened\n",it->c_str()));
while(pDirEntry = readdir(pDirStream) )
{
if( pDirEntry->d_type == DT_DIR )
{
count++;
if(count>2)
{
l_Name.append("/");

l_Name.append(string(pDirEntry->d_name));
LogInfo(("\n\nThe
subpath is: %s\n",l_Name.c_str()));

p_vector.push_back(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(pDirStream);
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<string>::size_type i = 0; i < p_vector.size(); ++i ) {
LogInfo(("\nNext directory %s\n", p_vector[i].c_str()));
pDirStream = opendir(p_vector[i].c_str());
LogInfo(("\n%s Directory opened\n",p_vector[i].c_str()));
while(pDirEntry = readdir(pDirStream) ) {
if( pDirEntry->d_type == DT_DIR ) {
count++;
if(count>2) {
l_Name.append("/");

l_Name.append(string(pDirEntry->d_name));
LogInfo(("\n\nThe subpath is: %s\n",l_Name.c_str()));

p_vector.push_back(l_Name);
l_Name.assign(p_Name);
}
}
}
closedir(pDirStream);
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<string>::iterator it = p_vector.begin(); it !=
p_vector.end(); it++)
{
LogInfo(("\nNext directory %s\n",it->c_str()));
pDirStream = opendir(it->c_str());
LogInfo(("\n%s Directory
opened\n",it->c_str()));
while(pDirEntry = readdir(pDirStream) )
{
if( pDirEntry->d_type == DT_DIR )
{
count++;
if(count>2)
{
l_Name.append("/");

l_Name.append(string(pDirEntry->d_name));
LogInfo(("\n\nThe
subpath is: %s\n",l_Name.c_str()));

p_vector.push_back(l_Name);
l_Name.assign(p_Name);
}
}
}
closedir(pDirStream);
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
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...
3
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...
13
by: zaineb | last post by:
Hi, This is a follow-up of sort of this thread:...
23
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: ...
2
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
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
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
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...
16
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...
0
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);
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.