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. using std::find in while loop

using std::find in while loop

Scheduled Pinned Locked Moved C / C++ / MFC
databasegraphicsquestion
10 Posts 5 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.
  • B Offline
    B Offline
    b rad311
    wrote on last edited by
    #1

    Hello, Can anyone tell me why the "index" variable isn't changing in my while loop, despite the fact that the vector "vec" has several "0." values stored in it? My goal is to find the index of each zero value in the vector "vec". The very first value calculated for the "index" variable is correct, but for the following loops in the while statement, it never updates from this value. Thanks!

    vector <long double>::iterator varying_start;
    varying_start=vec.begin();
    vector <long double>::iterator start;
    start=vec.begin();

    while(true)
    {
    varying_start= find(varying_start,vec.end() , 0.);
    int index=varying_start-start;
    }

    J D L A 4 Replies Last reply
    0
    • B b rad311

      Hello, Can anyone tell me why the "index" variable isn't changing in my while loop, despite the fact that the vector "vec" has several "0." values stored in it? My goal is to find the index of each zero value in the vector "vec". The very first value calculated for the "index" variable is correct, but for the following loops in the while statement, it never updates from this value. Thanks!

      vector <long double>::iterator varying_start;
      varying_start=vec.begin();
      vector <long double>::iterator start;
      start=vec.begin();

      while(true)
      {
      varying_start= find(varying_start,vec.end() , 0.);
      int index=varying_start-start;
      }

      J Offline
      J Offline
      josda1000
      wrote on last edited by
      #2

      Don't you want to use varying_start= vec.find(varying_start,vec.end() , 0.); instead of varying_start= find(varying_start,vec.end() , 0.);? I'm not saying this is the answer, but it could be.

      B 1 Reply Last reply
      0
      • J josda1000

        Don't you want to use varying_start= vec.find(varying_start,vec.end() , 0.); instead of varying_start= find(varying_start,vec.end() , 0.);? I'm not saying this is the answer, but it could be.

        B Offline
        B Offline
        b rad311
        wrote on last edited by
        #3

        Hello, If I do that then I get the following error:

        eror C2039: 'find' : is not a member of 'std::vector<_Ty>'

        J 1 Reply Last reply
        0
        • B b rad311

          Hello, If I do that then I get the following error:

          eror C2039: 'find' : is not a member of 'std::vector<_Ty>'

          J Offline
          J Offline
          josda1000
          wrote on last edited by
          #4

          What are the elements of your vector?

          B 1 Reply Last reply
          0
          • J josda1000

            What are the elements of your vector?

            B Offline
            B Offline
            b rad311
            wrote on last edited by
            #5

            0.000000
            0.000000
            0.013180
            0.424146
            1.300951
            2.690247
            4.658776
            6.345779
            7.057368
            7.186754
            6.796806
            6.209223
            6.240955
            6.789929
            7.257117
            7.541815
            7.229695
            5.995904
            4.311499
            2.421349
            0.651686
            -0.396626
            -0.931562
            -1.667433
            -3.014668
            -5.174474
            -7.164814
            -7.445005
            0.000000

            1 Reply Last reply
            0
            • B b rad311

              Hello, Can anyone tell me why the "index" variable isn't changing in my while loop, despite the fact that the vector "vec" has several "0." values stored in it? My goal is to find the index of each zero value in the vector "vec". The very first value calculated for the "index" variable is correct, but for the following loops in the while statement, it never updates from this value. Thanks!

              vector <long double>::iterator varying_start;
              varying_start=vec.begin();
              vector <long double>::iterator start;
              start=vec.begin();

              while(true)
              {
              varying_start= find(varying_start,vec.end() , 0.);
              int index=varying_start-start;
              }

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              b-rad311 wrote:

              Can anyone tell me why the "index" variable isn't changing in my while loop, despite the fact that the vector "vec" has several "0." values stored in it?

              Because varying_start always "points" to the same starting location. Increment it after the index assignment. You'll also want to provide a way out of the while() loop.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Man who follows car will be exhausted." - Confucius

              B 1 Reply Last reply
              0
              • B b rad311

                Hello, Can anyone tell me why the "index" variable isn't changing in my while loop, despite the fact that the vector "vec" has several "0." values stored in it? My goal is to find the index of each zero value in the vector "vec". The very first value calculated for the "index" variable is correct, but for the following loops in the while statement, it never updates from this value. Thanks!

                vector <long double>::iterator varying_start;
                varying_start=vec.begin();
                vector <long double>::iterator start;
                start=vec.begin();

                while(true)
                {
                varying_start= find(varying_start,vec.end() , 0.);
                int index=varying_start-start;
                }

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You need to increment varying_start after the assignment. You also need to check the return from find() to see whether you have hit the end of the vector. [edit]I see David posted the answer while I was running my tests[/edit]

                It's time for a new signature.

                1 Reply Last reply
                0
                • D David Crow

                  b-rad311 wrote:

                  Can anyone tell me why the "index" variable isn't changing in my while loop, despite the fact that the vector "vec" has several "0." values stored in it?

                  Because varying_start always "points" to the same starting location. Increment it after the index assignment. You'll also want to provide a way out of the while() loop.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Man who follows car will be exhausted." - Confucius

                  B Offline
                  B Offline
                  b rad311
                  wrote on last edited by
                  #8

                  Got it, thanks!

                  1 Reply Last reply
                  0
                  • B b rad311

                    Hello, Can anyone tell me why the "index" variable isn't changing in my while loop, despite the fact that the vector "vec" has several "0." values stored in it? My goal is to find the index of each zero value in the vector "vec". The very first value calculated for the "index" variable is correct, but for the following loops in the while statement, it never updates from this value. Thanks!

                    vector <long double>::iterator varying_start;
                    varying_start=vec.begin();
                    vector <long double>::iterator start;
                    start=vec.begin();

                    while(true)
                    {
                    varying_start= find(varying_start,vec.end() , 0.);
                    int index=varying_start-start;
                    }

                    A Offline
                    A Offline
                    Aescleal
                    wrote on last edited by
                    #9

                    The problem is that when std::find succeeds it's referring to an element holding a zero. At the moment when you call std::find again the first element in the range with a zero in it is the one it's just found.

                    for( std::vector::iterator start = v.begin(); start != v.end(); )
                    {
                    start = std::find( start, v.end(), 0 );
                    if( start != v.end() )
                    {
                    indices.push_back( start - v.begin() );
                    ++start;
                    }
                    }

                    You can probably collapse a few of the lines down a bit but it's probably not worth it unless you like code to tease the reader. The important differences between this and your original version is the ++start line. If you find a zero it bumps the iterator past it so the next call to find doesn't return hit the same thing again. Cheers, Ash PS: This is actually just what David and Richard said in their posts, I'll read the entire thread next time before answering :-)

                    B 1 Reply Last reply
                    0
                    • A Aescleal

                      The problem is that when std::find succeeds it's referring to an element holding a zero. At the moment when you call std::find again the first element in the range with a zero in it is the one it's just found.

                      for( std::vector::iterator start = v.begin(); start != v.end(); )
                      {
                      start = std::find( start, v.end(), 0 );
                      if( start != v.end() )
                      {
                      indices.push_back( start - v.begin() );
                      ++start;
                      }
                      }

                      You can probably collapse a few of the lines down a bit but it's probably not worth it unless you like code to tease the reader. The important differences between this and your original version is the ++start line. If you find a zero it bumps the iterator past it so the next call to find doesn't return hit the same thing again. Cheers, Ash PS: This is actually just what David and Richard said in their posts, I'll read the entire thread next time before answering :-)

                      B Offline
                      B Offline
                      b rad311
                      wrote on last edited by
                      #10

                      Thanks Ash. This is a very good explanation!

                      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