STL中list中push_back(对象)保存对象的内部实现
2023-09-27 14:21:09 时间
STL中list中push_back(对象)保存对象的内部实现
1. 在容器中,存放的是对象拷贝
#include<iostream> #include<list> using namespace std; class A{ int i; static int num; public: A():i(0){ cout<<"A()" <<endl; num ++;} A(int ii):i(ii){ cout<<"A(int)" <<endl; num ++;} ~A(){ cout<< "~A" <<endl;} A(const A& a){ i = a.i; cout<<"A(const A&)"<<endl; num ++; } A& operator =(const A& a){ cout<<"operator="<<endl; i = a.i; return *this; } void print(){ cout<<i<<endl; } void printN(){ cout<< " num = "<<num<<endl; } }; int A::num = 0; int main(){ //A a(1); //A b = a; //A c; //c = a; //c.print(); //a.printN(); list<A> li; li.push_back(A(2)); list<A>::iterator it = li.begin(); (*it).printN(); } 输出结果: A(int)//A2临时对象
A(const A&)//li.push_back(A(2))调用其复制构造函数 ~A//离开临时对象作用域A调用其析构函数 num = 2 ~A //程序结束 从输出结果可以看出传到list中的是对象的拷贝。
相关文章
- Java反转List,逆序打印
- C-Lodop获取打印机列表Create_Printer_List
- Java List简单操作
- 数据分析-第三方库(工具包):Numpy【使用ndarray对象处理多维数组】【比Python原生list运算效率高:①内存块风格;②支持并行化运算;③底层用C编写,内部解除了GIL(全局解释器锁)】
- The Web server is configured to not list the contents of this directory.
- JAVA-HashMap集合value存储list集合对象、类/包装类对象--都是浅复制
- fastJson 之JSONObject.toJavaObject()方法不能解析嵌套自定义list对象
- 《coredump问题原理探究》Linux x86版7.9节list相关的iterator对象
- JSON 格式的转换: 数组、字符串、List集合、DataTable,实体对象
- MyBatis传入参数为list、数组、map写法
- list里面放的实体对象,页面用c:foreach应该怎么取?
- python list元素为dict时的排序
- 创建一个list 里面方对象的方法
- @Valid 注解校验提交的List(list 集合) javax.validation.Valid
- list、map、对象对应json的格式
- java List转变为逗号分隔的字符串(字符串去重)
- 一步一步学List Control控件的用法(第二步)----创建第一行
- Reorder List [leetcode] 这两种思路
- Python生成指定范围数字正数和负数列表(list)
- Scala综合练习_基于以下List集合实现词频统计
- [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树
- java List 按指定长度分割
- List集合中的对象按照某个字段去重实现