如果数组已经在映射中,则如何将数组数据存储为地图密钥或增量频率?
我的代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在C ++中,
如果存在
TEMP
,则该代码将正常工作,那么它的频率将会增加。如果不存在,则将以0的频率插入,然后将其递增。这为您提供了与代码完全相同的结果。查看QMAP的文档,似乎上述代码也可以在QT上使用。
有时(不经常)事情比您想象的要容易。
In C++ this code will work fine
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.