看到网上很多使用iterator修改list列表中值的案例,发现都是定义的基本类型的list,没有定义类或者结构体类型的类,如果使用类或者结构体定义,是不能使用诸如*iter = "csdn";这样的修改方式的。
使用类定义的list改值需要(*it).n = 2;这样的修改方式。
源代码
#include<list>
#include <iostream>
using namespace std;
class cao
{
public:
cao();
~cao();
int n = 0;
private:
};
cao::cao()
{
}
cao::~cao()
{
}
int main()
{
list<cao>c;
list<cao>::iterator it;
cao temp1;
temp1.n = 1;
c.push_back(temp1);
it = c.begin();
(*it).n = 2;
cao temp2;
temp2 = c.front();
cout << temp2.n;
}
输出2
因为这个问题卡了半个月,我是猪鼻