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. Iterator algorithm: assignment
QtWS25 Last Chance

Iterator algorithm: assignment

Scheduled Pinned Locked Moved Solved C++ Gurus
7 Posts 2 Posters 544 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.
  • W Offline
    W Offline
    Weichao Wang
    wrote on last edited by Christian Ehrlicher
    #1

    Dears,

    Is it possible to assign an iterator to another one? With the following code I want to test it. But nothing will be printed except "vector filled".

    #include <iostream>
    #include <vector>
    using namespace std;
    int main(void) {
        vector<int> ivec;
        for (int i=0; i<10; ++i)
            ivec.push_back(i);
        cout << "vector filled" << endl;
        vector<int>::const_iterator iter;
        vector<int>::const_iterator itermemo;
        for (iter = ivec.begin(); iter != ivec.end(); ++iter) {
            if ((*iter) == 3) itermemo = iter;
            ++iter;
        }
        iter = itermemo;
        do {
            --iter;
            cout << *iter << endl;
        } while (iter != ivec.begin());
        //iter = itermemo;
        for (iter = itermemo; iter != ivec.end(); ++iter) {
            cout << *iter << endl;
            ++iter;
        }
        return 0;
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      Because itermemo is not initialized at all and you don't check for iter != ivec.begin() before dereferencing the iterator in your second loop.
      Why it's not initialized at all is a task for you, simply inspect the first loop.

      /edit: same mistake in the third loop.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • W Offline
        W Offline
        Weichao Wang
        wrote on last edited by
        #3

        Dear Christian,

        ivec contains 0 through 9, and with " if ((*iter) == 3) itermemo = iter;" itermemo must be initialized, if the syntax is correct. I expect the output 2 through 0 with the do loop and 3 through 9 in the last loop.

        W 1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Weichao-Wang said in Iterator algorithm: assignment:

          itermemo must be initialized,

          No, please take a look on what you're doing with iter in your first and last loop.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • W Weichao Wang

            Dear Christian,

            ivec contains 0 through 9, and with " if ((*iter) == 3) itermemo = iter;" itermemo must be initialized, if the syntax is correct. I expect the output 2 through 0 with the do loop and 3 through 9 in the last loop.

            W Offline
            W Offline
            Weichao Wang
            wrote on last edited by Weichao Wang
            #5

            @Weichao-Wang
            Oh, I've found the problem. In the last two for loops I've repeated ++iter, which leads to the result that *iter == 3 would never be encountered. Thank you!

            W 1 Reply Last reply
            0
            • W Weichao Wang

              @Weichao-Wang
              Oh, I've found the problem. In the last two for loops I've repeated ++iter, which leads to the result that *iter == 3 would never be encountered. Thank you!

              W Offline
              W Offline
              Weichao Wang
              wrote on last edited by
              #6
              This post is deleted!
              Christian EhrlicherC 1 Reply Last reply
              0
              • W Weichao Wang

                This post is deleted!

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Weichao-Wang said in Iterator algorithm: assignment:

                double instead of doppelt (which is German).

                You can edit your posts by clicking on the three vertical dots right down of the post.
                And when the topic is solved you can mark it as such with the 'Topic Tools' button.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                2

                • Login

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