Visual studio 2013 C++ standard library

Visual studio 2013 C++ standard library

本文关键字:standard library C++ 2013 studio Visual      更新时间:2023-10-16

我使用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 min/max可以帮助。然而,当我尝试编译时,我得到错误,告诉我命名空间"std"没有成员"min"和成员"max"。

我做错了什么,还是Visual Studio 2013不包括min/max?

我看你好像忘了#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;
}

添加

#include <algorithm>
使用前

std::min std::max