[英]Char 2D array in map in C++
How can I use a 2D char array with a map in C++. 如何在C ++中将2D char数组与地图一起使用。 I want to do this: 我想做这个:
map<char[50][50],int>M;
char brr[50][50];
//some operation here on the array
int aa=1;
if(M.find(brr)==M.end())
{
M[brr]=aa;
aa+=1;
}
what am I doing wrong? 我究竟做错了什么?
EDIT: 编辑:
I've just found another way. 我刚刚找到了另一种方法。 This way I can achieve what I stated in my question. 这样我就可以实现我在问题中所说的。 Instead of using the 2d array I'm just gonna convert it into a string and use it. 与其使用2d数组,不如将其转换为字符串并使用它。 It'll still yield the same result: 仍然会产生相同的结果:
map<string,int>M;
char brr[50][50];
//some operation here on the array
int aa=1,i,j;
string ss="";
for(i=0;i<50;i++)
{
for(j=0;j<50;j++)
{
ss+=brr[i][j];
}
}
if(M.find(ss)==M.end())
{
M[ss]=aa;
aa+=1;
}
You can't. 你不能 Arrays can't be assigned to (ie you can't do brr = XXX;
in your example), and this is a requirement of the key type of a std::map
. 无法将数组分配给您(例如,您不能在示例中使用brr = XXX;
),这是std::map
的键类型的要求。 Also, the key needs to have a strict weak ordering defined on it (ie it needs operator<
or a comparator function). 同样,密钥需要在其上定义严格的弱排序 (即,它需要operator<
或比较器功能)。
You could consider wrapping your array in a class, defining an appropriate operator <
, and then using this as the key type. 您可以考虑将数组包装在一个类中,定义一个适当的operator <
,然后将其用作键类型。
You have to use a wrapper class, and it needs to support operator<
. 您必须使用包装器类,并且它需要支持operator<
。 If a lexographical ordering is fine, you can do something like this: 如果按字母顺序排序很好,则可以执行以下操作:
#include <boost/array.hpp>
#include <map>
int main()
{
typedef boost::array<boost::array<char, 50>, 50> Array;
std::map<Array, int> m;
}
boost::array
can be replaced with std::array
if you are using C++11. 如果您使用的是C ++ 11,则可以用std::array
替换boost::array
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.