C++类(五)—— 重新审视auto、比较三种for循环的效率、自定义的类如何使用新版本的 for(auto i : v)

容易犯的小错误 

void autoValue(bool age) {
    //此时,该age会将传入的参数age覆盖掉
    auto age = 10;    
}
auto的使用范例
static void autoValue() {
    auto age = 10;
    auto name = std::string("Yt");
    auto height = 160.0f;
    auto weight = 72.0;
    //typeid是C++中的关键字。typeid的输出结果与平台相关
    std::cout << "age is type " << typeid(age).name() << std::endl;
    std::cout << "name is type " << typeid(name).name() << std::endl;
    std::cout << "height is type " << typeid(height).name() << std::endl;
    std::cout << "weight is type " << typeid(weight).name() << std::endl;
}
 
static void autoPointer() {
    auto age = new int(10);  //age类型  int*
    auto name = "Yt";  //name的类型为const char*
    auto height = new float(160.0f);
    auto weight = new double(72.0);
 
    std::cout << "age is type " << typeid(age).name() << std::endl;
    std::cout << "name is type " << typeid(name).name() << std::endl;
    std::cout << "height is type " << typeid(height).name() << std::endl;
    std::cout << "weight is type " << typeid(weight).name() << std::endl;
}
 

比较三种for循环的效率(for (auto v : group) 、for (size_t i = 0; i < group.size();  ++i) 、for (std::vector<int>::const_iterator iter = group.begin();iter != group.end(); ++iter))
static void newVersionFor() {
 
    int ids[] = { 1, 2, 3, 4, 5 };
    std::cout << "new version ";
    for (auto v : ids) {
        std::cout << v << " ";
    }
    std::cout << std::endl;  // "\n";
 
    std::cout << "old version ";
    // old version
    for (int i = 0; i < sizeof(ids) / sizeof(int); ++i) {
        std::cout << ids[i] << " ";
    }
    std::cout << std::endl;  // "\n";
 
    std::vector<int> group;
    for (int i = 0; i < 4; ++i) group.push_back(i);
    /*
    从效率上分析这三种遍历:最快的是for (auto v : group) 
    for (size_t i = 0; i < group.size();  ++i) 
    每次循环都要求一下group.size(),进入循环之后还要找一下group[i]的位置
    for (std::vector<int>::const_iterator iter = group.begin();
        iter != group.end(); ++iter) {
        std::cout << *iter << " ";
    }
    每次循环的时候都要比较一下iter != group.end(),
    并且还要把相应位置的值(即*iter)提取出来
    */
 
    // auto version
    for (std::vector<int>::const_iterator iter = group.begin();
        iter != group.end(); ++iter) {
        std::cout << *iter << " ";
    }
    std::cout << std::endl;
 
    for (size_t i = 0; i < group.size(); ++i) {
        std::cout << group[i] << " ";
    }
    std::cout << std::endl;
 
    std::cout << "vector verion : ";
    for (auto v : group) {
        v = v * v;
    }
    std::cout << std::endl;
 
    for (auto& v : group) { //auto求每个元素引用的例子
        v = v * v;
    }
 
    //const引用可以防止对传入的参数进行误操作。防止不小心把参数的值修改
    for (const auto& v : group) {}
}
 

自定义的类如何使用新版本的 for(auto i : v)
//自定义的类如何使用新版本的for(auto i : v)
class MyVector {
public:
    using GroupType = std::vector<int>;
    typedef std::vector<int> GroupType;
    //只需要定义一下这两个就可以了
    GroupType::iterator begin() { return m_group.begin();}
    GroupType::iterator end() { return m_group.end();}
private:
    GroupType m_group;
};
或者可以这么写

class MyVector {
public:
    using GroupType = std::vector<int>;
    typedef std::vector<int> GroupType;
    //GroupType::iterator begin() { return m_group.begin();}
    //GroupType::iterator end() { return m_group.end();}
 
    GroupType m_group;
};
 
MyVector::GroupType::iterator begin(MyVector& v) {
    return v.m_group.begin();
}
 
MyVector::GroupType::iterator end(MyVector& v) {
    return v.m_group.end();
}
 (1731条消息) C++类(五)—— 重新审视auto、比较三种for循环的效率、自定义的类如何使用新版本的 for(auto i : v)_c++ auto 效率_王小闹儿的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值