All that is very unnecessary to just test if a string has a space in it. This is all you need:
#include <ctype.h>
bool hasspace = std::find_if(str.begin(), str.end(), ::isspace) != str.end();
:: is the scope resolution operator specifying that isspace is a global function, not the similarly-named std::isspace, and find_if is a function inside std::. If you use using namespace std; then you don't need std:: but you do still need the plain ::.
The find_if function takes an iterator to the beginning of the string, an iterator to the end of the string, and a function that takes an argument and returns some value convertible to a bool. find_if iterates from the first iterator to the second iterator, passing each value of the current item to the function you gave it, and if the function returns true, find_if returns the iterator that caused the function to return true. If find_if goes through to the end and the function never returns true, then it returns an iterator to the end of the range, which in this case is str.end().
That means that if find_if returns str.end(), it got to the end of the string without isspace returning true, which means there was no space characters in the string. Therefore, you can test the result of find_if against str.end(); If they are unequal (!=), that means there was a space in the string, and hasspace is true. Else, hasspace is false.
All that is very unnecessary to just test if a string has a space in it. This is all you need:
#include <ctype.h>
bool hasspace = std::find_if(str.begin(), str.end(), ::isspace) != str.end();
:: is the scope resolution operator specifying that isspace is a global function, not the similarly-named std::isspace, and find_if is a function inside std::. If you use using namespace std; then you don't need std:: but you do still need the plain ::.
The find_if function takes an iterator to the beginning of the string, an iterator to the end of the string, and a function that takes an argument and returns some value convertible to a bool. find_if iterates from the first iterator to the second iterator, passing each value of the current item to the function you gave it, and if the function returns true, find_if returns the iterator that caused the function to return true. If find_if goes through to the end and the function never returns true, then it returns an iterator to the end of the range, which in this case is str.end().
That means that if find_if returns str.end(), it got to the end of the string without isspace returning true, which means there was no space characters in the string. Therefore, you can test the result of find_if against str.end(); If they are unequal (!=), that means there was a space in the string, and hasspace is true. Else, hasspace is false.
here is another way, if the above version seems strange, or it's above your knowledge
if(marks[i] == ' ') {
cout<<"Space found!";
}
Why I cannot put std in front of ::isspace ?
enter and isspace - C++ Forum
How do I make my program ignore spaces at the very beginning or very end of a string?
Why isspace dont register '\n' (c++)?
Hi everyone,
I recently watched the CppCon talk "C++20 Ranges in Practice" (https://youtu.be/d_E-VLyUnzc?t=3516) and there is something I did not understand.
In an example he gave, why did he omits the std before the ::isspace in this line: inline constexpr auto trim_front = views::drop_while(::isspace); When I try to put the std, the compiler gives me an error in this Compiler Explorer example (https://godbolt.org/z/sfnKnfvnP)
I tried to search why on internet but I don't even know what I should search ^^
Could you give me some insights about that pls ?