CMap关键字重复,如何LookUp?
CMap<CString,CString&,sometype,sometype&> myvar;
myvar用于存储客户连接信息的MAP,其中String为客户IP Address,不可避免会出现重复(比如同一台主机开两个客户端连接上来,其端口自然不同),如果此时用LookUp,则找到的总是最后一条记录
请问有没有办法,将String相同的所有记录全找出来?或者最好直接能找到?
问题点数:200、回复次数:9Top
1 楼jiangsheng(蒋晟.Net[MVP])回复于 2006-03-13 15:26:24 得分 30
没有,CMap不支持这么做
用数据库来实现吧Top
2 楼laiyiling(陌生人[MVP])回复于 2006-03-13 15:28:05 得分 0
可以试试STL中的multimap,具有字典查找功能Top
3 楼laiyiling(陌生人[MVP])回复于 2006-03-13 15:32:50 得分 150
下面是一个用multimap做字典的例子
Using a Multimap as a Dictionary
The following example shows how to use a multimap as a dictionary:
// cont/mmap1.cpp
//Copy from The C++ Standard Library dyne-book 186
#include <iostream>
#include <map>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//define multimap type as string/string dictionary
typedef multimap<string,string> StrStrMMap;
//create empty dictionary
StrStrMMap dict;
//insert some elements in random order
dict.insert(make_pair("day","Tag"));
dict.insert(make_pair("strange","fremd"));
dict.insert(make_pair("car","Auto"));
dict.insert(make_pair("smart","elegant"));
dict.insert(make_pair("trait","Merkmal"));
dict.insert(make_pair("strange","seltsam"));
dict.insert(make_pair("smart","raffiniert"));
dict.insert(make_pair("smart","klug"));
dict.insert(make_pair("clever","raffiniert"));
//print all elements
StrStrMMap::iterator pos;
cout.setf (ios::left, ios::adjustfield);
cout << ' ' << setw(10) << "english "
<< "german " << endl;
cout << setfil('-') << setw(20) << ""
<< setfil(' ') << endl;
for (pos = dict.begin(); pos != dict.end(); ++pos) {
cout << ' ' << setw(10) << pos>first.c_str()
<< pos->second << endl;
}
cout << endl;
//print all values for key "smart"
string word("smart");
cout << word << ": " << endl;
for (pos = dict.lower_bound(word);
pos != dict.upper_bound(word); ++pos) {
cout << " " << pos->second << endl;
}
//print all keys for value "raffiniert"
word = ("raffiniert");
cout << word << ": " << endl;
for (pos = dict.begin(); pos != dict.end(); ++pos) {
if (pos->second == word) {
cout << " " << pos->first << endl;
}
}
}
//程序的输出结果
The program has the following output:
--------------------
car Auto
clever raffiniert
day Tag
smart elegant
smart raffiniert
smart klug
strange fremd
strange seltsam
trait Merkmal
smart:
elegant
raffiniert
klug
raffiniert:
clever
smartTop
4 楼striking(庸人自扰)回复于 2006-03-13 15:32:51 得分 5
不晓得为什么要用ip来作为关键字, 为什么不对每个客户设置一个id.
Top
5 楼alfwolf(木马煞)回复于 2006-03-13 15:37:17 得分 10
你应该把IP和端口转换成key,这样比较好
MAP的键值如果可以重复,那就不叫key了Top
6 楼snipersun(道者,万物之奥)回复于 2006-03-13 16:06:58 得分 5
就搂住的要求 Multimap 正解Top
7 楼xhzxlqt(人这一辈子)回复于 2006-03-13 17:06:40 得分 0
谢谢各位Top
8 楼xhzxlqt(人这一辈子)回复于 2006-03-13 17:13:48 得分 0
alfwolf(在地狱中仰望天堂) ( ) 信誉:96 2006-3-13 15:37:17 得分: 10
你应该把IP和端口转换成key,这样比较好
MAP的键值如果可以重复,那就不叫key了
///////////////////////////////////////////////////////////
我曾试过用SOCKET作关键字
但在交互时,比如允许服务器上的用户向某一IP发MSG,如果让其去找PORT,行是行,但是太那个了点
Top
9 楼LongLongAgoImBoy(ThereIsAMe)回复于 2006-03-13 17:15:51 得分 0
三楼已给出答案
如果楼主还是要CMap做的话,那就把每个用户的KEY变成唯一就可以了啊,这个应该很简单的修改。比如IP+端口。Top




