我正在使用Visual Studio 2013在C ++中编写以下代码:
#include <iostream>
using namespace std;
int main()
{
std::cout << "Please enter two integers: " << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
int current = std::min(v1, v2);
int max = std::max(v1, v2);
while (current <= max)
{
std::cout << current << std::endl;
++current;
}
return 0;
}
该代码旨在解决:“编写一个程序,提示用户输入两个整数。在这两个整数指定的范围内打印每个数字。”
起初我很困惑,但是发现搜索后std的最小值/最大值可以提供帮助。但是,尝试编译时出现错误,告诉我名称空间“ std”没有成员“ min”,也没有成员“ max”。
我做错了什么,还是Visual Studio 2013不包含最小/最大?
在我看来,就像您忘记了#include <algorithm>
。
您的代码应如下所示:
#include <iostream>
#include <algorithm> // notice this
using namespace std;
int main()
{
std::cout << "Please enter two integers: " << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
int current = std::min(v1, v2);
int max = std::max(v1, v2);
while (current <= max)
{
std::cout << current << std::endl;
++current;
}
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句