在C++中使用一个而不是另一个有什么优缺点?
我尝试着
void function(int y,int w)
{
printf("int function");
}
void function(float y,float w)
{
printf("float function");
}
int main()
{
function(1.2,2.2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到一个错误错误,如..
error C2668: 'function' : ambiguous call to overloaded function
Run Code Online (Sandbox Code Playgroud)
当我尝试调用function(1.2,2)或function(1,2.2)打印为" int函数 "时
请说清楚什么时候function(float y,float w)打电话?
在下面的代码片段中,auto将变量推导出为double,但是我想要float。
auto one = 3.5;
Run Code Online (Sandbox Code Playgroud)
它是否始终double用于带小数点的文字?如何确定浮点数和两倍数?
当我给sizeof(a),其中a=13.33,浮子变量时,大小为4个字节.但如果我sizeof(13.33)直接给出,大小是8字节.
我不明白发生了什么.有人可以帮忙吗?
我看到很多C++代码都有如下行:
float a = 2;
float x = a + 1.0f;
float b = 3.0f * a;
float c = 2.0f * (1.0f - a);
Run Code Online (Sandbox Code Playgroud)
这些是.0f这些文字后,真的有必要吗?如果省略这些,你会失去数字准确性吗?
如果你有这样一条线,我认为你只需要它们:
float a = 10;
float x = 1 / a;
Run Code Online (Sandbox Code Playgroud)
你应该在哪里使用1.0f,对吗?
c++ floating-point performance floating-accuracy floating-point-precision
我有一个这样的程序:
#include <iostream>
using namespace std;
int main ()
{
double x;
for (int i = 1; i < 100; i++)
{
cout << (x = i/100) << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在运行它时,我只得到:
0
0
0
[...]
Run Code Online (Sandbox Code Playgroud)
我在Cygwin-g ++和MinGW-g ++上测试了这个; 版本4.7.2.哪里可能是问题?
#include <iostream>
using namespace std;
int square(int x);
float square(float x);
int main() {
cout<<square(3);
cout<<square(3.14);
return 0;
}
int square(int x) {
cout<<"\nINT version called\n";
return x*x;
}
float square(float x) {
cout<<"\nFLOAT version called\n";
return x*x;
}
Run Code Online (Sandbox Code Playgroud)
我试图用double替换函数的float版本,然后它开始工作.这里有什么问题?不能将3.14视为浮动?
错误:调用重载'square(double)'是模棱两可的
注意事项:候选者是:
注意:int square(int)
注意:float square(float)
我想使用此代码(源代码)在opencv中查找基本矩阵。
\n\nint point_count = 100;\n vector<Point2f> points1(point_count);\n vector<Point2f> points2(point_count);\n\n // initialize the points here ... */\n for( int i = 0; i < point_count; i++ )\n {\n points1[i] = ...;\n points2[i] = ...;\n }\n\n Mat fundamental_matrix =\n findFundamentalMat(points1, points2, FM_RANSAC, 3, 0.99);\nRun Code Online (Sandbox Code Playgroud)\n\n但我不知道如何在for循环中初始化points1、point2。\n我尝试过使用:
\n\npoints1[i] = 10.0;\npoints1[i] = (10.0, 20.0);\npoints1[i] = {10.0, 20.0};\nRun Code Online (Sandbox Code Playgroud)\n\n但出现这样的错误
\n\nno match for \xe2\x80\x98operator=\xe2\x80\x99 (operand types are \xe2\x80\x98cv::Point_<float>\xe2\x80\x99 and \xe2\x80\x98double\xe2\x80\x99)\nRun Code Online (Sandbox Code Playgroud)\n\n在所有这些中。
\n让我们看看以下函数重载声明
void funA(int);
void funA(float);
Run Code Online (Sandbox Code Playgroud)
然后我们调用这样的函数:
funA(1); // this will be ok.
Run Code Online (Sandbox Code Playgroud)
然而,
funA(1.333) // this will not ok..
Run Code Online (Sandbox Code Playgroud)
对于编译器,funA(int)和funA(float)将是不明确的..编译器将1.333值转换为整数(1)..虽然假设它是一个浮点值会更合适..
我正在使用g ++(GCC)4.8.2为什么编译器不会调用funA(float)呢?但是以下工作..
funA(static_cast<float>(1.333))
Run Code Online (Sandbox Code Playgroud) 为什么sizeof(5.0)我的机器为float数据类型分配了4个字节,因此返回8作为输出而不是4 ?默认情况下,是否考虑5.0为double(内存分配= 8个字节),如果是的话,为什么会这样呢?
c++ ×9
c ×2
overloading ×2
sizeof ×2
auto ×1
literals ×1
opencv ×1
performance ×1
types ×1
visual-c++ ×1