vector iterators

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • edu.mvk

    #1

    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,

  • Stuart Redmann

    #2
    Re: vector iterators

    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

    Comment

    • Kai-Uwe Bux

      #3
      Re: vector iterators

      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

      Comment

      • Salt_Peter

        #4
        Re: vector iterators


        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.

        Comment

        Working...