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>>>
{};
을 다음과 같은
그래서 나는 별칭을 제공하기 위해 상속을 사용하려고하지만 이것은 또 다른 문제에 관해서 똑똑한 방법 인 것 같습니다. 나는 별칭을 만들고 싶다.
간단한 방법은 매크로를 사용하는 것입니다. 그러나 형식 검사를 무시합니다. 중첩 된 구조를 사용하면 디버그 할 때 악몽이 될 수 있습니다.
모든 조언이나 토론을 부탁드립니다.
그래서 간단한 방법! 그러나 나는 set :: find() (이상한 주조 인 것 같다?)를 사용하기 위해 캐스터 (문자열 -> CommMapEntry)를 써야한다. 그러나 ctors를 std :: map으로 다시 작성하는 것보다 훨씬 효율적입니다. 고마워요! – Cigany