作者 CppLive | 发表于 2011-06-01
分章分类 : C++, 标准模板库
标签: C++, STL, 容器
1、push_back——将某个元素从容器后端入栈
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
template <class T>
class Name
{
public:
Name(T t) : name(t) {}
void print()
{
cout << name << " ";
}
private:
T name;
};
//=============================
int main ()
{
typedef Name<string> N;
typedef vector<N> V;
V v;
N n1("Robert");
N n2("Alex");
v.push_back(n1);
v.push_back(n2);
v.push_back(N("Linda"));
V::iterator It = v.begin();
while ( It != v.end() )
(It++)->print();
cout << endl;
return 0;
}
运行结果:
// Robert Alex Linda
阅读全文