Associative Containers

Notes from C++ Primer

Associative containers differ in fundamental respect from the sequential containers: elements in associative containers are stored and retrieved by a key, in contrast to elements in a sequential container, which are stored and accessed sequentially by their position within the container.

Associative container supports using key to find and access element with high efficiency. There‘re two base associative container type: map and set. The element of map is organized with the key-value form: the key is used as the index of map, and the value is the data of storing and accessing. The set contains only one key and supports efficient queries to whether a given key is present.

pair Type

pair is also a kind of template type, but with two type parameters passing when pair is initialized.

pair<string, string> anon;			// holds two strings
pair<string, int> word_count;		// holds a string and an int
pair<string, vector<int> > line;	// holds string and vector<int>

We also can provides initial value in definition:

pair<string, string> author("James", "Joyce");

If we need to define many same pair type, we can use typedef to simplify the declaration:

typedef pair<string, string> Author;
Author proust("Marcel", "Proust");
Author joyce("James", "Joyce");

pair object has two member variable: first, and second. The dot operation can be used to access them:

string firstBook;

// access and test the data members of the pair
if(author.first == "James" && author.second == "Joyce")
	firstBook = "Stephen Hero";

The library provides make_pair function to generate new pair object:

pair<string, string> next_auth;
string first, last;
while(cin >> first >> last)
{
	// generate a pair from first and last
	next_auth = make_pair(first, last);

	// process next_auth ...
}

These operations are equivalent to the below operations:

// use pair constructor to make first and last into a pair
next_auth = pair<string, string>(first, last);

or read directly from input stream:

pair<string, string> next_auth;

// read directly into the members of next_auth
while(cin >> next_auth.first >> next_auth.second)
{
	// process next_auth ...
}

map Type

A map is a collection of key-value pairs. It is often referred as an associative array: use the key to get value instead of using position to get value. There‘s a constraint for the key type. The type of key must support the comparasion function "<". And the "<" relationship must be validate.

Dereference the map iterator will generate pair type object:

// count number of times each word occurs in the input
map<string, int> word_count;	// empty map from string to int

// get an iterator to an element in word_count
map<string, int>::iterator map_it = word_count.begin();

// *map_it is a reference to a pair<const string, int> object
cout << map_it->first;			// prints the key for this element
cout << map_it->second;			// prints the value of the element
map_it->first = "new key";		// error: key is const
++map_it->second;				// ok: we can change value through an iterator
时间: 2024-08-30 08:11:38

Associative Containers的相关文章

关联式容器(associative containers)

根据数据在容器中的排列特性,容器可分为序列式(sequence)和关联式(associative)两种. 标准的STL关联式容器分为set(集合)和map(映射表)两大类,以及两大类的衍生体multiset(多键集合)和multimap(多键映射表).这些容器的底层机制均是以RB-tree(红黑树)完成.RB-tree也是一个独立的容器,但并不开放给外界使用. 此外,SGI STL还提供一个不在规格标准之列的关联式容器:hash table(散列表),以及以此hash table为底层机制而完成

C++ unordered Associative Containers(C++11)

C++11 引进了无序关联容器(unordered associative containers)的概念. 有unordered set or multiset, 以及unordered map or multimap. 顾名思义, unordered的意思就是元素没有固定的顺序, 并且元素的顺序可能会随着时间的变化而变化. Internally, unordered container 是使用hash table(哈希表)实现的. 所谓的hash Table, 就是an array of li

Understand the Qt containers(有对应表)

Container classes are one of the cornerstones of object-oriented programming, invaluable tools that free us from having to permanently think about memory management. Qt comes with its own set of container classes, closely modeled after those in the S

C++ STL源码学习(之hash_table篇)

stl_hash_table.h 这不属于C++标准,是SGI STL标准的一部分,用于辅助实现hash_map和hash_set /// Hashtable class, used to implement the hashed associative containers /// hash_set, hash_map, hash_multiset, and hash_multimap. ///STL HashTable采用的是所谓的开链哈希法,依靠一个类似vector<list<T>

C++ STL源码学习(之RB Tree篇)

stl_tree.h 这是整个STL中最复杂的数据结构,也是我接触到的最复杂的数据结构之一 /** Red-black tree class, designed for use in implementing STL associative containers (set, multiset, map, and multimap). The insertion and deletion algorithms are based on those in Cormen, Leiserson, and

STL中map与hash_map容器的选择

[转]STL中map与hash_map容器的选择 先看看alvin_lee 朋友做的解析,我觉得还是很正确的,从算法角度阐述了他们之间的问题! 实际上这个问题不光C++会遇到,其他所有语言的标准容器的实现及选择上都是要考虑的.做应用程序你可能觉得影响不大,但是写算法或者核心代码就要小心了.今天改进代码,顺便又来温习基础功课了. 还记得Herb Sutter那极有味道的<C++对话系列>么,在其中<产生真正的hash对象>这个故事里就讲了map的选择.顺便回顾一下,也讲一下我在实用中

C++拾遗(七)——关联容器

关联容器(Associative containers)支持通过键来高效地查找和读取元素.两个基本的关联容器类型是 map 和set.map 的元素以键-值(key-value)对的形式组织:键用作元素在 map 中的索引,而值则表示所存储和读取的数据.set仅包含一个键,并有效地支持关于某个键是否存在的查询.set 和 map 类型的对象所包含的元素都具有不同的键,不允许为同一个键添加第二个元素.如果一个键必须对应多个实例,则需使用 multimap 或 multiset,这两种类型允许多个元

STL容器 成员函数对比表

Sequence containers Associative containers   Headers <vector> <deque> <list> <set>   <bitset> Members complex vector deque list set multiset map multimap bitset constructor * constructor constructor constructor constructor co

chapter 16

Chapter 16 # string class Constructors (Page 952) string(const char *s) string(size_type n, char c) string(const string &str) string() string(const char *s,size_type n) template<class Iter> string(Iter begin, Iter end) string(const string &s