如果数组已经在映射中,则如何将数组数据存储为地图密钥或增量频率?

发布于 2025-02-13 00:52:21 字数 698 浏览 0 评论 0原文

我的代码:

map<array<byte, aes>, int> possible;
array<byte,aes> temp;

if (!possible.count(temp)) // if not found
    possible.insert(pair<array<byte,aes>,int>(temp,1)); // insert
else
    possible[temp]++; // if found increment frequency

我需要将数组与其数据一起用作地图密钥。整数表示阵列发生的频率。但是,新数组仅是第一次写的,而其他时间则是代码认为它是相同的数组。据我了解,该程序采用数组的地址,而不是其数据。如何将带有数据的数组传递到地图而不是数组的地址? 我知道在QT中,此代码可以正常工作:

QMap<QByteArray,int> possible;
QByteArray temp;
if(!possible.count(temp)) // if not found
    possible.insert(temp, 1); // insert
else
    possible[temp]++; // if found increment frequency

STD :: Array and STD :: Map的替代方法是什么?

My code:

map<array<byte, aes>, int> possible;
array<byte,aes> temp;

if (!possible.count(temp)) // if not found
    possible.insert(pair<array<byte,aes>,int>(temp,1)); // insert
else
    possible[temp]++; // if found increment frequency

I need to use an array with its data as a map key. An integer indicates the frequency of occurrence of the array. However, the new array is only written the first time, the other times the code considers it to be the same array. As I understand it, the program takes the address of the array, not its data. How to pass an array with data to the map instead of the address of the array?
I know that in Qt this code works fine:

QMap<QByteArray,int> possible;
QByteArray temp;
if(!possible.count(temp)) // if not found
    possible.insert(temp, 1); // insert
else
    possible[temp]++; // if found increment frequency

What is alternative for std::array and std::map to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2025-02-20 00:52:21

在C ++中,

possible[temp]++;

如果存在TEMP,则该代码将正常工作,那么它的频率将会增加。如果不存在,则将以0的频率插入,然后将其递增。这为您提供了与代码完全相同的结果。

查看QMAP的文档,似乎上述代码也可以在QT上使用。

有时(不经常)事情比您想象的要容易。

In C++ this code will work fine

possible[temp]++;

If temp exists then it's frequency will be incremented. If it does not exist then it will be inserted with a frequency of 0, which will then be incremented. This gives you exactly the same result as your code.

Looking at the documentation for QMap, it seems the above code would also work on Qt.

Sometimes (not often) things are easier than you imagined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文