gpt4 book ai didi

范围的 C++ 映射

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:46 24 4
gpt4 key购买 nike

我正在做一个 C++ 项目,我是这门语言的新手。

我正在执行一个函数,该函数必须执行从十六进制数字 0x00 到 0xFF 的范围内的操作,有人告诉我可以使用映射来完成。

问题是,到目前为止,在我看到的示例中,我只看到它只用于一个条目,而我需要根据特定范围做一些事情。

有什么方法可以在 map 中做到这一点,还是我需要使用其他东西来完成所需的功能?

最佳答案

如果理解正确,您将一些值存储在映射中,键值从 0x00 到 0xFF?可能的解决方案可能是:

typedef std::map<unsigned char, int> values;
void print( values::const_iterator begin, values::const_iterator end );

values v;
// print range [0x01,0x20]
print( v.lower_bound( 0x01 ), v.upper_bound( 0x20 ) );
// print range [0x10,0x40[
print( v.lower_bound( 0x10 ), v.lower_bound( 0x40 ) );
// print range ]0x20,0x50[
print( v.upper_bound( 0x20 ), v.lower_bound( 0x50 ) );

这是你需要的吗?

关于范围的 C++ 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15045177/

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