Stl 对于不习惯多索引容器的程序员,如何提高其可读性?
我创建了一个多索引容器,其中包含3个非唯一、无顺序的键,如下所示:Stl 对于不习惯多索引容器的程序员,如何提高其可读性?,stl,containers,c++17,visual-studio-2019,boost-multi-index,Stl,Containers,C++17,Visual Studio 2019,Boost Multi Index,我创建了一个多索引容器,其中包含3个非唯一、无顺序的键,如下所示: namespace bmi = boost::multi_index; class SurveyRepository { // some other code using SurveyCodeContainer = boost::multi_index_container< SurveyCode, bmi::indexed_by< bmi::hashed
namespace bmi = boost::multi_index;
class SurveyRepository {
// some other code
using SurveyCodeContainer = boost::multi_index_container<
SurveyCode,
bmi::indexed_by<
bmi::hashed_non_unique<bmi::tag<Survey>, bmi::member<SurveyCode, unsigned, &SurveyCode::survey_id>>,
bmi::hashed_non_unique<bmi::tag<Table>, bmi::member<SurveyCode, unsigned, &SurveyCode::table_id>>,
bmi::hashed_non_unique<bmi::tag<Check>, bmi::member<SurveyCode, unsigned, &SurveyCode::check_id>>
>
>;
SurveyCodeContainer m_SurveyCodeContainer;
};
namespace bmi=boost::multi_index;
班级调查报告{
//其他代码
使用SurveyCodeContainer=boost::多索引容器<
调查代码,
bmi::按索引索引<
bmi::哈希非唯一,
bmi::哈希非唯一,
bmi::哈希非唯一
>
>;
SurveyCodeContainer m_SurveyCodeContainer;
};
关键是能够使用这些键中的任何一个来搜索SurveyCode
对象,我认为这是一个完全可读的、非常简洁的解决方案
但是有一次代码审查,尽管我们的代码库中已经使用了multi_index_容器,但有些人对以下评论感到困惑:
难道没有一个不那么难看的容器吗
那么,有没有办法让它不那么难看/更具可读性?我们使用的是Visual Studio 2019,我更喜欢stl而不是boost提供的解决方案,但我想没有,对吧?如果您使用的是C++17,那么有一个可用的:
namespace bmi = boost::multi_index;
class SurveyRepository {
// some other code
using SurveyCodeContainer = boost::multi_index_container<
SurveyCode,
bmi::indexed_by<
bmi::hashed_non_unique<bmi::tag<Survey>, bmi::key<&SurveyCode::survey_id>>,
bmi::hashed_non_unique<bmi::tag<Table>, bmi::key<&SurveyCode::table_id>>,
bmi::hashed_non_unique<bmi::tag<Check>, bmi::key<&SurveyCode::check_id>>
>
>;
SurveyCodeContainer m_SurveyCodeContainer;
};
namespace bmi=boost::multi_index;
班级调查报告{
//其他代码
使用SurveyCodeContainer=boost::多索引容器<
调查代码,
bmi::按索引索引<
bmi::哈希非唯一,
bmi::哈希非唯一,
bmi::哈希非唯一
>
>;
SurveyCodeContainer m_SurveyCodeContainer;
};
我从未听说过boost::multi_index_容器,在看了大约10秒钟后,我发现typedef非常容易理解。只有我的两分钱:)。