广州建造网站公司,网站建设竞价托管服务,苏州哪家做网站便宜,北京网络网站建设公司本专栏目的
更新C/C的基础语法#xff0c;包括C的一些新特性
前言
initializer_list表达式、tuple元组、pair对组再C日常还是比较常用的#xff0c;尤其是对组在刷算法还是挺好用的#xff0c;这里做一个简介#xff1b;这三个语法结合C17的结构化绑定会更好用#xff…本专栏目的
更新C/C的基础语法包括C的一些新特性
前言
initializer_list表达式、tuple元组、pair对组再C日常还是比较常用的尤其是对组在刷算法还是挺好用的这里做一个简介这三个语法结合C17的结构化绑定会更好用结构化绑定后面会更新C语言后面也会继续更新知识点如内联汇编欢迎收藏 关注本人将会持续更新。 文章目录 std::initializer_list使用步骤 std::tuple创建获取元素值作为函数返回值 std::pair std::initializer_list
为了编写能够处理不同数量实参但是类型相同C11新标准提供了std::initializer_list的方法它提供了一种方便的方式来初始化容器或执行其他形式的初始化。std::initializer_list 主要用于构造函数和其他函数的参数列表中允许使用大括号 {} 包围的初始化列表来传递多个元素。
普通函数使用
void showMsg(const std::initializer_liststd::string list)
{for (auto n : list){cout n ;}cout endl;
}
showMsg({ hello,world,嘿嘿});构造函数使用
templatetypename T,size_t _size
class Array
{
public:Array() {}Array(const std::initializer_listT list) {size_t i 0;for (auto v : list) //必须使用这个{_arr[i] v;}}constexpr size_t size()const { return _size; }T operator[](int index){return _arr[index];}T _arr[_size]{ T() };
};Arrayint, 10 arr { 1,2,3,4,5,6 };
for (int i 0; i arr.size(); i)
{cout arr[i] ;
}注意 这个表达式只允许读不允许修改里面元素
相对于数组语法简单可以支持{}初始化也更安全了。
使用步骤
1、用花括号初始化器列表列表初始化一个对象其中对应构造函数接受一个std::initializer_list 参数 2、以花括号初始化器列表为赋值的右运算数或函数调用参数而对应的赋值运算符/函数接受 std::initializer_list 参数 3、绑定花括号初始化器列表到 auto 包括在范围 for 循环中
std::tuple 简介元组可以存储多个元素包括不同类型的元素
创建
void create()
{//1.模板std::tupleint, const char*, const char* tup(18, wy, wqy);//2.构造函数(推荐auto m_tup std::make_tuple(18, wy love, wqy);//3.右值引用auto m_right std::forward_as_tuple(18, wy love, wqy);
}获取元素值 可以通过std::get来获取
void print()
{auto m_tup std::make_tuple(18, wy, love, wqy);//1.get 获取可以改变值std::get0(m_tup) 19;std::cout std::get0(m_tup) std::get1(m_tup) std::get2(m_tup) std::get3(m_tup) std::endl;//2.tie 获取int age;const char* name;const char* word;const char* she;std::tie(age, name, word, she) m_tup;std::cout age name word she std::endl;std::string na;std::tie(std::ignore, std::ignore, na, std::ignore) m_tup; //得需要完整的参数个数//3.tie 还可以创建一个tuple 但是必须为左值auto m_tp std::tie(word, she);//4. std::tie_cat 连接auto new_tup std::tuple_cat(m_tup, m_tp);//输出 get / tiestd::cout std::get0(new_tup) std::get5(new_tup) std::endl;
}作为函数返回值
#includeiostream
#includetupleauto show()
{return std::make_tuple(1, 2, 3);
}int main()
{print();//作用可以做函数多个参数返回值int x, y, z;auto m_tup show();std::tie(x, y, z) show();std::cout x y z std::endl;return 0;
}std::pair
简介 这个用处还是很大的可以存储两个元素类似于KV存储。 常用操作
构建对组访问对组元素第一个.first第二个.second常用操作代码
#includeiostreamvoid createText();//用处
auto _with()
{return std::make_pair(1, 2);
}int main()
{createText();auto text _with();std::cout text.first text.second std::endl;return 0;
}void createText()
{//pair 对组 //first second//1. 模板构造std::pairstd::string, std::string m_pair(wy, wqy);//2. make_pairauto m_p std::make_pair(wqy, wy);输出std::cout m_pair.first m_pair.second std::endl; std::cout m_p.first m_p.second std::endl;
}注意对 对组 元素进行排序则默认按照第一个元素进行排序代码如下
#include iostream
#include algorithm
#include vector
using namespace std;int main()
{std::vectorstd::pairint, std::string kv{ {10, y},{19, x},{18, z},{1, b},{22,k} };sort(kv.begin(), kv.end());for (auto it : kv) {std::cout it.first it.second std::endl;}return 0;
}⛑ 结果 如果想要按照第二个排序则需要需要用介词函数什么是介词函数这个等到STL的时候在讲解吧代码如下
#include iostream
#include algorithm
#include vectorint main()
{std::vectorstd::pairint, std::string kv{ {10, y},{19, x},{18, z},{1, b},{22,k} };sort(kv.begin(), kv.end(), [](const std::pairint, std::string a, const std::pairint, std::string b) {return a.second b.second;});for (auto it : kv) {std::cout it.first it.second std::endl;}return 0;
}结果