加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C++11 Auto

发布时间:2020-12-15 04:50:29 所属栏目:百科 来源:网络整理
导读:// auto.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include "pch.h" #include #include #include using namespace std; class Foo { public : ?? ?static int get(void) ?? ?{ ?? ??? ?return 0; ?? ?} }; class Bar? { public: ?? ?

// auto.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

//

#include "pch.h"

#include

#include

#include

using namespace std;

class Foo

{

public :

?? ?static int get(void)

?? ?{

?? ??? ?return 0;

?? ?}

};

class Bar?

{

public:

?? ?static const char* get(void)

?? ?{

?? ??? ?return "0";

?? ?}

};

template

void func(void)

{

?? ?auto val = A::get();

}

int main()

{

?? ?auto ?x = 5;?? ??? ??? ??? ??? ?// OK: x==> int?

?? ?auto pi = new auto(1);?? ??? ??? ?// OK: pi==>int*

?? ?const auto *v = &x,u = 6;?? ??? ?//OK: v ==>const int*,u==>const int?

?? ?static auto y = 0.0;?? ??? ??? ?//OK: y ==>double

?? ?// auto int r ; error

?? ?// auto s; error

?? ?int xx = 0;

?? ?auto* a = &xx;?? ?//a==>int*,auto==>int

?? ?auto b = &xx;?? ?//b==>int*,auto==>int*

?? ?auto &c = xx;?? ?//c==>int&,auto==>int

?? ?auto d = c;?? ??? ?//d==>int,auto==>int

?? ?const auto e = xx;?? ?//e==>const int?

?? ?auto f = e;?? ??? ??? ?//f==>int

?? ?const auto& g = xx;?? ??? ?//g==>const int&

?? ?auto& h = g;?? ??? ??? ?//f==>const int&

?? ?//auto limits

?? ?//1.can't be a parameter of a function

?? ?//2.can't be used in a non-static variable member

?? ?//3.can't define an array

?? ?//4.can't infer the template's parameter

?? ?// stl container example

?? ?/*

?? ?map resMap;

?? ?map::iterator it = resMap.begin();

?? ?for (;it != resMap.end();++it)

?? ?{

?? ?}

?? ?*/

?? ?mapresMap;

?? ?for (auto it=resMap.begin();it != resMap.end();++it)

?? ?{

?? ?}

?? ?/*

?? ?unordered_multimapresultMap;

?? ?unordered_multimap::iterator range = resultMap.equal_range(key);

?? ?*/

?? ?unordered_multimapmap;

?? ?//auto range = map.equal_range(key);

?? ?// a new sample auto simple the function

?? ?func();

?? ?func();

?? ?return 0;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读