////////////////////////////////////////
// 2018/04/17 20:54:41
// vector-push_back
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
template<class T>
class Name
{
private:
T name;
public:
Name(T t) :name(t){};
void print(){
cout << 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);
//unnamed object of the type Name
v.push_back(N("Linda"));
V::iterator it = v.begin();
while (it != v.end()){
(it++)->print();
}
cout << endl;
return 0;
}
/*
OUTPUT:
Robert Alex Linda
*/
vector-push_back
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C++---emplace_back VS push_back
允许你将参数直接传递给元素类型的构造函数,而不需要创建临时对象。这使得它更加高效,并且可以方便地在容器中构造自定义类型
c++ 开发语言 构造函数 #include 临时对象 -
C++ vector关于push_back()调用构造函数、析构函数的分析
vector多次调用push_back时会重复的多次的调用构造和析构函数的问题
c++ vector push_back 构造函数 析构函数 -
list-push_back
//////////////////////...
#include ios -
std::vector push_back报错Access violation
...
#include ios c++ 内置类型 自定义