c++的fill函数使用注意点

来源:互联网 发布:js正则匹配字符串 编辑:程序博客网 时间:2024/06/02 01:37

今天在调试程序的习惯用fill函数初始化一些数组,

初始化一维数组

 #include <cstdio>
 #include <iostream>
 using namespace std;
 int main(){
  int a[9][9];
 
  printf("%p\n",a);
  printf("%p\n",a[0]);
  printf("%p\n",&a[0][0]);
  fill(a,a+9*9,8);
  return 0;
 }

点击编译时程序跳转至


程序在编译过程中跳转至stl_algobase.h 704行,说明程序调用了704行的代码时出现错误


通过查找发现,fill参数类型不匹配  (http://www.cplusplus.com/reference/algorithm/fill/?kw=fill)

故不能简单的认为fill是通过地址偏移赋值的


由图2可知,a,a[0],a[0][0]三者的地址是一样的,但是三者的类型不一样(int **,int *,int),传给fill的参数应该为int *.

原创粉丝点击