在使用<algorithm>中的fill()、fill_n()、generate()和generate_n()函数
模板向序列容器 vector list和deque中填充数据
已经看到有用
然而 实现都使用operator=赋值的方式将值放进序列容器
而关联式容器中添加对象的方式是使用它们各自的成员函数insert()
//: C07:assocGen.h
// 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.
// The fill_n() and generate_n() equivalents
// for associative containers.
#ifndef ASSOCGEN_H
#define ASSOCGEN_H
template<class Assoc, class Count, class T>
void assocFill_n(Assoc& a, Count n, const T& val) {
while(n-- > 0)
a.insert(val);
}
template<class Assoc, class Count, class Gen>
void assocGen_n(Assoc& a, Count n, Gen g) {
while(n-- > 0)
a.insert(g());
}
#endif // ASSOCGEN_H ///:~
没有使用迭代器
容器类自身被传递了
进行填充和产生map和set两个容器
创建一些模板化的发生器
//: C07:SimpleGenerators.h
// 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.
// Generic generators, including one that creates pairs.
#include <iostream>
#include <utility>
// A generator that increments its value:
template<typename T> class IncrGen {
T i;
public:
IncrGen(T ii) : i(ii) {}
T operator()() { return i++; }
};
// A generator that produces an STL pair<>:
template<typename T1, typename T2> class PairGen {
T1 i;
T2 j;
public:
PairGen(T1 ii, T2 jj) : i(ii), j(jj) {}
std::pair<T1,T2> operator()() {
return std::pair<T1,T2>(i++, j++);
}
};
namespace std {
// A generic global operator<< for printing any STL pair<>:
template<typename F, typename S> ostream&
operator<<(ostream& os, const pair<F,S>& p) {
return os << p.first << "\t" << p.second << endl;
}
} ///:~
两个发生器都希望T可以进行增1操作
无论用什么来进行初始化
仅使用operator++来产生新的值
最后函数是个一般用于输出流ostream的操作符operator<<
假定pair的每一个元素都支持流操作符operator<<
因此任何pair都能被打印
//: C07:AssocInserter.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.
// Using an insert_iterator so fill_n() and generate_n()
// can be used with associative containers.
#include <iterator>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include "SimpleGenerators.h"
using namespace std;
int main() {
set<int> s;
fill_n(inserter(s, s.begin()), 10, 47);
generate_n(inserter(s, s.begin()), 10,
IncrGen<int>(12));
copy(s.begin(), s.end(),
ostream_iterator<int>(cout, "\n"));
map<int, int> m;
fill_n(inserter(m, m.begin()), 10, make_pair(90,120));
generate_n(inserter(m, m.begin()), 10,
PairGen<int, int>(3, 9));
copy(m.begin(), m.end(),
ostream_iterator<pair<int,int> >(cout,"\n"));
getchar();
} ///:~
输出
12
13
14
15
16
17
18
19
20
21
47
3 9
4 10
5 11
6 12
7 13
8 14
9 15
10 16
11 17
12 18
90 120
传递给inserter的第2个参数是一个迭代器
是最佳化的,暗示可以帮助较快地进行插入
因为insert_iterator可以用于很多不同类型的容器