const std::map?
// BOOST Includes
#include <boost/assign.hpp> // Boost::Assign
#include <boost/assign/list_of.hpp> // Boost::Assign::List_Of
#include <boost/assign/std/map.hpp> // Boost::Assign::Map_List_Of
#include <boost/tuple/tuple.hpp> // Boost::Tuples
// STD Includes
#include <map>
#include <vector>
#include <string>
// Using namespaces
using namespace std;
using namespace boost;
using namespace boost::assign;
// Consts
const map<string, string> query_map = map_list_of<string, string>
("4556_SELECT_FILENAME", "SELECT FILENAME FROM Files WHERE PMU_ID = 4556")
("7552_SELECT_FILENAME", "SELECT FILENAME FROM Files WHERE PMU_ID = 7552")
("234x_SELECT_FILENAME", "SELECT FILENAME FROM Files WHERE PMU_ID = 2344 OR PMU_ID = 2345 OR PMU_ID = 2346 OR PMU_ID = 2347 OR PMU_ID = 2348")
("813x_SELECT_FILENAME", "SELECT FILENAME FROM Files WHERE PMU_ID = 8132 OR PMU_ID = 8133 OR PMU_ID = 8134 OR PMU_ID = 8135 OR PMU_ID = 8136");
const map<string, std::vector<int>> vector_map = map_list_of<string, std::vector<int>>
("4556", list_of(4556))
("7552", list_of(7552))
("234x", list_of(2344)(2345)(2346)(2347)(2348))
("813x", list_of(8132)(8133)(8134)(8135)(8136));
使用 boost - 可以初始化 const std::containers 进行测试等。
如上面的代码所示,制作 const std::map 或 std::map 非常简单。 创建 const map
有点复杂 - 但仍然相当简单。
我试图想出一个 const std::map
// Typedefs
typedef boost::tuples::tuple<string, string, string> x3_string_tuple;
// Constants
const map<x3_string_tuple, string> query_selector_map = map_list_of<x3_string_tuple, string>
("4556", "SELECT", "FILENAME"), "4556_SELECT_FILENAME"); // ETC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试了这个,但失败了,因为映射的键需要具有可比性(使用
std::less
,因此需要定义一个operator<
)。boost::tuple
的比较运算符在头文件boost/tuple/tuple_comparison.hpp
中定义。包含该内容后,这段代码可以正常工作:
I tried this, and it fails because the keys of the map need to be comparable (with
std::less
, thus there needs to be anoperator<
defined).boost::tuple
's comparison operators are defined in the headerboost/tuple/tuple_comparison.hpp
.Having included that, this code works fine:
我会尝试
但是,老实说,也许只拥有 3 个单独的字符串列表,然后将它们一一组合成一个元组并将其添加到地图中会更容易。
I would try
But, honestly, maybe it's easier just to have 3 separate lists of strings, and then one-by-one combine them into a tuple and add that to a map.