C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources] - نسخه متنی

Herb Sutter, Andrei Alexandrescu

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید


STL: Containers


76. Use vector by default. Otherwise, choose an appropriate container.

Using the "right container" is great: If you have a good reason to use a specific container type, use that container type knowing that you did the right thing.

So is using

vector

: Otherwise, write

vector

and keep going without breaking stride, also knowing you did the right thing.

77. Use vector and string instead of arrays.

Why juggle Ming vases? Avoid implementing array abstractions with C-style arrays, pointer arithmetic, and memory management primitives. Using

vector

or

string

not only makes your life easier, but also helps you write safer and more scalable software.

78. Use vector (and string::c_str) to exchange data with non-C++ APIs.

vector

isn't lost in translation:

vector

and

string::c_str

are your gateway to communicate with non-C++ APIs. But don't assume iterators are pointers; to get the address of the element referred to by a

vector<T>::iterator iter

, use

&*iter .

79. Store only values and smart pointers in containers.

Store objects of value in containers: Containers assume they contain value-like types, including value types (held directly), smart pointers, and iterators.

80. Prefer push_back to other ways of expanding a sequence.

push_back

all you can: If you don't need to care about the insert position, prefer using

push_back

to add an element to sequence. Other means can be both vastly slower and less clear.

81. Prefer range operations to single-element operations.

Don't use oars when the wind is fair (based on a Latin proverb): When adding elements to sequence containers, prefer to use range operations (e.g., the form of

insert

that takes a pair of iterators) instead of a series of calls to the single-element form of the operation. Calling the range operation is generally easier to write, easier to read, and more efficient than an explicit loop. (See alsoItem 84.)

82. Use the accepted idioms to really shrink capacity and really erase elements.

Use a diet that works: To really shed excess capacity from a container, use the "swap trick." To really erase elements from a container, use the erase-remove idiom.


/ 521