Notes

[1] This member function relies on member template functions, which at present (early 1998) are not supported by all compilers. If your compiler supports member templates, you can call this function with any type of input iterator. If your compiler does not yet support member templates, though, then the arguments must either be of type const bool* or of type bit_vector::const_iterator.

[2] Memory will be reallocated automatically if more than capacity() – size() bits are inserted into the bit_vector. Reallocation does not change size(), nor does it change the values of any bits of the bit_vector. It does, however, increase capacity(), and it invalidates [5] any iterators that point into the bit_vector.

[3] When it is necessary to increase capacity(), bit_vector usually increases it by a factor of two. It is crucial that the amount of growth is proportional to the current capacity(), rather than a fixed constant: in the former case inserting a series of bits into a bit_vector is a linear time operation, and in the latter case it is quadratic.

[4] reserve() is used to cause a reallocation manually. The main reason for using reserve() is efficiency: if you know the capacity to which your bit_vector must eventually grow, then it is probably more efficient to allocate that memory all at once rather than relying on the automatic reallocation scheme. The other reason for using reserve() is to control the invalidation of iterators. [5]

[5] A bit_vector's iterators are invalidated when its memory is reallocated. Additionally, inserting or deleting a bit in the middle of a bit_vector invalidates all iterators that point to bits following the insertion or deletion point. It follows that you can prevent a bit_vector's iterators from being invalidated if you use reserve() to preallocate as much storage as the bit_vector will ever use, and if all insertions and deletions are at the bit_vector's end.

See also

vector

Associative Containers

set<Key, Compare, Alloc>

Category: containers

Component type: type

Description

Set is a Sorted Associative Container that stores objects of type Key. Set is a Simple Associative Container, meaning that its value type, as well as its key type, is Key. It is also a Unique Associative Container, meaning that no two elements are the same.

Set and multiset are particularly well suited to the set algorithms includes, set_union, set_intersection, set_difference, and set_symmetric_difference. The reason for this is twofold. First, the set algorithms require their arguments to be sorted ranges, and, since set and multiset are Sorted Associative Containers, their elements are always sorted in ascending order. Second, the output range of these algorithms is always sorted, and inserting a sorted range into a set or multiset is a fast operation: the Unique Sorted Associative Container and Multiple Sorted Associative Container requirements guarantee that inserting a range takes only linear time if the range is already sorted.

Set has the important property that inserting a new element into a set does not invalidate iterators that point to existing elements. Erasing an element from a set also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.

Example

struct ltstr {

 bool operator()(const char* s1, const char* s2) const {

  return strcmp(s1, s2) < 0;

 }

};

int main() {

 const int N = 6;

 const char* a[N] = {'isomer', 'ephemeral', 'prosaic', 'nugatory', 'artichoke', 'serif'};

 const char* b[N] = {'flat', 'this', 'artichoke', 'frigate', 'prosaic', 'isomer'};

 set<const char*, ltstr> A(a, a + N);

 set<const char*, ltstr> B(b, b + N);

 set<const char*, ltstr> C;

 cout << 'Set A: ';

 copy(A.begin(), A.end(), ostream_iterator<const char*>(cout, ' '));

 cout << endl;

 cout << 'Set B: ';

 copy(B.begin(), B.end(), ostream_iterator<const char*>(cout, ' '));

 cout << endl;

 cout << 'Union: ';

 set_union(A.begin(), A.end(), B.begin(), B.end(), ostream_iterator<const char*>(cout, ' '), ltstr());

 cout << endl;

 cout << 'Intersection: ';

 set_intersection(A.begin(), A.end(), B.begin(), B.end(), ostream_iterator<const char*>(cout, ' '), ltstr());

 cout << endl;

 set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(C, C.begin()), ltstr ());

 cout << 'Set C (difference of A and B): ';

 copy(C.begin(), C.end(), ostream_iterator <const char*>(cout, ' '));

 cout << endl;

}

Definition

Defined in the standard header set, and in the nonstandard backward-compatibility header set.h.

Template parameters
Parameter Description
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату