加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在C中memcpy迭代元素?

发布时间:2020-12-16 09:51:24 所属栏目:百科 来源:网络整理
导读:我在C中有以下代码: typedef struct { int age; int roomNumber;} Student;Student student;student.age = 20;student.roomNumber = 10;vectorStudent studentV;student.push_back(student);student.push_back(student);student.push_back(student);Student
我在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)'

有什么问题以及如何解决?迭代器不能像这样复制吗?

解决方法

memcpy的第二个参数应该是Student对象的地址,所以你应该使用& * iterator而不是iterator.由于运算符重载,这两者不相等. 但我建议不要使用memcpy.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读