【发布时间】:2014-03-27 13:58:50
【问题描述】:
我在 C++ 中有以下代码:
typedef struct
{
int age;
int roomNumber;
} Student;
Student student;
student.age = 20;
student.roomNumber = 10;
vector<Student> studentV;
student.push_back(student);
student.push_back(student);
student.push_back(student);
Student student1[3];
int i;
for(vector<Student>::const_iterator iterator = studentV.begin();
iterator != studentV.end(); ++iterator,++i)
{
memcpy(&student1[i], iterator, sizeof(Student));
}
它为 memcpy 部分显示以下消息:
error: cannot convert 'std::vector<Student>::const_iterator {aka __gnu_cxx::__normal_iterator<const Student*, std::vector<Student> >}' to 'const void*' for argument '2' to 'void* memcpy(void*, const void*, size_t)'
问题是什么以及如何解决?难道迭代器就不能这样复制了吗?
【问题讨论】:
-
只需使用
std::copy。 -
一个迭代器不是一个指针..你可以得到迭代器
(*iterator)的值pointed然后得到它的地址&(*iterator)...还没有试过了。
标签: c++ vector iterator memcpy