Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to check iterator for NULL

How to check iterator for NULL

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • 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.
  • K Offline
    K Offline
    KASR1
    wrote on last edited by
    #1

    vector<int> vect;
    vector<int>::iterator pBegin;

    I want to check the iterator for NULL. In VC6 we can compare if (pBegin == NULL) and it works. But in VC10 we cannot compare iterator to NULL. How to achieve that in VC10?

    M S 2 Replies Last reply
    0
    • K KASR1

      vector<int> vect;
      vector<int>::iterator pBegin;

      I want to check the iterator for NULL. In VC6 we can compare if (pBegin == NULL) and it works. But in VC10 we cannot compare iterator to NULL. How to achieve that in VC10?

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      You should check against begin or end (usually past the end). if you have

      KASR1 wrote:

      vector<int>::iterator pBegin;

      Than it should be treated as an uninitialized variable, so it it give out bad results if used as-is. As far as I know, the STL implementation in VC6 is crap and not standard.

      Watched code never compiles.

      K 1 Reply Last reply
      0
      • M Maximilien

        You should check against begin or end (usually past the end). if you have

        KASR1 wrote:

        vector<int>::iterator pBegin;

        Than it should be treated as an uninitialized variable, so it it give out bad results if used as-is. As far as I know, the STL implementation in VC6 is crap and not standard.

        Watched code never compiles.

        K Offline
        K Offline
        KASR1
        wrote on last edited by
        #3

        Ok. Lets assume its not initialized. Can we consider checking if (pBegin != vect.end()) in place of if( pBegin != NULL)

        M 1 Reply Last reply
        0
        • K KASR1

          Ok. Lets assume its not initialized. Can we consider checking if (pBegin != vect.end()) in place of if( pBegin != NULL)

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          You need to assign the iterator to something valid before using it.

          std::vector v;
          // fill the vector.

          std::vector::iterator pBegin = v.begin();

          while ( pBegin != v.end() )
          {
          // do something.
          ++it;
          }

          or

          if ( pBegin != v.end() )
          {
          // ite
          }

          Watched code never compiles.

          K 2 Replies Last reply
          0
          • M Maximilien

            You need to assign the iterator to something valid before using it.

            std::vector v;
            // fill the vector.

            std::vector::iterator pBegin = v.begin();

            while ( pBegin != v.end() )
            {
            // do something.
            ++it;
            }

            or

            if ( pBegin != v.end() )
            {
            // ite
            }

            Watched code never compiles.

            K Offline
            K Offline
            KASR1
            wrote on last edited by
            #5

            Thanks for your input.

            1 Reply Last reply
            0
            • M Maximilien

              You need to assign the iterator to something valid before using it.

              std::vector v;
              // fill the vector.

              std::vector::iterator pBegin = v.begin();

              while ( pBegin != v.end() )
              {
              // do something.
              ++it;
              }

              or

              if ( pBegin != v.end() )
              {
              // ite
              }

              Watched code never compiles.

              K Offline
              K Offline
              KASR1
              wrote on last edited by
              #6

              std::vector<int>::iterator pIter = v.end();

              //do Some operation on pIter

              if ( pIter != v.end() )
              {
              // do something

              }

              Is the above logic make sense?

              1 Reply Last reply
              0
              • K KASR1

                vector<int> vect;
                vector<int>::iterator pBegin;

                I want to check the iterator for NULL. In VC6 we can compare if (pBegin == NULL) and it works. But in VC10 we cannot compare iterator to NULL. How to achieve that in VC10?

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #7

                Checking it against NULL is wrong. It's an implementation detail that it works in vc6 (because raw pointers are used, amongst other implementation specific details). Iterators are not required be pointers, in fact vectors are one of the few containers where they can do the job and as you've discovered even then they are not the only option.

                Steve

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups