C++ – 如何检查无符号整数数组是否为空或非空

huangapple go评论78阅读模式
英文:

C++ - How to check if array of unsigned int is empty or not

问题

I have this:

std::uint32_t data[6];
std::map<std::string, std::variant<int, float, std::string, std::uint32_t>> map_;

map_["data0"] = data[0] ? data[0] : 0;
map_["data1"] = data[1] ? data[1] : 0;
map_["data2"] = data[2] ? data[2] : 0;
map_["data3"] = data[3] ? data[3] : 0;
map_["data4"] = data[4] ? data[4] : 0;
map_["data5"] = data[5] ? data[5] : 0;

The idea of map_["data0"] = data[0] ? data[0] : 0; is to see if there is any value already set in data[0]if not to set 0.

However, this does not work properly. Even if data[0] is not set, it does not return '0'.

Any idea how can I check the array at a specific index if the std::uint32_t is "initialized" or set?

英文:

I have this:

std::uint32_t data[6];
std::map&lt;std::string, std::variant&lt;int, float, std::string, std::uint32_t&gt; map_;

map_[&quot;data0&quot;] = data[0] ? data[0] : 0;
map_[&quot;data1&quot;] = data[1] ? data[1] : 0;
map_[&quot;data2&quot;] = data[2] ? data[2] : 0;
map_[&quot;data3&quot;] = data[3] ? data[3] : 0;
map_[&quot;data4&quot;] = data[4] ? data[4] : 0;
map_[&quot;data5&quot;] = data[5] ? data[5] : 0;

The idea of map_[&quot;data0&quot;] = data[0] ? data[0] : 0; is to see if there is any value already set in data[0]if not to set 0.

However this does not work properly. Even if data[0] is not set it does not return '0'.

Any idea how can I check the array at specific index if the std::uint32_t is "initilized" or set ?

答案1

得分: 8

不可能检查整数类型是否已初始化。整数不是指针,也不是指针样式,不可为空。未初始化的整数具有不确定的值,无法检查,并且与空的概念不同。

您需要自行追踪每个元素是否具有值。

可能的解决方案包括:

  • 使用特殊值。例如,您可以决定最大可表示的值表示“无值”并将所有元素初始化为该值。

  • 使用标志。您可以创建另一个bool数组或std::bitset以跟踪哪个元素具有值。

  • 使用可以编码更多信息的不同类型,例如std::optional<std::uint32_t> data[6];

英文:

It is not possible to check if an integer type is initialized or not. An integer is not a pointer, and it is not pointer-like, it is not nullable. An uninitialized integer has an indeterminate value which cannot be inspected and which is distinct from the concept of null.

You need to track whether or not each element has a value yourself somehow.

Possible solutions include :

  • Using a sentinel value. For example, you could decide that the
    maximum representable value means "no value" and initialized all
    elements to that value.

  • Using flags. You could create another array of bool or a
    std::bitset to track which element has a value.

  • Using a different type which can encode more information, such as
    std::optional&lt;std::uint32_t&gt; data[6];.

答案2

得分: 0

请看以下翻译:

有没有办法检查特定索引处的数组,以确定 std::uint32_t 是否已经 "初始化" 或设置?

您的代码存在未定义行为(未初始化的值)。这会使代码无效,您无法从中恢复。因此,与其在代码中处理这个问题,不如配置编译器以检测所有警告并将它们视为错误(-Wall -Wextra -Werror -pedantic):

链接

还有其他工具可以检测C++代码中的常见错误:地址检测器,未定义行为检测器(还有更多检测器)(-fsanitize=address,undefined)。还有一些静态分析工具:cpplint、clang-analyzer等等。

您为项目设置的工具越多,就能更快地自动发现代码中的错误。

英文:

> Any idea how can I check the array at specific index if the std::uint32_t is "initilized" or set ?

Your code has undefined behavior (uninitialized value). This makes code invalid and you can't recover form this. So instead handling this in code, configure your compiler to detect all warnings and threat them as errors (-Wall -Wextra -Werror -pedantic):

https://godbolt.org/z/Mox4co7df

There are other tools which can detect common errors in C++ code: Address Sanitizer, Undefined Behavior Sanitizer (there are more sanitizers) (-fsanitize=address,undefined). There are also some static analysis tools: cpplint, clang-analyzer, ... .

The more tools you will setup for your project then better. You will find mistakes done in code faster and automatically.

huangapple
  • 本文由 发表于 2023年5月10日 22:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定