const std :: map< boost :: tuples :: tuple,std :: string> ;? [英] const std::map<boost::tuples::tuple, std::string>?
问题描述
// 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 - 可以使用init const std :: container进行测试等
一个const std :: map或std :: map很容易,如上面的代码所示。创建 const map< string,std :: vector< int>>
有点复杂,但仍然相当容易。
Using boost - it's possible to init const std::containers for testing etc.
making a const std::map or std::map is pretty easy as the above code shows. Creating a const map<string, std::vector<int>>
is a bit more complex - but still fairly easy.
我试图想出一个 const std :: map& boost :: tuples :: tuple< string,string,string> ;, string>
但我没有初始化它。有没有其他人有幸运吗?
I'm trying to come up with a const std::map<boost::tuples::tuple<string, string, string>, string>
but I'm failing to initialize it. Has anyone else had any luck with it ?
// 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.
推荐答案
该映射需要是可比的( std :: less
,因此需要定义一个运算符)。
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 an operator<
defined). boost::tuple
's comparison operators are defined in the header boost/tuple/tuple_comparison.hpp
.
包含这些代码后,此代码工作正常:
Having included that, this code works fine:
#include <boost/assign/list_of.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <map>
#include <string>
using std::string;
typedef boost::tuple<string, string, string> tpl_t;
int main() {
using boost::assign::map_list_of;
std::map<tpl_t, string> const m =
map_list_of(tpl_t("a","b","c"), "c")(tpl_t("x","y","c"), "z");
}
这篇关于const std :: map< boost :: tuples :: tuple,std :: string> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!