constのはstd ::マップ<後押し::タプルを::タプル、のstd :: string>に?

StackOverflow https://stackoverflow.com/questions/1094865

  •  11-09-2019
  •  | 
  •  

質問

// 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));

ブーストを使用する - だ可能性などをテストするためのconstのstd ::コンテナを初期化します constのはstd ::マップまたはSTD ::マップを作ることは、上記のコードが示すように非常に簡単です。 const map<string, std::vector<int>>を作成すると、少し複雑である - 。それでもかなり簡単

私はconst std::map<boost::tuples::tuple<string, string, string>, string>を思い付くしようとしているが、私はそれを初期化するために失敗しています。誰がそれを任意の運があったか?

// 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と、このようoperator<定義が必要です)。 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");
}

他のヒント

私がしようとするだろう。

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つの別々の文字列のリスト、および1対1組にそれらを組み合わせて、マップにそれを追加を持っている方が簡単です。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top