Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. loop through a vector using iterator and test for end of loop-
QtWS25 Last Chance

loop through a vector using iterator and test for end of loop-

Scheduled Pinned Locked Moved Solved C++ Gurus
20 Posts 5 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Christian Ehrlicher
    26 Jul 2020, 09:39

    @Natural_Bugger said in loop through a vector using iterator and test for end of loop-:

    but how do i know i reached the last "iteration" of the loop?

    You can do it exactly the same as in a for loop which a loop variable

    if (it == myList.end() - 1)
    

    or use the solution from @Chris-Kawa

    N Offline
    N Offline
    Natural_Bugger
    wrote on 26 Jul 2020, 10:03 last edited by
    #9

    @Christian-Ehrlicher

    thnx

    1 Reply Last reply
    0
    • C Chris Kawa
      26 Jul 2020, 09:35
      if (it == std::prev(mylist.end()))
      
      N Offline
      N Offline
      Natural_Bugger
      wrote on 26 Jul 2020, 10:30 last edited by Natural_Bugger
      #10

      @Chris-Kawa
      @Christian-Ehrlicher

      i wanna remove the current element i'm "testing"

      http://www.cplusplus.com/reference/vector/vector/erase/

      • iterator erase (iterator position);
      • iterator erase (iterator first, iterator last);
      std::cout << "*it: " << *it << std::endl; // outputs the correct  numbers
      mylist.erase(*it);
      

      error:

      error: no matching function for call to ‘std::vector<int>::erase(int&)’                    
      

      i also tried:

      mylist..erase(ar.begin() + *it);
      

      results is fatal crash.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 26 Jul 2020, 10:50 last edited by Chris Kawa
        #11

        *it is the value pointed to by the iterator. ar.begin() + *it is nonsense. It advances the iterator by value pointed to by it.

        You want to remove the element at iterator so simply

        mylist.erase(it);
        

        Keep in mind that erasing the element invalidates the iterator, so if you're doing it in a loop you need to use the iterator returned by erase:

        it = mylist.erase(it);
        

        which erases the element pointed by it and then sets it to point to next element.

        N 2 Replies Last reply 26 Jul 2020, 11:15
        4
        • C Chris Kawa
          26 Jul 2020, 10:50

          *it is the value pointed to by the iterator. ar.begin() + *it is nonsense. It advances the iterator by value pointed to by it.

          You want to remove the element at iterator so simply

          mylist.erase(it);
          

          Keep in mind that erasing the element invalidates the iterator, so if you're doing it in a loop you need to use the iterator returned by erase:

          it = mylist.erase(it);
          

          which erases the element pointed by it and then sets it to point to next element.

          N Offline
          N Offline
          Natural_Bugger
          wrote on 26 Jul 2020, 11:15 last edited by Natural_Bugger
          #12

          @Chris-Kawa
          thnx,
          ah ... removing the "*", i dind't try that.
          at first when i tried to use "if else" statements inside the loop, the compiler couldn't convert "int" into pointer.
          so, i though i had to use it.

          if(mylistr[0] == *it){
                              std::cout << "*it: " << *it << std::endl; // i do need to use pointer here
                              mylist.erase(mylist.begin()); // remove first
                              mylist.erase(it); // remove current.
                          break;
                          }
          

          i'm existing the loop after i remove the element.

          break;
          

          but i still have error on certain conditions, using a list with filled different values, some are valid, some crash.

          Program terminated with signal SIGSEGV, Segmentation fault.
          
          1 Reply Last reply
          0
          • C Chris Kawa
            26 Jul 2020, 10:50

            *it is the value pointed to by the iterator. ar.begin() + *it is nonsense. It advances the iterator by value pointed to by it.

            You want to remove the element at iterator so simply

            mylist.erase(it);
            

            Keep in mind that erasing the element invalidates the iterator, so if you're doing it in a loop you need to use the iterator returned by erase:

            it = mylist.erase(it);
            

            which erases the element pointed by it and then sets it to point to next element.

            N Offline
            N Offline
            Natural_Bugger
            wrote on 26 Jul 2020, 11:29 last edited by Natural_Bugger
            #13

            @Chris-Kawa

            this is how my loops looks like ...

            find a pair of matching numbers and remove them.
            the find next pair ...
            etc

            int pairs = 0;
            int counter = 0;
            
            do{
                    if(counter == n){ // n = number of number in list.
                        break;
                    }
            int counterB = 0;
            bool remove = false;
            for(std::vector<int>::iterator it = (mylist.begin()+ 1); it != mylist.end(); ++it) {
                            if(mylist[0] == *it){
                                std::cout << "*it: " << *it << std::endl;
                                mylist.erase(mylist.begin());
                                mylist.erase(it); // fails occasionally, but more the the line below.
                                // mylist..erase(mylist.begin() + counterB); // works 100% ... but does not pas all tests.
                                pairs++;
                            break;
                            }
                            if(it == std::prev(mylist.end())){
                                remove = true; // bool value
                            }
                               counterB++;
                    }
            if(remove){
                        mylist.erase(mylist.begin()); // value didn't occur more than once, remove first value.
                        remove = false;
                    }
            while(true);
            ...
            ..
            .
            

            kind regards.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 26 Jul 2020, 11:39 last edited by Chris Kawa
              #14

              @Natural_Bugger said:

              mylist.erase(mylist.begin());
              mylist.erase(it); // fails occasionally, but more the the line below.

              As I said in my previous post erase invalidates iterators so it in the second line points to garbage. For these two lines it's enough to switch them, but you're using it couple lines lower and that's also an error. I repeat - whenever you use erase any iterator you have is invalid. erase returns an iterator to element after the one that was removed. Keep in mind that the removed element could be the last one so iterator returned from erase can be end() so pointing past the container.

              By the way, if all you want to do is remove duplicates you can just do:

              std::sort(mylist.begin(), mylist.end());
              mylist.erase(std::unique(mylist.begin(), mylist.end()), mylist.end() );
              
              N 4 Replies Last reply 26 Jul 2020, 11:45
              1
              • C Chris Kawa
                26 Jul 2020, 11:39

                @Natural_Bugger said:

                mylist.erase(mylist.begin());
                mylist.erase(it); // fails occasionally, but more the the line below.

                As I said in my previous post erase invalidates iterators so it in the second line points to garbage. For these two lines it's enough to switch them, but you're using it couple lines lower and that's also an error. I repeat - whenever you use erase any iterator you have is invalid. erase returns an iterator to element after the one that was removed. Keep in mind that the removed element could be the last one so iterator returned from erase can be end() so pointing past the container.

                By the way, if all you want to do is remove duplicates you can just do:

                std::sort(mylist.begin(), mylist.end());
                mylist.erase(std::unique(mylist.begin(), mylist.end()), mylist.end() );
                
                N Offline
                N Offline
                Natural_Bugger
                wrote on 26 Jul 2020, 11:45 last edited by
                #15

                @Chris-Kawa

                how do i store the position of the element, found by the condition of the statement and remove it outside the loop.

                if(mylistr[0] == *it){
                    ...
                   ..
                   .
                                break;
                  }
                

                regards.

                1 Reply Last reply
                0
                • C Chris Kawa
                  26 Jul 2020, 11:39

                  @Natural_Bugger said:

                  mylist.erase(mylist.begin());
                  mylist.erase(it); // fails occasionally, but more the the line below.

                  As I said in my previous post erase invalidates iterators so it in the second line points to garbage. For these two lines it's enough to switch them, but you're using it couple lines lower and that's also an error. I repeat - whenever you use erase any iterator you have is invalid. erase returns an iterator to element after the one that was removed. Keep in mind that the removed element could be the last one so iterator returned from erase can be end() so pointing past the container.

                  By the way, if all you want to do is remove duplicates you can just do:

                  std::sort(mylist.begin(), mylist.end());
                  mylist.erase(std::unique(mylist.begin(), mylist.end()), mylist.end() );
                  
                  N Offline
                  N Offline
                  Natural_Bugger
                  wrote on 26 Jul 2020, 11:56 last edited by Natural_Bugger
                  #16

                  @Chris-Kawa

                  thnx

                  std::sort(mylist.begin(), mylist.end());
                  mylist.erase(std::unique(mylist.begin(), mylist.end()), mylist.end() );
                  

                  i need to find a pair, count it, remove that pair, find next pair, count it, etc, etc ... but not all number in the list have a pair.

                  well ... i just need to count the pairs ... somehow,
                  so i think the best way to do that, is to find a pair and remove it, so you don't keep finding it over and over.

                  1 Reply Last reply
                  0
                  • C Chris Kawa
                    26 Jul 2020, 11:39

                    @Natural_Bugger said:

                    mylist.erase(mylist.begin());
                    mylist.erase(it); // fails occasionally, but more the the line below.

                    As I said in my previous post erase invalidates iterators so it in the second line points to garbage. For these two lines it's enough to switch them, but you're using it couple lines lower and that's also an error. I repeat - whenever you use erase any iterator you have is invalid. erase returns an iterator to element after the one that was removed. Keep in mind that the removed element could be the last one so iterator returned from erase can be end() so pointing past the container.

                    By the way, if all you want to do is remove duplicates you can just do:

                    std::sort(mylist.begin(), mylist.end());
                    mylist.erase(std::unique(mylist.begin(), mylist.end()), mylist.end() );
                    
                    N Offline
                    N Offline
                    Natural_Bugger
                    wrote on 26 Jul 2020, 13:37 last edited by Natural_Bugger
                    #17

                    @Chris-Kawa

                    in the end, i changed the code to store the positions and remove it outside the loop.
                    worked fine, but the online compiler didn't pas all tests.
                    than i tried with the Qt creator IDE and the code return good results.

                    now, I'm trying that what you suggesting.

                    std::sort(mylist.begin(), mylist.end());
                    

                    but how to turn

                    vector::begin
                    vector::end
                    

                    into a INT values to compare against?
                    so i don't over run the max length of the list.

                    the code became much much smaller, but also tries to compare value beyond the end of the vector.

                       int pairs = 0;
                        sort( mylist.begin(), mylist.end() );
                        int counter = 0;
                        for(auto ii : mylist){
                            if(mylist.at(counter) == mylist.at(counter + 1)){
                                std::cout << "found duplicate" << std::endl;
                                pairs++;
                            } 
                            counter ++;
                        }
                    
                    1 Reply Last reply
                    0
                    • C Chris Kawa
                      26 Jul 2020, 11:39

                      @Natural_Bugger said:

                      mylist.erase(mylist.begin());
                      mylist.erase(it); // fails occasionally, but more the the line below.

                      As I said in my previous post erase invalidates iterators so it in the second line points to garbage. For these two lines it's enough to switch them, but you're using it couple lines lower and that's also an error. I repeat - whenever you use erase any iterator you have is invalid. erase returns an iterator to element after the one that was removed. Keep in mind that the removed element could be the last one so iterator returned from erase can be end() so pointing past the container.

                      By the way, if all you want to do is remove duplicates you can just do:

                      std::sort(mylist.begin(), mylist.end());
                      mylist.erase(std::unique(mylist.begin(), mylist.end()), mylist.end() );
                      
                      N Offline
                      N Offline
                      Natural_Bugger
                      wrote on 26 Jul 2020, 17:59 last edited by Natural_Bugger
                      #18

                      intput:

                      20 // number of value
                      4 5 5 5 6 6 4 1 4 4 3 6 6 3 6 1 4 5 5 5
                      

                      sorting:

                      std::sort(mylist.begin(), mylist.end());
                      

                      atfter sorting:

                      1 1 3 3 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 
                      
                      int example(vector<int> mylist){
                      sort( ar.begin(), ar.end() );
                      int pairs = 0;
                      unsigned long int counterC = 0;
                          do{
                      
                              if(counterC == (mylist.size() - 1)){
                                  std::cout << "end loop" << std::endl;
                                  break;
                              }
                              else
                              {
                                  do
                                  {
                                      if(mylist[0] == mylist[1])
                                      {
                                          // equal;
                                          mylist.erase(mylist.begin()); // erase first element, than try to erase next ...... it works ... but partially ... at first iteration it does remove the first 2 element, but not on the following.
                                          mylist.erase(mylist.begin()); // breaks "sometimes" the code according to Qt creator debug
                                          // Program terminated with signal SIGSEGV, Segmentation fault.
                                          std::cout << "found equals" << std::endl;
                                          pairs++;
                                          break;
                                      }
                                      else
                                      {
                                          // remove
                                          std::cout << "found soletary" << mylist.at(0) << std::endl;
                                          ar.erase(ar.begin());
                                          break;
                                      }
                                  }
                                  while(true);
                              }
                              for(auto ii : mylist)
                              {
                                  std::cout << "ii: "<< ii << " ";
                              }
                              std::cout << std::endl;
                              std::cout << "counterC: " << counterC << std::endl;
                              counterC++;
                          }
                          while(true);
                      return pairs;
                      }
                      

                      i also tried:

                      mylist.erase(mylist.begin(), mylist.begin()+1);
                      

                      this is the total output:

                      found equals
                      ii: 1 ii: 3 ii: 3 ii: 4 ii: 4 ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 0
                      found soletary1
                      ii: 3 ii: 3 ii: 4 ii: 4 ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 1
                      found equals
                      ii: 3 ii: 4 ii: 4 ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 2
                      found soletary3
                      ii: 4 ii: 4 ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 3
                      found equals
                      ii: 4 ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 4
                      found equals
                      ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 5
                      found equals
                      ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 6
                      found equals
                      ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 7
                      found soletary4
                      ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 8
                      found equals
                      ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 9
                      found equals
                      ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 10
                      found equals
                      ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 11
                      found equals
                      ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 12
                      found equals
                      ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 13
                      found soletary5
                      ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 14
                      found equals
                      ii: 6 ii: 6 ii: 6 ii: 6 
                      counterC: 15
                      found equals
                      ii: 6 ii: 6 ii: 6 
                      counterC: 16
                      found equals
                      ii: 6 ii: 6 
                      counterC: 17
                      found equals
                      ii: 6 
                      counterC: 18
                      found equals
                      
                      counterC: 19
                      
                      
                      J 1 Reply Last reply 27 Jul 2020, 08:49
                      0
                      • N Natural_Bugger
                        26 Jul 2020, 17:59

                        intput:

                        20 // number of value
                        4 5 5 5 6 6 4 1 4 4 3 6 6 3 6 1 4 5 5 5
                        

                        sorting:

                        std::sort(mylist.begin(), mylist.end());
                        

                        atfter sorting:

                        1 1 3 3 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 
                        
                        int example(vector<int> mylist){
                        sort( ar.begin(), ar.end() );
                        int pairs = 0;
                        unsigned long int counterC = 0;
                            do{
                        
                                if(counterC == (mylist.size() - 1)){
                                    std::cout << "end loop" << std::endl;
                                    break;
                                }
                                else
                                {
                                    do
                                    {
                                        if(mylist[0] == mylist[1])
                                        {
                                            // equal;
                                            mylist.erase(mylist.begin()); // erase first element, than try to erase next ...... it works ... but partially ... at first iteration it does remove the first 2 element, but not on the following.
                                            mylist.erase(mylist.begin()); // breaks "sometimes" the code according to Qt creator debug
                                            // Program terminated with signal SIGSEGV, Segmentation fault.
                                            std::cout << "found equals" << std::endl;
                                            pairs++;
                                            break;
                                        }
                                        else
                                        {
                                            // remove
                                            std::cout << "found soletary" << mylist.at(0) << std::endl;
                                            ar.erase(ar.begin());
                                            break;
                                        }
                                    }
                                    while(true);
                                }
                                for(auto ii : mylist)
                                {
                                    std::cout << "ii: "<< ii << " ";
                                }
                                std::cout << std::endl;
                                std::cout << "counterC: " << counterC << std::endl;
                                counterC++;
                            }
                            while(true);
                        return pairs;
                        }
                        

                        i also tried:

                        mylist.erase(mylist.begin(), mylist.begin()+1);
                        

                        this is the total output:

                        found equals
                        ii: 1 ii: 3 ii: 3 ii: 4 ii: 4 ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 0
                        found soletary1
                        ii: 3 ii: 3 ii: 4 ii: 4 ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 1
                        found equals
                        ii: 3 ii: 4 ii: 4 ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 2
                        found soletary3
                        ii: 4 ii: 4 ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 3
                        found equals
                        ii: 4 ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 4
                        found equals
                        ii: 4 ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 5
                        found equals
                        ii: 4 ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 6
                        found equals
                        ii: 4 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 7
                        found soletary4
                        ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 8
                        found equals
                        ii: 5 ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 9
                        found equals
                        ii: 5 ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 10
                        found equals
                        ii: 5 ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 11
                        found equals
                        ii: 5 ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 12
                        found equals
                        ii: 5 ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 13
                        found soletary5
                        ii: 6 ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 14
                        found equals
                        ii: 6 ii: 6 ii: 6 ii: 6 
                        counterC: 15
                        found equals
                        ii: 6 ii: 6 ii: 6 
                        counterC: 16
                        found equals
                        ii: 6 ii: 6 
                        counterC: 17
                        found equals
                        ii: 6 
                        counterC: 18
                        found equals
                        
                        counterC: 19
                        
                        
                        J Offline
                        J Offline
                        JonB
                        wrote on 27 Jul 2020, 08:49 last edited by JonB
                        #19

                        @Natural_Bugger
                        In some shape or form: when iterating through the elements looking at adjacent ones, you must either omit/skip the first and start at the second one (if you compare elements against the one to the left), or omit/skip the last one and stop at the penultimate one (if you compare elements against the one to the right).

                        N 1 Reply Last reply 27 Jul 2020, 11:26
                        0
                        • J JonB
                          27 Jul 2020, 08:49

                          @Natural_Bugger
                          In some shape or form: when iterating through the elements looking at adjacent ones, you must either omit/skip the first and start at the second one (if you compare elements against the one to the left), or omit/skip the last one and stop at the penultimate one (if you compare elements against the one to the right).

                          N Offline
                          N Offline
                          Natural_Bugger
                          wrote on 27 Jul 2020, 11:26 last edited by
                          #20

                          @JonB

                          thank you for the reply.

                          well, i saw that some people or most did that for the same problem, but in Java and apparently, they also sorted the "array/map" like Chris Kawa mentioned it worked for them.
                          so i tried that as well in c++.

                          bit i also think the "solutions" they published do not work, because they only compare 2 (if) or else (nothing). no taking in consideration if the list of values is either even or uneven in length or the numbers of equal value is either even or uneven.

                          now i'm using nested "Do While" loops instead of For loop in combination with Iteration.
                          what to do next?

                          .

                          1 Reply Last reply
                          0

                          18/20

                          26 Jul 2020, 17:59

                          • Login

                          • Login or register to search.
                          18 out of 20
                          • First post
                            18/20
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved