【问题标题】:const std::map<boost::tuples::tuple, std::string>?常量 std::map<boost::tuples::tuple, std::string>?
【发布时间】:2009-07-07 21:05:24
【问题描述】:
// 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&lt;string, std::vector&lt;int&gt;&gt; 有点复杂 - 但仍然相当容易。

我正在尝试创建一个const std::map&lt;boost::tuples::tuple&lt;string, string, string&gt;, string&gt;,但我无法对其进行初始化。其他人有运气吗?

// 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.

【问题讨论】:

  • 请始终包含编译错误(如果有)导致的错误消息

标签: c++ map boost-tuples


【解决方案1】:

我试过了,但它失败了,因为地图的键需要可比较(使用std::less,因此需要定义operator&lt;)。 boost::tuple 的比较运算符在标题 boost/tuple/tuple_comparison.hpp 中定义。

包含这些后,此代码可以正常工作:

#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");
}

【讨论】:

    【解决方案2】:

    我会试试的

    const map<x3_string_tuple, string> query_selector_map = map_list_of<x3_string_tuple, string>
    (x3_string_tuple("4556", "SELECT", "FILENAME"), "4556_SELECT_FILENAME");
    

    但是,老实说,拥有 3 个单独的字符串列表,然后将它们一个接一个地组合成一个元组并将其添加到映射中,可能会更容易。

    【讨论】:

    • 这是我最初的想法,但它导致了大量的编译错误。来吧 - 看看会发生什么。
    • 我正在考虑 3 个列表变体,但我没有看到如何使用 boost:assign 在 1 中初始化它,以便它们一起工作:/
    猜你喜欢
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多