C++编程思想 第2卷 第7章 通用容器 关联式容器 多重集合

set仅允许插入每个值的唯一一个对象
而multiset看起来比较古怪
因为它允许插入每个值的多个对象

包含一个用于进行比较的元素与另一个不用于进行比较的元素的类

//: C07:MultiSet1.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.
// Demonstration of multiset behavior.
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
#include <set>
using namespace std;

class X {
  char c; // Used in comparison
  int i; // Not used in comparison
  // Don't need default constructor and operator=
  X();
  X& operator=(const X&);
  // Usually need a copy-constructor (but the
  // synthesized version works here)
public:
  X(char cc, int ii) : c(cc), i(ii) {}
  // Notice no operator== is required
  friend bool operator<(const X& x, const X& y) {
    return x.c < y.c;
  }
  friend ostream& operator<<(ostream& os, X x) {
    return os << x.c << ":" << x.i;
  }
};

class Xgen {
  static int i;
  // Number of characters to select from:
  enum { SPAN = 6 };
public:
  X operator()() {
    char c = 'A' + rand() % SPAN;
    return X(c, i++);
  }
};

int Xgen::i = 0;

typedef multiset<X> Xmset;
typedef Xmset::const_iterator Xmit;

int main() {
  Xmset mset;
  // Fill it with X's:
  srand(time(0));  // Randomize
  generate_n(inserter(mset, mset.begin()), 25, Xgen());
  // Initialize a regular set from mset:
  set<X> unique(mset.begin(), mset.end());
  copy(unique.begin(), unique.end(),
    ostream_iterator<X>(cout, " "));
  cout << "\n----" << endl;
  // Iterate over the unique values:
  for(set<X>::iterator i = unique.begin();
      i != unique.end(); i++) {
    pair<Xmit, Xmit> p = mset.equal_range(*i);
    copy(p.first,p.second, ostream_iterator<X>(cout, " "));
    cout << endl;
  }
  getchar();
} ///:~


输出
A:20 B:11 C:24 D:22 E:14 F:23
----
A:20 A:18 A:9 A:5 A:2
B:11 B:4
C:24 C:21 C:6
D:22 D:19 D:8 D:3
E:14 E:15 E:12 E:10 E:7 E:0
F:23 F:16 F:17 F:13 F:1

在X中 所有的比较都产生char c
因为使用了默认的less比较对象
比较由operator<进行
这是multiset所必须的全部工作

multiset创建的一个版本

//: C07:MultiSetWordCount.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.
// Count occurrences of words using a multiset.
#include <fstream>
#include <iostream>
#include <iterator>
#include <set>
#include <string>
#include "../require.h"
using namespace std;

int main(int argc, char* argv[]) {
  const char* fname = "MultiSetWordCount.cpp";
  if(argc > 1) fname = argv[1];
  ifstream in(fname);
  assure(in, fname);
  multiset<string> wordmset;
  string word;
  while(in >> word)
    wordmset.insert(word);
  typedef multiset<string>::iterator MSit;
  MSit it = wordmset.begin();
  while(it != wordmset.end()) {
    pair<MSit, MSit> p = wordmset.equal_range(*it);
    int count = distance(p.first, p.second);
    cout << *it << ": " << count << endl;
    it = p.second; // Move to the next word
  }
  getchar();
} ///:~


输出  放工程目录 MultiSetWordCount.cpp
!=: 1
": 1
"../require.h": 1
":: 1
"MultiSetWordCount.cpp";: 1
"Thinking: 1
#include: 6
&: 1
'License.txt',: 1
(c): 1
*it: 1
//: 6
///:~: 1
//:: 1
1): 1
1995-2004: 1
2",: 1
<<: 4
<fstream>: 1
<iostream>: 1
<iterator>: 1
<set>: 1
<string>: 1
=: 6
>: 1
>>: 1
All: 1
Allison.: 1
Bruce: 1
C++,: 1
C07:MultiSetWordCount.cpp: 1
Chuck: 1
Count: 1
Eckel: 1
From: 1
Inc.: 1
MSit: 1
MSit;: 1
MSit>: 1
MindView,: 1
Move: 1
Reserved.: 1
Rights: 1
See: 1
Volume: 1
a: 1
argc,: 1
argv[1];: 1
argv[]): 1
assure(in,: 1
at: 1
available: 1
by: 1
char*: 2
code: 2
const: 1
count: 2
cout: 1
distance(p.first,: 1
distributed: 1
endl;: 1
file: 1
fname: 2
fname);: 1
getchar();: 1
if(argc: 1
ifstream: 1
in: 2
in(fname);: 1
int: 2
it: 2
main(int: 1
multiset.: 1
multiset<string>: 1
multiset<string>::iterator: 1
namespace: 1
next: 1
occurrences: 1
of: 1
p: 1
p.second);: 1
p.second;: 1
package: 1
pair<MSit,: 1
permissions: 1
source: 1
stated: 1
std;: 1
string: 1
the: 3
to: 1
typedef: 1
use: 1
using: 2
while(in: 1
while(it: 1
with: 1
word: 1
word): 1
word;: 1
wordmset.begin();: 1
wordmset.end()): 1
wordmset.equal_range(*it);: 1
wordmset.insert(word);: 1
wordmset;: 1
words: 1
www.MindView.net.: 1
{: 2
}: 2

每个单词都只是被插入到multiset<string>中
一个迭代器被创建出来并且初始化为指向multiset的起始处
解析迭代器就可以产生它所指向的当前单词
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值