gpt4 book ai didi

c++ - 将用户值(value)与枚举中的值(value)进行比较

转载 作者:行者123 更新时间:2023-12-02 09:53:53 26 4
gpt4 key购买 nike

任务的实质是:根据用户的数据为用户选择最佳的屏幕分辨率。
我需要用ENUM中的值来表示用户值,然后返回相应的值。
例如:

enum class Resolutions {
V720_height = 720,
V720_width = 1280,
V1080_height = 1080,
V1080_width = 1920
};

int main() {
int user_height = 1200;
int user_width = 430;
// I know this does not work, just an example.
std::for_each(Resolutions.begin(), Resolutions.end(), [](Resolutions tmp) {
if (static_cast<int>(tmp) > user_height) {
std::cout << tmp << " - is better resolution\n";
}
});
}

我需要一个好主意,如何实现呢?

最佳答案

使用枚举不是做到这一点的最佳方法。如果需要命名不同的分辨率,我建议您使用std::map。

#include <map>
#include <string>
#include <iostream>

struct Resolution {
int height;
int width;
};

const std::map<std::string, Resolution> resolutions = {
{ "V720", {720, 1280} },
{ "V1080", {1080, 1920} }
};

int main() {
int user_height = 1200;
int user_width = 430;

for (auto& [key, value] : resolutions) {
if (value.height > user_height &&
value.width > user_width) {
std::cout << key << " - is better resolution\n";
}
}
}

关于c++ - 将用户值(value)与枚举中的值(value)进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62009633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com