temptemp4.cpp

来自「很经典的书籍」· C++ 代码 · 共 39 行

CPP
39
字号
//: C05:TempTemp4.cpp
//{-bor}
//{-msc}
// Passes standard sequences as template arguments
#include <iostream>
#include <list>
#include <memory>  // Declares allocator<T>
#include <vector>
using namespace std;

template<class T, template<class U, class = allocator<U> >
                    class Seq>
class Container {
  Seq<T> seq; // Default of allocator<T> applied implicitly
public:
  void push_back(const T& t) { seq.push_back(t); }
  typename Seq<T>::iterator begin() { return seq.begin(); }
  typename Seq<T>::iterator end() { return seq.end(); }
};

int main() {
  // Use a vector
  Container<int, vector> theData;
  theData.push_back(1);
  theData.push_back(2);
  for(vector<int>::iterator p = theData.begin();
      p != theData.end(); ++p) {
    cout << *p << endl;
  }
  // Use a list
  Container<int, list> theOtherData;
  theOtherData.push_back(3);
  theOtherData.push_back(4);
  for(list<int>::iterator p2 = theOtherData.begin();
      p2 != theOtherData.end(); ++p2) {
    cout << *p2 << endl;
  }
} ///:~

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?