vector 一系列空间连续的元素;可以用作默认容器
#include <vector>
vector();
vector( const vector& c );
vector( size_type num, const TYPE& val = TYPE() );
vector( input_iterator start, input_iterator end );
~vector();
第一个构造函数的时间复杂度是常量,其余都是线性的。
// create a vector of random integers
cout << "original vector: ";
vector<int> v;
for( int i = 0; i < 10; i++ ) {
int num = (int) rand() % 10;
cout << num << " ";
v.push_back( num );
}
cout << endl;
// find the first element of v that is even
vector<int>::iterator iter1 = v.begin();
while( iter1 != v.end() && *iter1 % 2 != 0 ) {
iter1++;
}
// find the last element of v that is even
vector<int>::iterator iter2 = v.end();
do {
iter2--;
} while( iter2 != v.begin() && *iter2 % 2 != 0 );
// only proceed if we find both numbers
if( iter1 != v.end() && iter2 != v.begin() ) {
cout << "first even number: " << *iter1 << ", last even number: " << *iter2 << endl;
cout << "new vector: ";
vector<int> v2( iter1, iter2 );
for( int i = 0; i < v2.size(); i++ ) {
cout << v2[i] << " ";
}
cout << endl;
}
list 一个双向链表;当希望在不移动现有元素的情况下完成对元素的插入和删除时使用
The template class describes an object that controls a varying-length sequence of elements that has bidirectional access. You use the container list to manage a sequence of elements as a bidirectional linked list of nodes, each storing one element.
template<typename Value>
ref class list
: public
System::ICloneable,
System::Collections::IEnumerable,
System::Collections::ICollection,
System::Collections::Generic::IEnumerable<GValue>,
System::Collections::Generic::ICollection<GValue>,
Microsoft::VisualC::StlClr::IList<GValue>
{ ..... };
deque 列表和向量的结合;除非对算法和计算机结构非常精通,否则不要使用它
Double-ended queues (or deques) are similar to vectors, except that they allow fast insertions and deletions at both the beginning and the end of the container and that they are not required to be contiguous.
template<
class T,
class Allocator = std::allocator<T>
> class deque;
The template class describes an object that controls a varying-length sequence of elements that has random access. You use the container deque to manage a sequence of elements that looks like a contiguous block of storage, but which can grow or shrink at either end without the need to copy any remaining elements. Thus it can implement efficiently a double-ended queue
list与deque都是双向的,但是deque支持随机访问。
map 一个平衡的有序树;当需要实现按值访问元素时使用
The template class describes an object that controls a varying-length sequence of elements that has bidirectional access. You use the container map to manage a sequence of elements as a (nearly) balanced ordered tree of nodes, each storing one element. An element consists of a key, for ordering the sequence, and a mapped value, which goes along for the ride
template<typename Key,
typename Mapped>
ref class map
: public
System::ICloneable,
System::Collections::IEnumerable,
System::Collections::ICollection,
System::Collections::Generic::IEnumerable<GValue>,
System::Collections::Generic::ICollection<GValue>,
System::Collections::Generic::IList<GValue>,
System::Collections::Generic::IDictionary<Gkey, GMapped>,
Microsoft::VisualC::StlClr::ITree<Gkey, GValue>
{ ..... };
multimap 一个平衡的有序树,其中可以包含同一个key的多个拷贝;当需要实现按值访问元素时使用
unordered_multimap 一个可以包含同一个key的多个拷贝的散列表;一种优化的multimap;当对性能要求很高且可以设计出较好的散列函数时使用
set 一个平衡的有序树;当需要对每个值进行跟踪时使用
multiset 一个可以包含同一个key 的多个拷贝的平衡的有序树;当需要对每个值进行跟踪时使用
unordered_set 与unordered_map相似,但只有value,不是二元组(key, value)
unordered_multiset 鱼unordered_multimap相似,但只有value,不是二元组(key, value)
array 一个大小固定的数组,不存在嵌入数组所存在的大部分问题
The array keyword lets you create a dynamic array that is allocated on the common language runtime heap.