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.
How to use the isspace function in c++? - Stack Overflow
c++ - How to pass std::isspace to function taking unary predicate as argument? - Stack Overflow
Why I cannot put std in front of ::isspace ?
Does including <string> change the overload set of std::isspace?
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!";
}
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 ?
I am trialing the VS insider with some old code from a VS2017 project. I stumbled into a strange compilation error and after boiling it down to a minimal example on Compiler Explorer I found that it also generates an error on clang and gcc. I really want to understand if this code is actually incorrect or is this somehow a bug that all three vendors share (possibly in their libraries).
This code compiles:
#include <cctype>
#include <functional>
void test()
{
auto is_non_space = std::not_fn(std::isspace);
}But if I just change it to include the string header ...
#include <cctype>
#include <functional>
#include <string>
void test()
{
auto is_non_space = std::not_fn(std::isspace);
}Now the compilation fails with an error about not being able to determine the correct template substitution in not_fn. For example, clang 21.1.0 on compiler explorer gives
<source>:8:26: error: no matching function for call to 'not_fn'
8 | auto is_non_space = std::not_fn(std::isspace);
| ^~~~~~~~~~~
(long path)/include/c++/v1/__functional/not_fn.h:47:58: note: candidate template ignored: couldn't infer template argument '_Fn'
47 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto not_fn(_Fn&& __f) {
| ^I can resolve the problem by dropping the "std::" qualifier from isspace
#include <cctype>
#include <functional>
#include <string>
void test()
{
auto is_non_space = std::not_fn(isspace);
}After a little searching I see that there *is* a second std:isspace in the <locale> header and that would explain the compilation error, but I am not including locale in the failing example. So my questions are:
-
Does the <string> implementation include <locale> for some of these vendors?
-
If so, was that something that was changed since C++17?
-
If not, is there something else going on?