문제

// 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 :: map 또는 std :: 맵을 만드는 것은 위의 코드가 보여지는 것처럼 매우 쉽습니다. 생성 a const map<string, std::vector<int>> 조금 더 복잡하지만 여전히 상당히 쉽습니다.

나는 a를 생각해 내려고 노력하고있다 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 개의 별도의 문자열 목록을 갖는 것이 더 쉬운 다음, 한 번만 튜플에 결합하여지도에 추가합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top