重载赋值运算符不能完全拿到数据的问题
问题叙述1 在代码 b = c = a;一行b c 没有拿到a的全部数据,而是只拿到部分数据,有图为证this的数值明明已经被赋值,为何拿不到?
2 swap(VectorStudent.begin(),VectorStudent2.begin());一行的计算结果并没有实现两个vector的交换
望给位前辈能指点迷境!
// stl_swap3.cpp : Defines the entry point for the console application.
//
// stl_swap_2.cpp : Defines the entry point for the console application.
//
#pragma warning(once:4385)
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <algorithm>
#include <iostream>
using namespace std;
struct student
{
public:
int num;
int sex;
int age;
public:
student(const int nNum,const int nSex,const int nAge)
{
num = nNum;
sex = nSex;
age = nAge;
}
student()
{}
student& operator = (const student& aStu)
{
if (this == &aStu)
{}
else
{
this->age = aStu.age;
this->sex = aStu.sex;
this->age = aStu.age;
}
return *this;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
//student Array[5] = {{1,0,13},{2,0,14},{3,1,10},{4,0,11},{5,1,35}};
/*student Array2[5] = {{1,0,23},{2,0,24},{3,1,20},{4,0,21},{5,1,15}};
vector<student> VectorStudent(5),VectorStudent2(5);
for(short i = 0; i < 5; i++)
{
VectorStudent[i] = (student)Array[i];
VectorStudent2[i] = (student)Array2[i];
}*/
vector<student> VectorStudent(5),VectorStudent2(5);
VectorStudent[0].num = 1;
VectorStudent[0].sex = 0;
VectorStudent[0].age = 13;
VectorStudent[1].num = 2;
VectorStudent[1].sex = 0;
VectorStudent[1].age = 14;
VectorStudent[2].num = 3;
VectorStudent[2].sex = 1;
VectorStudent[2].age = 10;
VectorStudent[3].num = 4;
VectorStudent[3].sex = 0;
VectorStudent[3].age = 11;
VectorStudent[4].num = 5;
VectorStudent[4].sex = 0;
VectorStudent[4].age = 35;
VectorStudent2[0].num = 1;
VectorStudent2[0].sex = 0;
VectorStudent2[0].age = 23;
VectorStudent2[1].num = 2;
VectorStudent2[1].sex = 0;
VectorStudent2[1].age = 24;
VectorStudent2[2].num = 3;
VectorStudent2[2].sex = 1;
VectorStudent2[2].age = 20;
VectorStudent2[3].num = 4;
VectorStudent2[3].sex = 0;
VectorStudent2[3].age = 21;
VectorStudent2[4].num = 5;
VectorStudent2[4].sex = 1;
VectorStudent2[4].age = 15;
student a = VectorStudent2[4];
a = a;
student b,c;
b = c = a;
swap(VectorStudent.begin(),VectorStudent2.begin());
for each(student elem in VectorStudent)
{
cout << elem.num << " "<< elem.age<< " " << elem.sex<< " "<< endl;
}
for each(student elem in VectorStudent2)
{
cout << elem.num << " "<< elem.age<< " " << elem.sex<< " "<< endl;
}
return 0;
}