C++ C++;使用std::vector或std::map创建一个banlist
我正试图用std::vector/std::map编写一个小的banlist。但我还不知道该怎么做 以下是“BanList”是如何建立在网络基础上的。h:C++ C++;使用std::vector或std::map创建一个banlist,c++,list,dictionary,vector,std,C++,List,Dictionary,Vector,Std,我正试图用std::vector/std::map编写一个小的banlist。但我还不知道该怎么做 以下是“BanList”是如何建立在网络基础上的。h: static std::vector<int, std::string>BanList; 所以在where的行中//例如BanList.addTarget(int,string);它应该如何使用std::vector或std::map?我现在如何创建一个充满目标的列表?获得IP不是我的问题!问题是如何自动设置ID,然后将目标添加
static std::vector<int, std::string>BanList;
所以在where的行中//例如BanList.addTarget(int,string);它应该如何使用std::vector或std::map?我现在如何创建一个充满目标的列表?获得IP不是我的问题!问题是如何自动设置ID,然后将目标添加到列表中。。。现在已经感谢您的帮助。请仔细阅读的模板参数std::string
不是int
:)的有效分配器
这将更接近std::map
:
std::向量BanList;
从参考/您最喜欢的书中了解关于std::vector
/std::pair
或std::map
的其余内容。这里不值得解释(而且没有足够的空间)
如果
TargetsIP
类似于std::vector
,则需要遍历它并在循环中将元素附加到BanList
。我不太确定您的问题是什么。如果您想知道如何使用地图,那么您应该查看
在您的特定情况下,如果您使用地图:
static std::map<int, std::string> banList;
banList[id] = ipAddress;
static std::map banList;
banList[id]=ipAddress;
我不知道为什么要将int映射到禁止列表的字符串。但你就是这样做的
对于向量,除非推送std::pair
对象,否则不能有键/值对。不过,你几乎总是想使用地图
要添加到向量,请使用vec.push_back(项目)
您可以在网上参考资料中找到几乎所有这些内容。谢谢!:-)这对我有帮助^-^
std::vector<std::pair<int, std::string>> BanList;
static std::map<int, std::string> banList;
banList[id] = ipAddress;