当前位置: 首页 > news >正文

网站怎么做子分类百度产品有哪些

网站怎么做子分类,百度产品有哪些,网站前端开发语言,苏州小程序开发公司前言 本期我们接着继续介绍C11的新特性,本期我们介绍的这个新特性是很多人都感觉抽象的语法!它就是可变的模板参数! 目录 前言 一、可变的模板参数 1.1可变的参数列表 1.2可变的参数包 1.3可变参数包的解析 • 递归展开解析 • 逗号…

前言

本期我们接着继续介绍C++11的新特性,本期我们介绍的这个新特性是很多人都感觉抽象的语法!它就是可变的模板参数!

目录

前言

一、可变的模板参数

1.1可变的参数列表

1.2可变的参数包

1.3可变参数包的解析

• 递归展开解析

• 逗号表达式解析

二、emplace系列接口


一、可变的模板参数

1.1可变的参数列表

对于可变参数列表,我们在C语言就见过!例如最典型的就是 printf / scanf 了!

我们在使用的时候,需要指定类型将不同类型/数量的类型都可以传递过去!

int main() 
{int a;double b;char c;printf("请输入一个整数、一个浮点数和一个字符: ");scanf("%d %lf %c", &a, &b, &c);printf("输入了: %d %lf %c\n", a, b, c);return 0;
}

1.2可变的参数包

C++98/03,类模板和函数模板中只能含固定的模板参数,C++11引入了可变的模板参数能够让你创建接受可变参数的函数模板和类模板!这无疑是一个巨大的改进!正是改变巨大,所以这块比较抽象,也比较晦涩难懂!本博客主要介绍的是函数模板的可变参数!

先来见一见基本的可变参数的函数模板

template <class ...Args>
void ShowList(Args... args)
{// ...
}

此时,我们就可以给这个函数模板传递任意类型、任意个数的参数,把这个前面带 ... 的参数统称为参数包Args模板的参数包args函数形参的参数包(Args... args的个数是0~N

一般为了提高传递参数的效率,可变参数的类型一般会被写成,万能引用(引用折叠)的形式:

template <class ...Args>
void ShowList(Args&&... args)
{// ...
}

由于可变模板的参数是可变的,即支持任意类型、任意数量的参数,所以可以这样写:

int main()
{ShowList(1);ShowList(1, 'a', 9.9);ShowList(1, "aaa", 9.9, 'b');ShowList(std::vector<int>(), std::list<double>(), 'zzz', 999, "aaaaaaa");return 0;
}

这也体现了可变模板参数的强大!

1.3可变参数包的解析

用可变参数的人爽了,但是解析参数包的过程其实是不简单的!下面是一种非常典型的错误栗子:

template<class ...Args>
void showList(Args... args)
{// 错误的解析参数方式int n = sizeof...(args);for (int i = 0; i < n; i++){// 获取具体的可变参数args[i];}
}

这里使用sizeof获取参数包args的大小是把...放到了args的前面,第一次看起来的时候有点别扭~!(个人感觉)

这种方式是符合我们的直觉的,但是他是错的!

这里简单理解就是编译器不支持这样写!sizeof是编译时操作符,而参数包不是和数组一样连续的,所以不可用下标访问!

• 递归展开解析

void ShowList() { cout << endl; }// 用于结束递归template<class T, class ...Args>
void ShowList(const T& t, Args&& ...args)
{cout << t << " ";ShowList(args...);
}

这里他是如何做的呢?我用一个简单的例子,第三个来画一下:

也就是编译器,在编译期间将上述的函数模板实例化成了很多的实例,在运行时进行了调用

• 逗号表达式解析

除了上述递归的展开的方式解析参数包以外,还有一种直接展开的方式,就是用逗号表达式解析!具体如下:

template<class T>
void Print(T& t)
{cout << t << endl;
}template<class ...Args>
void ShowList(Args&&... args)
{int arr[] = { (Print(args), 0)... };cout << endl;
}

OK,编译后他就是这个样子:

template<class ...Args>
void ShowList(Args&&... args)
{int arr[] = { (Print(1), 0), (Print('a'), 0), (Print(9,9), 0)};cout << endl;
}

为什么这里要写成 (Print(args), 0)... 呢?

我们知道,逗号表达式的结果是最后一项的值!这是主要是在展开的同时给arr数组赋值!

二、emplace系列接口

C++11 也在STL中引入了可变的模板参数,这一批接口被称为emlpace系列的接口/函数!

这里以list为例,进行介绍!

为了方便演示,我们玩还是使用我们前几期用的cp::string,所以我们把他给拿过来:

namespace cp
{class string{public:typedef char* iterator;iterator begin(){return _str;}iterator end(){return _str + _size;}string(const char* str = ""):_size(strlen(str)), _capacity(_size){cout << "string(char* str) -- 构造" << endl;_str = new char[_capacity + 1];strcpy(_str, str);}// s1.swap(s2)void swap(string& s){::swap(_str, s._str);::swap(_size, s._size);::swap(_capacity, s._capacity);}// 拷贝构造string(const string& s):_str(nullptr){cout << "string(const string& s) -- 拷贝构造" << endl;string tmp(s._str);swap(tmp);}// 赋值重载string& operator=(const string& s){cout << "string& operator=(string s) -- 赋值拷贝" << endl;string tmp(s);swap(tmp);return *this;}// 移动构造string(string&& s):_str(nullptr), _size(0), _capacity(0){cout << "string(string&& s) -- 移动构造" << endl;swap(s);}// 移动赋值string& operator=(string&& s){cout << "string& operator=(string&& s) -- 移动赋值" << endl;swap(s);return *this;}~string(){delete[] _str;_str = nullptr;}char& operator[](size_t pos){assert(pos < _size);return _str[pos];}void reserve(size_t n){if (n > _capacity){char* tmp = new char[n + 1];strcpy(tmp, _str);delete[] _str;_str = tmp;_capacity = n;}}void push_back(char ch){if (_size >= _capacity){size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;reserve(newcapacity);}_str[_size] = ch;++_size;_str[_size] = '\0';}//string operator+=(char ch)string& operator+=(char ch){push_back(ch);return *this;}const char* c_str() const{return _str;}private:char* _str;size_t _size;size_t _capacity; // 不包含最后做标识的\0};cp::string to_string(int value){cp::string str;bool flag = true;if (value < 0){flag = false;value = 0 - value;}while (value > 0){int x = value % 10;value /= 10;str += ('0' + x);}if (flag == false){str += '-';}std::reverse(str.begin(), str.end());return str;}
}
list<cp::string> lt;
// 分别让emplace_back和push_back都插入一个 左值 对象
cp::string s1("hello");
cout << "-------------------------------" << endl;
lt.push_back(s1);
cout << endl;
lt.emplace_back(s1);// 分别让emplace_back和push_back都插入一个 左值move后的 对象
cout << "-------------------------------" << endl;
lt.push_back(move(s1));
cout << endl;
lt.emplace_back(move(s1));// 分别让emplace_back和push_back都插入一个 右值 对象
cout << "-------------------------------" << endl;
lt.push_back(cp::string("6666")); 
cout << endl;
lt.emplace_back(cp::string("6666"));// 分别让emplace_back插入一个参数包   push_back都插入一个右值 对象
cout << "-------------------------------" << endl;
lt.push_back("6666");
cout << endl;
lt.emplace_back("6666");

看结果:

通过上面的栗子,我们可以得出结论:

1、当emplace和push系列都插入左值/move(左值)的结果是一样的(拷贝构造/移动构造)!

2、当emplace插入一个参数包,push系列都插入一个右值时,emplace是构造,而push系列是构造+移动构造!

• 为什么emplace系列传入参数包后是直接构造?

上面介绍了,emplace系列的是引用了模板的可变参数,所以他可以接受的是一个参数包!其实他的底层是一层层的传递下去,直接到构造那里,用参数包的参数直接构造的,所以就只有一次构造!

这其实就是,有人所说的emplace效率高的原因所在!就是直接使用emplace传递参数包可以少一次,移动构造!

所以,emplace系列的插入对象的优先级为

参数包 > 右值 > 左值

这里左值的代价较高,因为有一次的拷贝构造!右值其实和参数包的差别也还好,原因是移动构造的待见很轻!但是能用参数包的地方还是用参数包!以后就建议多使用emplace系列,因为emplace系列>=push系列 的效率!


OK,好兄弟,本期分享就到这里!我是cp我们下期再见~!


文章转载自:
http://postcode.xsfg.cn
http://page.xsfg.cn
http://marlberry.xsfg.cn
http://subacetate.xsfg.cn
http://edacious.xsfg.cn
http://regularly.xsfg.cn
http://palingenesist.xsfg.cn
http://forsooth.xsfg.cn
http://peiping.xsfg.cn
http://allochroic.xsfg.cn
http://koestler.xsfg.cn
http://fundamentalism.xsfg.cn
http://declinatory.xsfg.cn
http://samely.xsfg.cn
http://nonpeak.xsfg.cn
http://hackmatack.xsfg.cn
http://patriotism.xsfg.cn
http://pourparler.xsfg.cn
http://maraca.xsfg.cn
http://advisement.xsfg.cn
http://diluvium.xsfg.cn
http://calorifier.xsfg.cn
http://mckinley.xsfg.cn
http://antisymmetric.xsfg.cn
http://prodrome.xsfg.cn
http://pheidippides.xsfg.cn
http://teminism.xsfg.cn
http://catastrophe.xsfg.cn
http://poncho.xsfg.cn
http://footplate.xsfg.cn
http://whey.xsfg.cn
http://microlite.xsfg.cn
http://zealousness.xsfg.cn
http://truism.xsfg.cn
http://jotting.xsfg.cn
http://vacua.xsfg.cn
http://scaldingteass.xsfg.cn
http://tracheary.xsfg.cn
http://teacupful.xsfg.cn
http://swashbuckling.xsfg.cn
http://kibbutznik.xsfg.cn
http://disseat.xsfg.cn
http://naturalisation.xsfg.cn
http://falcate.xsfg.cn
http://strabismal.xsfg.cn
http://monotreme.xsfg.cn
http://hinterland.xsfg.cn
http://epigamic.xsfg.cn
http://allopatrically.xsfg.cn
http://parable.xsfg.cn
http://deign.xsfg.cn
http://wholly.xsfg.cn
http://thrall.xsfg.cn
http://luebke.xsfg.cn
http://craniometrist.xsfg.cn
http://shrubbery.xsfg.cn
http://generator.xsfg.cn
http://luau.xsfg.cn
http://particular.xsfg.cn
http://inbreathe.xsfg.cn
http://decrescendo.xsfg.cn
http://sided.xsfg.cn
http://liturgic.xsfg.cn
http://lactoovovegetarian.xsfg.cn
http://inurbane.xsfg.cn
http://telford.xsfg.cn
http://baggageman.xsfg.cn
http://suborning.xsfg.cn
http://fluoroplastic.xsfg.cn
http://sweetshop.xsfg.cn
http://mimi.xsfg.cn
http://forage.xsfg.cn
http://coyote.xsfg.cn
http://upshift.xsfg.cn
http://keelblock.xsfg.cn
http://prosector.xsfg.cn
http://phagolysis.xsfg.cn
http://scandic.xsfg.cn
http://narrative.xsfg.cn
http://biliary.xsfg.cn
http://dowry.xsfg.cn
http://phratry.xsfg.cn
http://fujian.xsfg.cn
http://tiddled.xsfg.cn
http://hexose.xsfg.cn
http://fibrinous.xsfg.cn
http://undershoot.xsfg.cn
http://gadgetry.xsfg.cn
http://anathematically.xsfg.cn
http://dyarchy.xsfg.cn
http://prelaw.xsfg.cn
http://entophyte.xsfg.cn
http://subsynchronous.xsfg.cn
http://burnisher.xsfg.cn
http://bdsa.xsfg.cn
http://leiotrichi.xsfg.cn
http://mengovirus.xsfg.cn
http://monamide.xsfg.cn
http://lucullian.xsfg.cn
http://drivability.xsfg.cn
http://www.hrbkazy.com/news/83339.html

相关文章:

  • 建议网站的方案seo短视频网页入口
  • 合肥网站建设久飞百度站长提交
  • 网站开发和app开发跨境电商怎么做
  • 国外最新创意产品网站网站查询ip
  • 深圳营销型网站seo网站检测
  • 怎样修改wordpress模板朔州网站seo
  • http:localhostwordpress宁波seo外包费用
  • 沛县徐州网站开发旅游网站的网页设计
  • 西安做义工网站百度搜索引擎排名规则
  • java短租网站开发全媒体运营师报考官网在哪里
  • 网站开发跟网页制作网络营销的好处和优势
  • 怎么做切片网站西安seo代理
  • 佛山做外贸网站哪家好seo排名优化app
  • HTML5网站建设案例营销网站建设软件下载
  • 宝鸡住房和城市建设局网站专业网站建设公司首选
  • wordpressμ宁波seo排名费用
  • 怎样做网站的轮播图片app开发
  • 网站遇到攻击时应该怎么做考研培训班集训营
  • 深圳设计家官网河北seo技术交流
  • 杭州市网站建设公司作品推广
  • 怎么做qq钓鱼网站家居seo整站优化方案
  • 我英文网站建设正规seo一般多少钱
  • 大气自适应网站源码网络推广方案的基本思路
  • b2c网站结构网站建设与管理就业前景
  • 长城建设投资有限公司网站百度竞价被换着ip点击
  • 移动网站建设推广剪辑培训班一般学费多少
  • 网站如何引入流量营销软件网站
  • 专业网站制作网络公司网络推广工作内容
  • 做标识的网站 知乎网络优化培训骗局
  • ps网站切图教程网站自然排名优化