set map multiset和multimap被称为关联式容器
因为它们将关键字和值关联起来
至少map和multimap将关键字与值关联在一起
可以将一个set看成事没有值的map
//: C07:AssociativeBasics.cpp {-bor}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Basic operations with sets and maps.
//{L} Noisy
#include <cstddef>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include "Noisy.h"
using namespace std;
int main() {
Noisy na[7];
// Add elements via constructor:
set<Noisy> ns(na, na + sizeof na/sizeof(Noisy));
Noisy n;
ns.insert(n); // Ordinary insertion
cout << endl;
// Check for set membership:
cout << "ns.count(n)= " << ns.count(n) << endl;
if(ns.find(n) != ns.end())
cout << "n(" << n << ") found in ns" << endl;
// Print elements:
copy(ns.begin(), ns.end(),
ostream_iterator<Noisy>(cout, " "));
cout << endl;
cout << "\n-----------" << endl;
map<int, Noisy> nm;
for(int i = 0; i < 10; i++)
nm[i]; // Automatically makes pairs
cout << "\n-----------" << endl;
for(size_t j = 0; j < nm.size(); j++)
cout << "nm[" << j <<"] = " << nm[j] << endl;
cout << "\n-----------" << endl;
nm[10] = n;
cout << "\n-----------" << endl;
nm.insert(make_pair(47, n));
cout << "\n-----------" << endl;
cout << "\n nm.count(10)= " << nm.count(10) << endl;
cout << "nm.count(11)= " << nm.count(11) << endl;
map<int, Noisy>::iterator it = nm.find(6);
if(it != nm.end())
cout << "value:" << (*it).second
<< " found in nm at location 6" << endl;
for(it = nm.begin(); it != nm.end(); it++)
cout << (*it).first << ":" << (*it).second << ", ";
cout << "\n-----------" << endl;
getchar();
} ///:~
输出
d[0]
d[1]
d[2]
d[3]
d[4]
d[5]
d[6]
c[0]
c[1]
c[2]
c[3]
c[4]
c[5]
c[6]
d[7]
c[7]
ns.count(n)= 1
n(7) found in ns
0 1 2 3 4 5 6 7
-----------
d[8]
c[8]
c[8]
~[8]
~[8]
d[9]
c[9]
c[9]
~[9]
~[9]
d[10]
c[10]
c[10]
~[10]
~[10]
d[11]
c[11]
c[11]
~[11]
~[11]
d[12]
c[12]
c[12]
~[12]
~[12]
d[13]
c[13]
c[13]
~[13]
~[13]
d[14]
c[14]
c[14]
~[14]
~[14]
d[15]
c[15]
c[15]
~[15]
~[15]
d[16]
c[16]
c[16]
~[16]
~[16]
d[17]
c[17]
c[17]
~[17]
~[17]
-----------
nm[0] = 8
nm[1] = 9
nm[2] = 10
nm[3] = 11
nm[4] = 12
nm[5] = 13
nm[6] = 14
nm[7] = 15
nm[8] = 16
nm[9] = 17
-----------
d[18]
c[18]
c[18]
~[18]
~[18]
(18)=[7]
-----------
c[7]
c[7]
~[7]
c[7]
~[7]
-----------
nm.count(10)= 1
nm.count(11)= 0
value:14 found in nm at location 6
0:8, 1:9, 2:10, 3:11, 4:12, 5:13, 6:14, 7:15, 8:16, 9:17, 10:7, 47:7,
-----------
使用两个迭代器来创建set<Noisy>对象ns
使其进入一个Noisy对象的数组之内
但是也有一个默认的构造函数和一个拷贝构造函数
可以传入一个对象
map::value_type是map的一个关键字-值 pair
是map的一个条目
pair由值封装它的对象
意味着将对象装入pair之内,拷贝构造是必须的
//: C07:NoisyMap.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Mapping Noisy to Noisy.
//{L} Noisy
#include <map>
#include "Noisy.h"
using namespace std;
int main() {
map<Noisy, Noisy> mnn;
Noisy n1, n2;
cout << "\n--------" << endl;
mnn[n1] = n2;
cout << "\n--------" << endl;
cout << mnn[n1] << endl;
cout << "\n--------" << endl;
getchar();
} ///:~
输出
d[0]
d[1]
--------
d[2]
c[0]
c[2]
c[0]
c[2]
~[2]
~[0]
~[2]
(2)=[1]
--------
1
--------
插入和查询两者都会产生很多额外的对象
因为tmp对象的创建
如果看map::operator[]
就会看到第2行调用了insert()
并向其传递tmp