C++17 新特性

一、C++语言特性

结构化绑定

一个用于解构初始化的提案,允许编写 auto [ x, y, z ] = expr;,其中 expr 的类型是一个类似元组的对象,其元素将绑定到变量 x、y 和 z(这些变量由该构造声明)。类似元组的对象包括 std::tuple、std::pair、std::array 和聚合结构。

复制代码
using Coordinate = std::pair<int, int>;
Coordinate origin() 
{
    return Coordinate{0, 0};
}
 
const auto [ x, y ] = origin();
x; // == 0
y; // == 0

std::unordered_map<std::string, int> mapping {
  {"a", 1},
  {"b", 2},
  {"c", 3}
};
 
// 按引用解构
for (const auto& [key, value] : mapping) {
  // 对 key 和 value 进行操作
}

queue<pair<int, int>> que;
for (auto& source : sources) 
{
    que.push(source);
}
auto [x, y] = que.front(); //取出队首元素

二、STD标准库特性

std::filesystem

新的 std::filesystem 库提供了一种标准的方式来:操作文件、目录和文件系统中的路径。

以下是一个大文件被复制到临时路径的示例,前提是存在足够的空间:

复制代码
const auto bigFilePath {"bigFileToCopy"};
if (std::filesystem::exists(bigFilePath)) //路径是否存在
{
	const auto bigFileSize {std::filesystem::file_size(bigFilePath)};//文件系统容量大小
	std::filesystem::path tmpPath {"/tmp"};//路径
	
	if (std::filesystem::space(tmpPath).available > bigFileSize) //文件系统可用空间
	{
		std::filesystem::create_directory(tmpPath.append("example"));//创建目录
		std::filesystem::copy_file(bigFilePath, tmpPath.append("newFile"));//文件copy
	}
}

1

https://blog.csdn.net/zxf347085420/article/details/150486169?sharetype=blogdetail&sharerId=150486169&sharerefer=PC&sharesource=zxf347085420&spm=1011.2480.3001.8118

C++ 第三阶段 新标准库组件 - 第二节:std::filesystem(文件系统操作)-CSDN博客

2

原文链接:https://blog.csdn.net/qq_29426201/article/details/147447716