STL Collection Types

 

STL Collection Types

Each STL collection type has its own template parameters, which will be discussed later. What type of collection you use is up to your needs and tastes. From past experience, the vector and map classes are the most useful. The vector class is ideal for simple and complex collection types, while the map class is used when an associative type of collection is needed. The deque collection is excellent for use in systems that have queued based processing, such as a message based system.

vector
A collection of elements of type T.
list
A collection of elements of type T. The collection is stored as a bi-directional linked list of elements, each containing a member of type T.

To include the class definition use:

#include <list>
deque
A collection of varying length of elements of type T. The sequence is represented in a way that permits insertion and removal of an element at either end with a single element copy, and supports insertion and removal anywhere in the sequence, but a sequence copy follows each operation.

To include the class definition use:

#include <deque>
map
A collection for a varying length sequence of elements of type pair<const Key, T>. The first element of each pair is the sort key and the second is its associated value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element. The key can be a simple as a number or string or as complex a key class. The key class must support normal comparison operations so that the collection can be sorted or searched.

To include the class definition use:

#include <map>
set
A collection that controls a varying length sequence of elements of type const Key. Each element serves as both a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element.

To include the class definition use:

#include <set>
multimap
A collection of a varying length sequence of elements of type pair<const Key, T>. The first element of each pair is the sort key and the second is its associated value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element.

To include the class definition use:

#include <map>
multiset
A collection of a varying-length sequence of elements of type const Key. Each element serves as both a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element.

To include the class definition use:

#include <set>

STL Strings

STL strings support both ascii and unicode character strings.

string
A string is a collection of ascii characters that supports both insertion and removal.

To include the string class definitions use:

#include <string>
wstring
A wstring is a collection of wide characters that it supports both insertion and removal. In MFC the string class is CString, which provides a Format and other methods to manipulate the string. CString has the advantage of providing methods such as Format, TrimLeft, TrimRight and LoadString. It is easy to provide a string-based class that contains these methods.

To include the wstring class definitions use:

#include <xstring>

STL Streams

Streams provide the developer with classes that can output to a container variable types of stream elements.

stringstream
A string stream that supports insertions of elements, and elements are inserted via the overloaded operator <<. The method str() gives a reference back to the underlying string, and the c_str() can be used to get a constant pointer to the string buffer.
wstringstream
A wstring stream that supports insertions of elements, and elements are inserted via the overloaded operator <<. The method str() gives a reference back to the underlying string, and the c_str() can be used to get a constant pointer to the string buffer.

To use a string stream we would do the follwing:

stringstream strStr;
for ( long i=0; i< 10; i++ )
strStr << "Element " << i << endl;

To include the string class definitions use:

#include <strstring>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值