C++ 使用 std::vector 或 std::map 制作一个禁止列表
C++ Make a banlist with std::vector or std::map
我正在尝试用std::vector/std::map编写一个小的禁止列表。但我还不知道它应该如何工作...
以下是"BanList"在Networking.h上的构建方式:
static std::vector<int, std::string>BanList;
- Int 代表 ID
- 目标 IP 的字符串
这是我的网络片段.cpp(目标被添加到禁止列表中)
if (boost::contains(dataPackage.data, needle1) && boost::contains(dataPackage.data, needle2))
{
// All okay here - Let's jump over & let the thread handle the action
}
else
{
//e.g. BanList.addTarget(Auto-Incremented ID, TargetsIP);
break;
}
所以在//的行上,例如 BanList.addTarget(int, string); 它应该如何与 std::vector 或 std::map 一起工作?现在如何创建包含目标的列表?获得IP不是我的问题!问题是如何自动设置 ID 并将目标添加到列表中......现在已经感谢您的帮助。
我不太确定你的问题是什么。如果您想知道如何使用地图,那么您应该看到在线参考。
在特定情况下,如果您使用地图:
static std::map<int, std::string> banList;
banList[id] = ipAddress;
我不知道你为什么要将整数映射到禁止列表的字符串。但这就是你的做法。
对于向量,除非推送std::pair
对象,否则不能有键/值对。为此,您几乎总是希望使用地图。
要添加到矢量,请使用 vec.push_back(item)
。
您几乎可以在在线参考中找到所有这些。
小心地读取std::vector
的模板参数。 std::string
不是int
:)的有效分配器
这将更接近std::map<int, std::string>
:
std::vector<std::pair<int, std::string>> BanList;
从参考/您最喜欢的书中了解有关std::vector
/std::pair
或std::map
的其余信息。这里不值得解释(而且没有足够的空间)。
如果TargetsIP
类似于 std::vector<std::string>
,则需要遍历它并将元素附加到循环中的BanList
。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 捕获/禁止发送到 std::cout 的 OpenCV 警告
- 语言的哪一部分禁止更改 std::set 的元素
- C++标准是否明确允许/禁止实例化不完整类型的 std::函数?
- 为什么std::变体中禁止引用
- ISO C++禁止声明 .没有设置 --std=c++0x 的类型
- 当std::string是参数时,如何禁止在参数列表中传递NULL
- 使用 std::sort 时禁止使用函子(inherrited)
- C++ 使用 std::vector 或 std::map 制作一个禁止列表
- std::unique_lock<std::mutex> 禁止 DLL 卸载