using std::find in while loop
-
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;
} -
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;
} -
Don't you want to use
varying_start= vec.find(varying_start,vec.end() , 0.);
instead ofvarying_start= find(varying_start,vec.end() , 0.);
? I'm not saying this is the answer, but it could be. -
Hello, If I do that then I get the following error:
eror C2039: 'find' : is not a member of 'std::vector<_Ty>'
-
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 -
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;
}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 theindex
assignment. You'll also want to provide a way out of thewhile()
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
-
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;
}You need to increment
varying_start
after the assignment. You also need to check the return fromfind()
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.
-
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 theindex
assignment. You'll also want to provide a way out of thewhile()
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
-
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;
}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 :-)
-
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 :-)