2014-03-06 1 views
2

std :: pair 또는 std :: map을 사용할 때 "first"또는 "second"를 사용하여 데이터에 액세스해야합니다. 그러나이 두 변수 이름은이 코드를 작성하지 않은 다른 동료들에게 실제로 저장되는 것에 대한 명확한 의미를 가지고 있지 않습니다. 따라서 "첫 번째"또는 "두 번째"에 대한 별칭을 만들면 많은 가독성이 향상됩니다. 예를 들어멤버 함수 또는 특정 클래스의 변수 (STL 컨테이너와 같은)의 별칭을 만드는 방법

, 다음 코드

static const std::map<std::string, std::pair<std::string, PFConvert>> COMM_MAP = 
{ // keyword->   (caption,     function) 
{std::string("1"), {std::string("Big5 to Utf16LE"), &FileConvert_Big5ToUtf16LE}}, 
{std::string("2"), {std::string("Utf16LE to Utf8"), &FileConvert_Utf16LEToUtf8}}, 
{std::string("3"), {std::string("Utf8 to Big5"), &FileConvert_Utf8ToBig5}} 
}; 

auto iterToExe = COMM_MAP.find(strTransType); 
iterToExe->second.second(); 

iterToExe->second.second();는 정말 나쁜 가독성을 가지고있다. 아직하지 내가 기본 ctors를 호출 할 수 있지만, 모든 ctors를 선언해야하지만 :

template<typename PFComm> 
class CCommContent : public std::pair<std::string, PFComm> 
{ 
public: 
    std::string &strCaption = std::pair<std::string, PFComm>::first; 
    PFComm &pfComm = std::pair<std::string, PFComm>::second; 
}; 

template<typename PFComm> 
class CCommPair : public std::pair<std::string, CCommContent<PFComm>> 
{ 
public: 
    std::string &strPattern = std::pair<std::string, CCommContent<PFComm>>::first; 
    CCommContent<PFComm> commContent = std::pair<std::string,CCommContent<PFComm>>::second; 
}; 

template<typename PFComm> 
class CCommMap : public std::map<std::string, CCommContent<PFComm>, std::less<std::string>, std::allocator<CCommPair<PFComm>>> 
{}; 

을 다음과 같은

그래서 나는 별칭을 제공하기 위해 상속을 사용하려고하지만 이것은 또 다른 문제에 관해서 똑똑한 방법 인 것 같습니다. 나는 별칭을 만들고 싶다.

간단한 방법은 매크로를 사용하는 것입니다. 그러나 형식 검사를 무시합니다. 중첩 된 구조를 사용하면 디버그 할 때 악몽이 될 수 있습니다.

모든 조언이나 토론을 부탁드립니다.

답변

2

자신의 요소 이름을 사용하여 자신의 struct을 사용하지 않는 이유는 무엇입니까? 당신이 당신의 자신의 operator<을 정의 할 경우지도로 std::set 작업을 할 수 있습니다,

MyPair{std::string("Big5 to Utf16LE"), &FileConvert_Big5ToUtf16LE}} 

을 그리고 :

bool operator<(const MyPair& a, const MyPair& b) { 
    return a.strCaption < b.strCaption; 
} 

typedef std::set<MyPair> MyPairMap; 
C++ (11)와

struct MyPair { 
    std::string strCaption; 
    PFComm pfComm; 
}; 

당신은 쉽게 그것을 새로운 개체를 만들 수 있습니다

물론 네이티브 구조체를 중첩하여 더 복잡한 중첩 된 쌍을 만들 수도 있지만, 경우에 따라 평면형 3 중선을 고려할 수도 있습니다.

struct CommMapEntry { 
    std::string number; 
    std::string caption; 
    PFComm pfComm; 
}; 
bool operator<(const MyPair& a, const MyPair& b) { 
    return a.number<b.number; 
} 
static const std::set<CommMapEntry> COMM_MAP; 
+0

그래서 간단한 방법! 그러나 나는 set :: find() (이상한 주조 인 것 같다?)를 사용하기 위해 캐스터 (문자열 -> CommMapEntry)를 써야한다. 그러나 ctors를 std :: map으로 다시 작성하는 것보다 훨씬 효율적입니다. 고마워요! – Cigany

2

일부 typedef 및 접근 자 기능은 어떻습니까?

using CommEntry = std::pair<std::string, PFConvert>; 

std::string const & getCaption(CommEntry const & e) { return e.first; } 
PFConvert const & getFunction(CommEntry const & e) { return e.second; } 

이제 당신은 말할 수 :

auto it = COMM_MAP.find(strTransType); 
if (it != COMM_MAP.end()) 
{ 
    auto & c = getCaption(it->second); 
    auto & l = getLabel(it->second); 
    // ... 
} 

나중에 유형의 세부 사항을 변경하는 경우, 당신은 단지 접근 기능을 적용합니다.

+0

별칭 ("."또는 "->"을 사용하여 회원 스타일을 좋아하지만)은 경제적으로 좋은 방법이지만. 고마워. – Cigany

1

우물, C++ 11, 우리는 using base::base을 기본 ctors를 사용하는 클래스로 만들 수 있습니다. 그러나 vs2013이이를 준수하지 않음을 유의하십시오. g ++ 4.8 않습니다.

관련 문제