string iterators--Accelerated C++ example
I can't get the following code from Accelerated C++ to work. It seems to be a problem with the string interators being passed to the find_if() <algorithm>. The error says the iterator arguments are ambiguous: they could be char* or const char*:
Code:
#include <iostream>
#include <string>
#include<vector>
#include <algorithm>
using namespace std;
bool space(char ch)
{
//if ch is a space, returns true:
return isspace(ch)?true:false;
}
bool not_space(char ch)
{
//if ch is not a space, returns true:
return isspace(ch)?false:true;
}
int main()
{
string str = "Although functors and predicates are cool, you"
"should be very careful when writing unary and binary functors."
"Except when used with the std::for_each algorithm, the context"
"they hold should be constant.";
vector<string> words;
string::const_iterator i = str.begin();
string::const_iterator j = 0;
while(i != str.end())
{
i = find_if(i, str.end(), not_space); //error
//rest of code
}
return 0;
}
C:\Beginning C++\test2\main.cpp(37) : error C2782: '_II __cdecl std::find_if(_II,_II,_Pr)' : template parameter '_II' is ambiguous
could be 'char *'
or 'const char *'