zoukankan      html  css  js  c++  java
  • vector作为参数的三种传参方式

    c++中常用的vector容器作为参数时,有三种传参方式,分别如下(为说明问题,用二维vector):

    • function1(std::vector<std::vector<int> > vec),传值
    • function2(std::vector<std::vector<int> >& vec),传引用
    • function3(std::vector<std::vector<int> >* vec),传指针

    注意,三种方式分别有对应的const形式,不在此讨论。

    三种方式对应的调用形式分别为:

    • function1(vec),传入值
    • function2(vec),传入引用
    • function3(&vec),传入地址

    三种方式的效果分别为:

    • 会发生拷贝构造
    • 不会发生拷贝构造
    • 不会发生拷贝构造

    验证程序:

      1 #include <iostream>
      2 #include <vector>
      3 
      4 using namespace std;
      5 
      6 void function1(std::vector<std::vector<int> > vec)
      7 {
      8     cout<<"-----------------------------------------"<<endl;
      9     //打印vec的地址
     10     cout<<"function1.&vec:"<<&vec<<endl;
     11     //打印vec[i]的地址(即第一层vector的地址)
     12     cout<<"function1.&vec[i]:"<<endl;
     13     for(int i=0;i<2;i++)
     14         cout<<&vec[i]<<endl;
     15     //打印vec的各元素地址
     16     cout<<"function1.&vec[i][j]:"<<endl;
     17     for(int i=0;i<2;i++)
     18     {
     19         for(int j=0;j<3;j++)
     20             cout<<&vec[i][j]<<" ";
     21         cout<<endl;
     22     }
     23     cout<<"---------------------------"<<endl;
     24     //打印vec的各元素值
     25     cout<<"function1.vec[i][j]:"<<endl;
     26     for(int i=0;i<2;i++)
     27     {
     28         for(int j=0;j<3;j++)
     29             cout<<vec[i][j]<<" ";
     30         cout<<endl;
     31     }
     32 }
     33 void function2(std::vector<std::vector<int> >& vec)
     34 {
     35     cout<<"-----------------------------------------"<<endl;
     36     //打印vec的地址
     37     cout<<"function2.&vec:"<<&vec<<endl;
     38     //打印vec[i]的地址(即第一层vector的地址)
     39     cout<<"function2.&vec[i]:"<<endl;
     40     for(int i=0;i<2;i++)
     41         cout<<&vec[i]<<endl;
     42     //打印vec的各元素地址
     43     cout<<"function2.&vec[i][j]:"<<endl;
     44     for(int i=0;i<2;i++)
     45     {
     46         for(int j=0;j<3;j++)
     47             cout<<&vec[i][j]<<" ";
     48         cout<<endl;
     49     }
     50     cout<<"---------------------------"<<endl;
     51     //打印vec的各元素值
     52     cout<<"function2.vec[i][j]:"<<endl;
     53     for(int i=0;i<2;i++)
     54     {
     55         for(int j=0;j<3;j++)
     56             cout<<vec[i][j]<<" ";
     57         cout<<endl;
     58     }
     59 
     60 }
     61 void function3(std::vector<std::vector<int> > *vec)
     62 {
     63     cout<<"-----------------------------------------"<<endl;
     64     //打印vec的地址
     65     cout<<"function3.&vec:"<<vec<<endl;
     66     //打印vec[i]的地址(即第一层vector的地址)
     67     cout<<"function3.&vec[i]:"<<endl;
     68     for(int i=0;i<2;i++)
     69         cout<<&(*vec)[i]<<endl;
     70     //打印vec的各元素地址
     71     cout<<"function3.&vec[i][j]:"<<endl;
     72     for(int i=0;i<2;i++)
     73     {
     74         for(int j=0;j<3;j++)
     75             cout<<&(*vec)[i][j]<<" ";
     76         cout<<endl;
     77     }
     78     cout<<"---------------------------"<<endl;
     79     //打印vec的各元素值
     80     cout<<"function3.vec[i][j]:"<<endl;
     81     for(int i=0;i<2;i++)
     82     {
     83         for(int j=0;j<3;j++)
     84             cout<<(*vec)[i][j]<<" ";
     85         cout<<endl;
     86     }
     87 }
     88 
     89 int main()
     90 {
     91     //创建2*3的vector容器v,初始值均初始化为0 1 2 1 2 3
     92     std::vector<std::vector<int> > v(2,std::vector<int>(3,0));
     93     for(int i=0;i<2;i++)
     94     {
     95         for(int j=0;j<3;j++)
     96             v[i][j]=i+j;
     97     }
     98 
     99     //打印v的地址
    100     cout<<"&v:"<<&v<<endl;
    101     //打印v[i]的地址(即第一层vector的地址)
    102     cout<<"&v[i]:"<<endl;
    103     for(int i=0;i<2;i++)
    104         cout<<&v[i]<<endl;
    105     //打印v的各元素地址
    106     cout<<"&v[i][j]:"<<endl;
    107     for(int i=0;i<2;i++)
    108     {
    109         for(int j=0;j<3;j++)
    110             cout<<&v[i][j]<<" ";
    111         cout<<endl;
    112     }
    113 
    114     cout<<"---------------------------"<<endl;
    115     //打印v的各元素值
    116     cout<<"v[i][j]:"<<endl;
    117     for(int i=0;i<2;i++)
    118     {
    119         for(int j=0;j<3;j++)
    120             cout<<v[i][j]<<" ";
    121         cout<<endl;
    122     }
    123 
    124     function1(v);
    125     function2(v);
    126     function3(&v);
    127 
    128     return 0;
    129 }

    输出(为便于观察,简单处理了一下效果):

    简而言之,vector的内部存储模型是这个样子(以main()函数中的v为例):

     关于12个字节的问题,请参考博客http://blog.csdn.net/kangroger/article/details/38386099

    『注:本文来自博客园“小溪的博客”,若非声明均为原创内容,请勿用于商业用途,转载请注明出处http://www.cnblogs.com/xiaoxi666/』
  • 相关阅读:
    javascript中的console.log有什么作用?
    在线js调试工具JSbin、jsFiddle
    mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)字符串拼接cancat实战例子
    处理内容有&特殊字符thinkphp返回xml无法解析的问题<![CDATA[xxx]]>
    checkbox的readonly不起作用的解决方案
    jquery-easyui combobox combogrid 级联不可编辑实例
    表格行的全选与单选
    表格与ckeckbox的全选与单选
    隐藏与显示铵钮
    判断字符是否包含有特殊字符
  • 原文地址:https://www.cnblogs.com/xiaoxi666/p/6843211.html
Copyright © 2011-2022 走看看