C++核心準(zhǔn)則C.100:定義容器時(shí)遵從STL標(biāo)準(zhǔn)?




C.100: Follow the STL when defining a container
C.100:定義容器時(shí)遵從STL標(biāo)準(zhǔn)?




Reason(原因)
The STL containers are familiar to most C++ programmers and a fundamentally sound design.
STL容器被大多數(shù)程序員所熟知,是非常完美的設(shè)計(jì)。

Note(注意)?
There are of course other fundamentally sound design styles and sometimes reasons to depart from the style of the standard library, but in the absence of a solid reason to differ, it is simpler and easier for both implementers and users to follow the standard.
當(dāng)然存在其他的完美設(shè)計(jì),有時(shí)也存在違背標(biāo)準(zhǔn)庫(kù)風(fēng)格的進(jìn)行設(shè)計(jì)的理由,但如果沒(méi)有足夠充分的理由,遵照標(biāo)準(zhǔn)庫(kù)風(fēng)格對(duì)于實(shí)現(xiàn)者和使用者雙方都簡(jiǎn)單和容易。
In particular, std::vector and std::map provide useful relatively simple models.
特別是std::vector和std::map,提供了有用且相當(dāng)簡(jiǎn)單的模型。

Example(示例)?
//?simplified?(e.g.,?no?allocators):
template
class?Sorted_vector?{
????using?value_type?=?T;
????//?...?iterator?types?...
????Sorted_vector()?=?default;
????Sorted_vector(initializer_list);????//?initializer-list?constructor:?sort?and?store
????Sorted_vector(const?Sorted_vector&)?=?default;
????Sorted_vector(Sorted_vector&&)?=?default;
????Sorted_vector&?operator=(const?Sorted_vector&)?=?default;???//?copy?assignment
????Sorted_vector&?operator=(Sorted_vector&&)?=?default;????????//?move?assignment
????~Sorted_vector()?=?default;
????Sorted_vector(const?std::vector&?v);???//?store?and?sort
????Sorted_vector(std::vector&&?v);????????//?sort?and?"steal?representation"
????const?T&?operator[](int?i)?const?{?return?rep[i];?}
????//?no?non-const?direct?access?to?preserve?order
????void?push_back(const?T&);???//?insert?in?the?right?place?(not?necessarily?at?back)
????void?push_back(T&&);????????//?insert?in?the?right?place?(not?necessarily?at?back)
????//?...?cbegin(),?cend()?...
private:
????std::vector?rep;??//?use?a?std::vector?to?hold?elements
};
template?bool?operator==(const?Sorted_vector &,?const?Sorted_vector &);
template?bool?operator!=(const?Sorted_vector &,?const?Sorted_vector &);
//?...
Here, the STL style is followed, but incompletely. That's not uncommon. Provide only as much functionality as makes sense for a specific container. The key is to define the conventional constructors, assignments, destructors, and iterators (as meaningful for the specific container) with their conventional semantics. From that base, the container can be expanded as needed. Here, special constructors from std::vector were added.
這里遵守了標(biāo)準(zhǔn)庫(kù)風(fēng)格,但是不完全。這沒(méi)有什么特別。應(yīng)該提供構(gòu)成特定容器的需要的功能。關(guān)鍵是定義帶有常規(guī)語(yǔ)義的符合常規(guī)的構(gòu)造函數(shù),復(fù)制運(yùn)算符,析構(gòu)函數(shù)和迭代器(對(duì)于特殊容器有意義)。以此為基礎(chǔ),容器可以按照需要進(jìn)行擴(kuò)展。這里增加了來(lái)自std::vector的特殊構(gòu)造函數(shù)。

Enforcement(實(shí)施建議)
???

原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c100-follow-the-stl-when-defining-a-container
覺(jué)得本文有幫助?請(qǐng)分享給更多人。
關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_(kāi)發(fā),面向?qū)ο笏伎迹?/span>
