【c++】——vector one example

【c++】——vector one example,第1张

example

解释

  • vector在resize的时候,就会初始化变量
  • vector[0] = input, 这里会调用编译器为类添加的默认赋值拷贝函数

参考:C++11新特性4 - 编译器生成的函数

code

#include 
#include 
#include 
#include 

class Mat
{
public:
    Mat(): w(0)
    {
        printf("mat init... \n");
    }

    Mat(int size): w(size)
    {

    }

    int w;
};

int main(int argc, char **argv){
    
    std::vector<Mat> blob_mat;
    printf("000000 \n");
    blob_mat.resize(5);
    printf("111111 \n");

    Mat input(3);
    printf("input.w = %d \n", input.w);

    blob_mat[1] = input;        // 编译器 默认添加的 复制构造函数

    Mat blob;
    for(int i=0; i<5; i++)
    {
        blob = blob_mat[i];
        printf("blob.w: %d \n", blob.w);
    }
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/662316.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-18
下一篇2022-04-18

发表评论

登录后才能评论

评论列表(0条)

    保存