C++编程思想 第2卷 第7章 通用容器 概述

容器类是一种特定代码重用问题的解决方案

set模板
一个模拟传统数学集合的容器
该容器不接受重复值
下面创建的set与int整形数据一起工作

//: C07:Intset.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.
// Simple use of STL set.
#include <cassert>
#include <set>
using namespace std;

int main() {
  set<int> intset;
  for(int i = 0; i < 25; i++)
    for(int j = 0; j < 10; j++)
      // Try to insert duplicates:
      intset.insert(j);
  assert(intset.size() == 10);
} ///:~

无异常对话框

成员函数insert()完成所有的工作
试图插入一个元素并且如果容器中已经存在相同的元素则不予插入

在使用一个集合中涉及的操作通常只限于插入元素和检测集合是否
要插入的元素


使用IntSet.cpp形式修改
用以显示包含在一个文档中的单词清单

//: C07:WordSet.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.
#include <fstream>
#include <iostream>
#include <iterator>
#include <set>
#include <string>
#include "../require.h"
using namespace std;

void wordSet(const char* fileName) {
  ifstream source(fileName);
  assure(source, fileName);
  string word;
  set<string> words;
  while(source >> word)
    words.insert(word);
  copy(words.begin(), words.end(),
    ostream_iterator<string>(cout, "\n"));
  cout << "Number of unique words:"
       << words.size() << endl;
}

int main(int argc, char* argv[]) {
  if(argc > 1)
    wordSet(argv[1]);
  else
    wordSet("WordSet.cpp");
  getchar();
} ///:~


输入 要放一个WordSet.cpp 或者自己制定一个参数
"../require.h"
"Number
"Thinking
"\n"));
#include
&
'License.txt',
(c)
//
///:~
//:
1)
1995-2004
2",
<<
<fstream>
<iostream>
<iterator>
<set>
<string>
>
>>
All
Allison.
Bruce
C++,
C07:WordSet.cpp
Chuck
Eckel
From
Inc.
MindView,
Reserved.
Rights
See
Volume
argc,
argv[])
assure(source,
at
available
by
char*
code
copy(words.begin(),
cout
distributed
else
endl;
file
fileName)
fileName);
getchar();
if(argc
ifstream
in
int
main(int
namespace
of
ostream_iterator<string>(cout,
package
permissions
set<string>
source
source(fileName);
stated
std;
string
the
unique
use
using
void
while(source
with
word)
word;
wordSet("WordSet.cpp");
wordSet(argv[1]);
wordSet(const
words.end(),
words.insert(word);
words.size()
words:"
words;
www.MindView.net.
{
}
Number of unique words:89

这里唯一的区别
集合保存字符串而不是整数
这些单词被从一个文件中取出来
输出不仅显示出所有重复的单词都忽略掉
而且set的实现方式 
单词被自动排序


容器由值来保存对象
在某些时候希望容器存储一些指针
这些指针可以指向某一层次结构的对象
这样就可以利用类表现出的多态行为

//: C07:Stlshape.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.
// Simple shapes using the STL.
#include <vector>
#include <iostream>
using namespace std;

class Shape {
public:
  virtual void draw() = 0;
  virtual ~Shape() {};
};

class Circle : public Shape {
public:
  void draw() { cout << "Circle::draw" << endl; }
  ~Circle() { cout << "~Circle" << endl; }
};

class Triangle : public Shape {
public:
  void draw() { cout << "Triangle::draw" << endl; }
  ~Triangle() { cout << "~Triangle" << endl; }
};

class Square : public Shape {
public:
  void draw() { cout << "Square::draw" << endl; }
  ~Square() { cout << "~Square" << endl; }
};

int main() {
  typedef std::vector<Shape*> Container;
  typedef Container::iterator Iter;
  Container shapes;
  shapes.push_back(new Circle);
  shapes.push_back(new Square);
  shapes.push_back(new Triangle);
  for(Iter i = shapes.begin(); i != shapes.end(); i++)
    (*i)->draw();
  // ... Sometime later:
  for(Iter j = shapes.begin(); j != shapes.end(); j++)
    delete *j;
  getchar();
} ///:~


输出
Circle::draw
Square::draw
Triangle::draw
~Circle
~Square
~Triangle

类Shape Circle Square和Triangle的创建非常类似
Shape是一个抽象基类
定义了所有Shape类型的接口
派生类覆盖虚函数virtualdraw()以实现相应的操作

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值