MAP功能/错误输出 [英] MAP Function / Wrong Output

查看:52
本文介绍了MAP功能/错误输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用地图功能来检查http头文件中是否包含某些单词.它总是返回找到匹配项!".我的问题是,它真的是在搜索数据[i],是搜索数字还是字符串?谢谢.

I''m using a map function to check if certain words are in a http header file. It''s always returning, "Found A Match!". My question is, is it really searching data[i] and is it seraching the number or the string? Thank you.

#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]);
			c+=1;
			//////////////////////////////////////////////////////
			map<int,> mymap;
			char c;
			mymap [1] = "audio/basic";
			mymap [2] = "audio/x-aiff";
			mymap [3] = "audio/x-wav";
			mymap [4] = "video/mpeg";
			mymap [5] = "video/mpeg-2";
  			mymap [6] = "video/quicktime";
			for (c=1; c<6; c++)
			{
			cout << c;
			if (mymap.count(c)>0)
				cout << "Found A Match!\n";
			else 
				cout << "No Match Is Found.\n";
			}
			return;
			////////////////////////////////////////////////////
			//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>



这是输出.....



This is the output.....

Packet 33: at 1314898068
-----------------Packet Begins-----------------
IP Version: 4
IP Packet Size: 555bytes
IP Id: 21010
IP Fragment: 1024
IP TTL: 128
IP HL: 5wds
IP Protocol: 6
IP Source: 192.168.1.105
IP Destination: 192.168.1.102

TCP Source Port: 2869,
TCP Destination Port: 2675

TCP Sequence: 1903923820,
TCP Acknowledgment: 3115968810

TCP Hdr Size: 8,
TCP Flags:
 FIN
 PSH
 ACK
     -------------IP Data Begins-------------
H1  Found A Match!
2  Found A Match!
3  Found A Match!
4  Found A Match!
5  Found A Match!

------------------Packet Ends------------------

推荐答案

0)您的地图索引应从0开始. >
1)mymap.count(c)始终> 0,因此认为找到了匹配项.

2)您的地图应填充在最外层的for循环的外部.

使用调试器将为您发现这些问题.
0) Your map index should start at 0.

1) mymap.count(c) is always > 0, therefore it thinks a match is found.

2) Your map should be populated OUTSIDE of the outer-most for loop.

Using the debugger would have found these problems for you.


对于您在解决方案1中有关#2的问题

For you question about #2 in Solution 1

// The follwing code does not change inside for (int i = ...) loop
// So it should be before the loop so it will be executed only.
map<int, string> mymap;
char c;
mymap [1] = "audio/basic";
mymap [2] = "audio/x-aiff";
mymap [3] = "audio/x-wav";
mymap [4] = "video/mpeg";
mymap [5] = "video/mpeg-2";
mymap [6] = "video/quicktime";

for (int i = 0; i < length; ++i)
{
   // Other code here...
}


1)您应该在循环中声明循环变量:

1) You should declare your loop variable inside your loop:

for int i = 0; i < length; ++i)
{
    /* more code here */
}



2)您不应在同一函数中声明两个具有相同名称的变量.它使代码难以理解.您的函数顶部附近有一个变量int c,用于不确定的用途(很难理解要对该变量执行的操作)

如(1)所述,您的内部c变量也应在其自己的循环中声明.

3)正如约翰在解决方案1中所提到的,由于指定键始终存在一个匹配项,因此mymap.count(c)总是返回大于0的值.使用功能之前,您是否已阅读该功能的文档?您有6个键(1、2、3、4、5和6),由于c从1循环到6(不包括在内),因此循环计数整个数组中的每个键.顺便说一句,您的输出中有5行.上一项被跳过.

4)内循环的目的是什么?

5)如果您的问题建议您这样做,那么您真的想检查data[i]是否是地图中的键.您可以使用mymap.count(data[i]).顺便说一句,count(c)计算密钥(因此您的情况下为数字).

6)如果您不知道函数的功能(如在文档中明确指出的那样),那么阅读该函数的文档也不会造成任何伤害. cplusplus.com和MSDN甚至都在文档中提供了示例.因此,没有任何错误的借口!

http://www.cplusplus.com/reference/stl/map/count/ [ ^ ]

http://msdn.microsoft.com/en-us/library/cw6z9ae9 (v = VS.100).aspx [



2) You should not declare two variables with the same name in the same function. It make the code hard to understand. You have a variable int c near the top of your function for an undetermined purpose (It is hard to understand what you want to do with that variable)

As mentionned in (1), your inner c variable should also be declared in its own loop.

3) As mentionned by John in solution 1, mymap.count(c) will always returns a value greater than 0, since there is always one match for the specified key. Have you read the documentation of the function before using it? You have 6 keys (1, 2, 3, 4, 5 and 6) and your loop count each of those key in the whole array since c loops from 1 to 6 (not included). By the way, you have 5 lines in your output. Last item is skipped.

4) What is the purpose of the inner loop?

5) If as your question suggest it, you really want to check if data[i] is a key in your map. you could uses mymap.count(data[i]). By the way, count(c) count the key (thus the number in your case).

6) It won''t hurt if you would read the documentation of a function when you don''t know what the function does as it is clearly indicated in the documentation. Both cplusplus.com and MSDN even provide example in the documentation. So there is no excuse to get it wrong!

http://www.cplusplus.com/reference/stl/map/count/[^]

http://msdn.microsoft.com/en-us/library/cw6z9ae9(v=VS.100).aspx[^]


这篇关于MAP功能/错误输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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