Abs () and an alternative simple if statement don't seem to work

I want to check if the length of 2 lines differs by more than 2. This is what I wrote.

#include <cmath>
#include <string>
if (abs(str1.size() - str2.size()) >= 2)
  return false;

Compiler Error:

1-5.cpp:8:9: error: call to 'abs' is ambiguous
    if (abs(str1.size() - str2.size()) >= 2)
        ^~~
/Applications/Xcode.app/Contents/Develop
        /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/stdlib.h:129:6: note: 
          candidate function
    int      abs(int) __pure2;
             ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstdlib:167:44: note: 
          candidate function
    inline _LIBCPP_INLINE_VISIBILITY long      abs(     long __x) _NOEXCEPT {return  labs(__x);}
                                               ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstdlib:169:44: note: 
          candidate function
    inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {return llabs(__x);}
                                               ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:664:1: note: 
          candidate function
    abs(float __lcpp_x) _NOEXCEPT {return fabsf(__lcpp_x);}
    ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:668:1: note: 
          candidate function
    abs(double __lcpp_x) _NOEXCEPT {return fabs(__lcpp_x);}
    ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:672:1: note: 
          candidate function
    abs(long double __lcpp_x) _NOEXCEPT {return fabsl(__lcpp_x);}
    ^
    1 error generated.

Failed to solve the problem, I changed the if statement,

if (str1.size() - str2.size() >= 2 || str1.size() - str2.size() <= -2)
  return false;

Even this statement does not work correctly. No matter what lines I enter, the if statement always returns false.

+4
source share
3 answers

An indefinite call of 1 is not a real problem: it std::string::sizereturns std::size_t, which is an unsigned integer, therefore:

str1.size() - str2.size()

( "" 2 n ).

:

if (str1.size() > str2.size() + 2 || str2.size() > str1.size() + 2) {
    // Do whatever you want
}

: , , clang ( ):

: unsigned type "unsigned long" [-Wabsolute-value]

1 abs , std::size_t abs ( -, ?), int, long long long, .

+9

size_t , . -2, -2 , .

, ssize_t sizediff = ssize_t(s1.size()) - ssize_t(s2.size()), .

0

Since it string::size()returns unsignedan integer. Any arithmetic operation on a value unsignedwill result in a value unsigned. If the size is str1smaller than the size str2and you are trying to execute str1.size()-str2.size(), it will return a value from 4,294,967,295 - (diff -1).

Note: here diffis the actual difference in length.

0
source

Source: https://habr.com/ru/post/1650628/


All Articles