Click to See Complete Forum and Search --> : going mad


kakalake
March 23rd, 2003, 12:32 PM
Hello!
I try to use the stl::search function but i am not able to figure out the problem. I thing I know how to use this function because i have already used it sucessfully serveral times but now it doesn´t work.


#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>

using namespace std;

int main()
{
vector<string> vec(1);
vec[0] = "#include";

ifstream ifs( __FILE__ );
istream_iterator<string> is( ifs ), end;

//copy( is, end, ostream_iterator<string>(cout, "\n" ) );
istream_iterator<string> it = search( is, end, vec.begin(), vec.end() );

cout << it->c_str();

return 0;
}


The stl::copy function works fine and outputs me the whole file but the search function doesn´t work.
I tried to debug the function "search" and i saw that in the first loop the iterator "is" has a correct value but in the next loops the iterator "is" keeps always the value of a empty string.
Can somone help me ?

Thanks in advance

kakalake
March 23rd, 2003, 12:51 PM
I found the error maybe.
The istream_iterator is a input_iterator_tag and the function search uses the function _Distance (for-loop) to count the number of element inside the container. So the function _Distance puts the iterator (the filepointer) at the end of the file. Is this rigth???