K
Kamran
I am a newbie in C++ and having a lot of problems with vectors.
I have a "vector <string> channels" and this may contain
several copies of the same string. I need to find the indices
of all the occurances of that particular string in "channels".
this is the code that is supposed to do the job:
---------------
vector<string>::iterator index;
index = channels.begin();
while (index != channels.end()) {
pos = find(index,channels.end(),string("SPB2_BHZ").c_str()) -
channels.begin();
cout << "pos: " << pos << endl;
if (pos != channels.size()) {
cout << "pos " << pos << " ncopy " << ncopy << endl;
indices[ncopy++] = pos;
}
++index;
}
--------------
If there is only one occurance of "SPB2_BHZ" and it is at position 9 I
get 20 different hits:
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
which is 19 too many. I also get 11 hits at position 21 which is
the size of the vector !!! If there are more occurances of the string
I get a mess.
I suspect substraction of "channels.begin()" in the find statement
is the culprit but don't know how to do it.
Does anyone know how to do this ?
Thanks in advance
Kamran
I have a "vector <string> channels" and this may contain
several copies of the same string. I need to find the indices
of all the occurances of that particular string in "channels".
this is the code that is supposed to do the job:
---------------
vector<string>::iterator index;
index = channels.begin();
while (index != channels.end()) {
pos = find(index,channels.end(),string("SPB2_BHZ").c_str()) -
channels.begin();
cout << "pos: " << pos << endl;
if (pos != channels.size()) {
cout << "pos " << pos << " ncopy " << ncopy << endl;
indices[ncopy++] = pos;
}
++index;
}
--------------
If there is only one occurance of "SPB2_BHZ" and it is at position 9 I
get 20 different hits:
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
pos 9
which is 19 too many. I also get 11 hits at position 21 which is
the size of the vector !!! If there are more occurances of the string
I get a mess.
I suspect substraction of "channels.begin()" in the find statement
is the culprit but don't know how to do it.
Does anyone know how to do this ?
Thanks in advance
Kamran