Hey guys, I'm making a global operator, and I have a line where I'm comparing a std::string::difference_type to a std::string::size_type, I think the difference_type is usually a signed integer, and the size_type an unsigned..
Any ideas for an algorithm change? Also, there is a lambda function used I would like to remove without adding another function.
Basically the algorithm will malfunction if the std::string &left has only empty spaces,
so there is a count_if() to count spaces and compare the count to the size.
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
//global operator overload.
//define STRING_OPER_RIGHT_SHIFT_REVERSE
//if you want "string right" to have the characters
//in reverse order.
bool operator>>(std::string &left, std::string &right){
typedef std::string::reverse_iterator rit;
if( !left.size() )
return false;
rit it_begin;
rit it_end;
rit it_e_end;//erase section end (whitespace removal)
rit it;
std::string::difference_type sum = 0;
//detect if all spaces.
sum =
std::count_if(left.begin(),left.end(), [](std::string::value_type ch){
return std::isspace(ch) ? true : false;
});
if( sum == left.size() ){
left.clear();
return false;
}
//Loop backward, find first !isspace or begin()
for(it = left.rbegin(); it != left.rend() && std::isspace(*it); ++it) {}
it_begin = it;
//Loop backward from previous iterator, find first isspace, or begin()
for( ; it != left.rend() && !std::isspace(*it); ++it) {}
it_end = it;
//Loop backward from previous iterator, find first !isspace, or begin()
for( ; it != left.rend() && std::isspace(*it); ++it) {}
it_e_end = it;
//Remove range from left, set right equal to.
right.clear();
//Copy from begin to end.
#ifdef STRING_OPER_RIGHT_SHIFT_REVERSE
right.insert(right.begin(),it_begin,it_end);
#else
right.insert(right.begin(), it_end.base(),it_begin.base());
#endif
//Erase from ebegin, to end.
left.erase(it_e_end.base(),it_begin.base());
return true;
}
int main()
{
std::string input;
std::string token;
std::getline(std::cin,input);
while(input >> token)
{
std::cout << token << std::endl;
}
}
I started with:
begin = {
loop backward, find first !isspace, or begin()
}
end = {
loop backward from previous iterator, find first isspace() or begin()
}
e_end = {
loop backward from previous iterator, find first !isspace(), or begin().
}
copy begin to end to right hand side
erase begin to e_end (on left hand side string)