使用地图计数功能 [英] USING THE MAP COUNT FUNCTION

查看:79
本文介绍了使用地图计数功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MAP COUNT并存在一些问题.它必须在data []中搜索关键字,似乎无法弄清楚.并指出,这不是在==部件上进行调试,关于类型"const char *"的值不能分配给实体类型"int",最后它说返回值类型与函数类型不匹配.任何帮助都超过了赞赏!

I''m trying to use the MAP COUNT and have a fewproblems. It has to search data[] for keywords, can''t seem to figure that out. And secound it''s not debugging on the == parts, something about a value of type "const char *" cannot be assigned to an entity type of "int" and lastly it says that return value typedoes not match the function type. Any help is more than appreciated!

#include <stdio.h>
#include <iostream>
#include <map>
using namespace std;
void printRawData(unsigned char *data, int length, int more)
{
	int i, c=0;
	printf("     -------------IP Data Begins-------------\n");
	for (i=0; i<length;>	{
		if ((data[i]>30 && data[i]<122) || 
			(((data[i]==10) || (data[i]==13) || (data[i]==123) || (data[i]==125))
            && (more>0)))
		{
			printf("%c", data[i]);
			//////////////////////////////////////////////////////
			map<char,int> mymap;
			char c;
			mymap [1] = "audio/basic";
			mymap [2] = "audio/x-aiff";
			mymap [3] = "audio/x-wav";
	
			for (c='1'; c<'3'; c++)
			{
			cout << c;
			if (mymap.count(c)>0)
				cout << " is an element of mymap.\n";
			else 
				cout << " is not an element of mymap.\n";
			}
			return 0;
			////////////////////////////////////////////////////
			c+=1;
        }
		else
		{
			//printf("[%i]", data[i]);
			c+=3;
			if (data[i]>9) c++;
			if (data[i]>99) c++;
                }
		if (c>=47)
		{
			printf("\n");
			c=0;
                }
       }
}</iostream></stdio.h>

推荐答案


我的错误,在初读时就想念您正在使用

My mistake, on first reading I missed that you are using
map<char,int> mymap;

并尝试执行类似

mymap [1] = "audio/basic";


正如Stefan所说,您可以更改键类型,还必须更改值类型.


对于返回值类型与功能类型不匹配"的更改


You may change key type, as Stefan says, and you also must change value type.


For "return value type does not match the function type" change

return 0;




to

return;


[edit]以下内容是对解决方案1 ​​[/edit]
您输入键(char)(1)(char)(2)(char)(3)的值,但是在for循环中,您查询键''1''''2''''3''的值完全不同! >
将输入值的键切换到''1''''2''''3'',或者将for循环从13而不是从''1''''3''.

提示尝试像这样打印"1"的实际数值:
[edit]The following was meant as an addition to Solution 1[/edit]
You enter values for the keys (char)(1), (char)(2) and (char)(3), but in the for loop you query values for the keys ''1'', ''2'' and ''3'' which are different altogether!

Either switch the keys for entering your values to ''1'', ''2'' and ''3'' or switch your for loop to run from 1 through 3 rather than ''1'' through ''3''.

Hint try printing the actual numerical value of ''1'' like this:
cout << (int)'1' << endl;


您可能会感到惊讶...


You might be surprised...


您在这里遇到了几个问题.在映射中,模板参数是[key,value]类型,因此您有一个包含char键和int值的映射.

由于char是一个iteger类型,因此使用数字常量作为键是完全合法的,就像您在这里所做的那样:mymap [1].这是指map . char键为数字1的元素(与字符"1"不同).

碰巧的是,指针也是整数类型,文字文本字符串(例如音频/基本")实际上是const char*.

因此,在mymap [1] = "audio/basic";行中,有一个map元素,其中char键是数字1,而int值是字符串常量的地址.

我怀疑您想要的是带有int键和文本字符串值的映射,如下所示:

You have a couple of problems here. In a map, the template parameters are the [key, value] types, so you have a map with a char key and int value.

Since char is an iteger type, it''s perfectly legal to use numeric constants as keys, like you do here: mymap [1]. This refers to a map<char, int> element with the char key being the number 1 (which is not the same thing as the character ''1'').

As it happens, pointers are also integer types, and literal text strings (eg "audio/basic" for instance) are actually const char*.

So in the line mymap [1] = "audio/basic"; you have a map element where the char key is number 1, and the int value is the address of the string constant.

I suspect that what you wanted was a map with an int key and a text string value, like this:

map<int, string> mymap;
mymap [1] = "audio/basic"; // int key 1, string value "audio/basic"



正如其他人指出的那样,for循环使用字符(c =''1''; c<''3'';)而不是数值(c = 1; c< 3;),这会使事情搞砸,从1 != ''1''开始.

因此,使用上面更改的mymap,循环应如下所示:



As others have pointed out, the for loop is using characters (c=''1''; c<''3'';) rather than numerical values (c = 1; c< 3;), which will mess things up, since 1 != ''1''.

So with the changed mymap from above, the loop should be like this:

for (int c = 1; c< 3; c++)
{
  cout << c;
  if (mymap.count(c)>0)
...

.


这篇关于使用地图计数功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆