引用参数的不同种类

1. 示例一

#include <iostream>
#include <string>

struct Obj{
    Obj(){
        std::cout << "Ctor called.\n";
    }
    Obj(const Obj&){
        std::cout << "Copy ctor called.\n";    
    }
    ~Obj(){
        std::cout << "Dtor called.\n";
    }
};

void test1(const Obj& obj){
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

template<typename T>
void test2(const T& obj){
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

void test3(Obj& obj){
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

template<typename T>
void test4(T& obj){
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

void test5(Obj&& obj){
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

// 转发引用(万能引用)
template<typename T>
void test6(T&& obj){
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

int main () {
    Obj obj;
    const Obj obj2;
    
    test1(obj);
    test1(obj2);
    test1(Obj{});

    std::cout << std::string(30, '-') << std::endl;

    test2(obj);
    test2(obj2);
    test2(Obj{});

    std::cout << std::string(30, '-') << std::endl;

    test3(obj);
    //test3(obj2);

    test4(obj);
    test4(obj2);     // T&接受const Obj
    //test4(Obj{});  // T&不可能接受右值

    std::cout << std::string(30, '-') << std::endl;

    test5(Obj{});
    std::cout << std::string(30, '-') << std::endl;

    test6(obj);
    test6(obj2);
    test6(Obj{});

    std::cout << std::string(30, '-') << std::endl;
}

输出结果:

Ctor called.
Ctor called.
void test1(const Obj&)
void test1(const Obj&)
Ctor called.
void test1(const Obj&)
Dtor called.
------------------------------
void test2(const T&) [with T = Obj]
void test2(const T&) [with T = Obj]
Ctor called.
void test2(const T&) [with T = Obj]
Dtor called.
------------------------------
void test3(Obj&)
void test4(T&) [with T = Obj]
void test4(T&) [with T = const Obj]
------------------------------
Ctor called.
void test5(Obj&&)
Dtor called.
------------------------------
void test6(T&&) [with T = Obj&]
void test6(T&&) [with T = const Obj&]
Ctor called.
void test6(T&&) [with T = Obj]
Dtor called.
------------------------------
Dtor called.
Dtor called.

2. 示例二

#include <iostream>
#include <string>

struct Obj{
    Obj(){
        std::cout << "Ctor called.\n";
    }
    Obj(const Obj&){
        std::cout << "Copy ctor called.\n";    
    }
    ~Obj(){
        std::cout << "Dtor called.\n";
    }
};

void test(const Obj& obj){
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

void test(Obj& obj){
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

void test(Obj&& obj){
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

template<typename T>
void verifyValueCategory(T&& obj){
    if(std::is_lvalue_reference_v<T>){
        std::cout << "T is a lvalue reference\n";
    }
    else if(std::is_rvalue_reference_v<T>){
        std::cout << "T is a rvalue reference\n";
    }
    else {
        std::cout << "T is not a reference\n";
    }
    test(std::forward<T>(obj));
}

template<typename T>
void verifyValueCategoryNoForward(T&& obj){
    if(std::is_lvalue_reference_v<T>){
        std::cout << "T is a lvalue reference\n";
    }
    else if(std::is_rvalue_reference_v<T>){
        std::cout << "T is a rvalue reference\n";
    }
    else {
        std::cout << "T is not a reference\n";
    }
    test(obj);
}

int main () {
    Obj obj;
    const Obj obj2;

    std::cout << std::string(30, '-') << std::endl;

    verifyValueCategory(obj);     // void test(Obj&)
    verifyValueCategory(obj2);    // void test(const Obj&)
    verifyValueCategory(Obj{});   // void test(Obj&&)

    std::cout << std::string(30, '-') << std::endl;

    verifyValueCategoryNoForward(obj);
    verifyValueCategoryNoForward(obj2);
    verifyValueCategoryNoForward(Obj{});  // 这里会有问题,会调用左值引用的版本而不是右值引用的版本

    std::cout << std::string(30, '-') << std::endl;

    Obj&& r = Obj{};
    Obj&& r1 = Obj{};
    test(r);  // r是右值引用,但是r是变量因此属于左值,所以会调用void test(Obj&)的版本
    test(std::move(r));
    test(std::forward<decltype(r1)>(r1));

    std::cout << std::string(30, '-') << std::endl;
}

输出结果:

Ctor called.
Ctor called.
------------------------------
T is a lvalue reference
void test(Obj&)
T is a lvalue reference
void test(const Obj&)
Ctor called.
T is not a reference
void test(Obj&&)
Dtor called.
------------------------------
T is a lvalue reference
void test(Obj&)
T is a lvalue reference
void test(const Obj&)
Ctor called.
T is not a reference
void test(Obj&)
Dtor called.
------------------------------
Ctor called.
Ctor called.
void test(Obj&)
void test(Obj&&)
void test(Obj&&)
------------------------------
Dtor called.
Dtor called.
Dtor called.
Dtor called.
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在GEE中,随机森林分类器的参数设置可以根据实际需求进行调整。根据引用的代码,可以看到以下参数设置: 1. numTrees:随机森林中决策树的数量。在该代码中,numTrees的取值范围是从5到50,步长为5,即每次增加5个决策树进行分类。 2. 训练集和标签:在代码中使用了trainingPartition和classProperty来指定训练集和标签。其中,trainingPartition代表训练集,classProperty代表标签。 3. inputProperties:表示输入图像的波段,用于训练分类器。 另外,根据引用的代码,还可以看到以下参数设置: 1. 随机森林中决策树的数量:在该代码中,决策树的数量为20。 2. 图像分类结果的可视化参数:使用了min、max和palette来设置分类结果的可视化参数。 需要注意的是,具体的参数设置需要根据数据集的特点和具体任务进行调整,以获得最佳的分类效果。参考以上代码和参数设置可以作为起点,并根据实际需求进行调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [GEE东拼西凑之随机森林分类](https://blog.csdn.net/qq_50291870/article/details/129391880)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值