我正在尝试检查两个字符是否在较大字符串中为邻居,但无法检查其索引差的绝对值。下面是我的代码:
string state_string = "012345";
string goal_state_string = "125430";
for (int i = 0; i < state_string.length() - 1; i++)
{
cout << abs(goal_state_string.find(state_string[i]) - goal_state_string.find(state_string[i + 1])) << endl;
}
运行时,我得到以下输出:
$ make test
g++ -std=c++11 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:107:106: error: call of overloaded ‘abs(std::basic_string<char>::size_type)’ is ambiguous
cout << abs(goal_state_string.find(state_string[i]) - goal_state_string.find(state_string[i + 1])) << endl;
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib:75:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ext/string_conversions.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6349,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:52,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from test.cpp:1:
/usr/include/stdlib.h:70:5: note: candidate: int abs(int)
int abs (int);
^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib:77:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ext/string_conversions.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6349,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:52,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from test.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/std_abs.h:56:3: note: candidate: long int std::abs(long int)
abs(long __i) { return __builtin_labs(__i); }
^~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/std_abs.h:61:3: note: candidate: long long int std::abs(long long int)
abs(long long __x) { return __builtin_llabs (__x); }
^~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/std_abs.h:70:3: note: candidate: constexpr double std::abs(double)
abs(double __x)
^~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/std_abs.h:74:3: note: candidate: constexpr float std::abs(float)
abs(float __x)
^~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/std_abs.h:78:3: note: candidate: constexpr long double std::abs(long double)
abs(long double __x)
^~~
make: *** [Makefile:37: test] Error 1
hp user@DESKTOP-PGC8V9D /cygdrive/e/Google Drive/Phd_git/psvn/core/lessons/A-star
$ make test
g++ -std=c++11 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:13:106: error: call of overloaded ‘abs(std::basic_string<char>::size_type)’ is ambiguous
cout << abs(goal_state_string.find(state_string[i]) - goal_state_string.find(state_string[i + 1])) << endl;
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib:75:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ext/string_conversions.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6349,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:52,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from test.cpp:1:
/usr/include/stdlib.h:70:5: note: candidate: int abs(int)
int abs (int);
^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib:77:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ext/string_conversions.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6349,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:52,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from test.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/std_abs.h:56:3: note: candidate: long int std::abs(long int)
abs(long __i) { return __builtin_labs(__i); }
^~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/std_abs.h:61:3: note: candidate: long long int std::abs(long long int)
abs(long long __x) { return __builtin_llabs (__x); }
^~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/std_abs.h:70:3: note: candidate: constexpr double std::abs(double)
abs(double __x)
^~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/std_abs.h:74:3: note: candidate: constexpr float std::abs(float)
abs(float __x)
^~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/std_abs.h:78:3: note: candidate: constexpr long double std::abs(long double)
abs(long double __x)
^~~
make: *** [Makefile:37: test] Error 1
我的问题如下:
- 不应找到find函数,即返回某种整数类型。一个无符号的int,如果是这样,为什么不呢?有什么意义 这种替代类型似乎不适用于大多数 函数调用应该针对哪个?
- 使用这种类型/避免此问题时的最佳实践是什么?我应该将其转换为整数吗?
答案 0 :(得分:1)
abs
的功能,我们必须了解signed
和unsigned
类型之间的区别。请关注以下声明:
a = 10
b = -20
c = a + b
c
的结果取决于a
,b
,c
是带符号的还是无符号的?
令人惊讶的不是。因为计算机使用二进制补码来处理数字,所以如果2个数字用2 ^ N分隔,则它们被视为相同的数字。
不仅add
,subtract
不在乎符号,multiplication
也不在乎符号:
x*(2^N - y) = x*2^N - x*(-y) = x*(-y) mod 2^N
由于这种影响,有符号和无符号数字之间的限制被放宽了。
更多细节可以在X86体系结构的指令集中找到。实际上,X86定义了一个统一加法,可同时处理有符号和无符号加法,有符号和无符号加法之间的唯一区别是载波位(也称为上溢/下溢位)。
使用关心符号的运算符时,您最好多加注意。这就是为什么abs
不能采用无符号参数的原因。