《Beginning C++17》-学习笔记-Chapter 09-Functions Templates

C++(17):any C++17增加了any类型,通过any声明的变量可以保存任意类型的值: 可见通过any可以定义类似于动态语言一样的动态绑定的变量。通过成员函数type()确定其运行时的实际类型: 可以通过reset()等方式将其值清空,清空后type()返回void,并可以通过has_value进行检测: 如果初始化时的字面量值无法确切的表明其类型,可以通过指定类型或in_place_type进行赋值: any定义的变量不能随便的访问,必须通过any_cast将其转换为合适的类型后,才能读取或操作其值,这一 阅读详情
// Using a function template
#include <iostream>
#include <string>
#include <vector>

template<typename T> T larger(T a, T b);    // Function template prototype

// a specialization of the template
template <>
int* larger<int*>(int* a, int* b)
{
	return *a > *b ? a : b;
}


int* larger(int* a, int* b); // Function overloading the larger template

//define a template that overloads the larger() template above

template <typename T>
T larger(const T data[], size_t count)
{
	T result{ data[0] };
	for (size_t i{ 1 }; i < count; ++i)
		if (data[i] > result) result = data[i];
	return result;
}

//define another template overload for vector
template <typename T>
T larger(const std::vector<T>& data)
{
	T result{ data[0] };
	for (auto& value : data)
		if (value > result) result = value;
	return result;
}
// overload the original template with another template specifically for pointer types
template<typename T>
T* larger(T* a, T* b)
{
	return *a > *b ? a : b;
}


int main()
{
	std::cout << "Larger of 1.5 and 2.5 is " << larger(1.5, 2.5) << std::endl;
	std::cout << "Larger of 3.5 and 4.5 is " << larger(3.5, 4.5) << std::endl;

	int big_int{ 17011983 }, small_int{ 10 };
	std::cout << "Larger of " << big_int << " and " << small_int << " is "
		<< larger(big_int, small_int) << std::endl;

	//using explicit instantiation of the template
	std::string a_string{ "A" }, z_string{ "Z" };
	std::cout << "Larger of \"" << a_string << "\" and \"" << z_string << "\" is "
		<< '"' << larger(a_string, z_string) << '"' << std::endl;

	/*You put the explicit type argument for the function template between angled brackets after the function
	name.This generates an instance with T as type double.*/
	std::cout << "Larger of " << small_int << " and 19.6 is "
		<< larger<double>(small_int, 19.6) << std::endl; // Outputs 19.6
	std::cout << "Larger of " << small_int << " and 19.6 is "
		<< larger<int>(small_int, 19.6) << std::endl; // Outputs 19
	std::cout << "Larger of " << small_int << " and 19.6 is "
		<< *(larger<int*>(&small_int, &big_int)) << std::endl; // Outputs 17011983
	std::cout << "Larger of " << small_int << " and 19.6 is "
		<< *larger(&small_int, &big_int) << std::endl; // Outputs 17011983

	int data_array[]{ 1,2,3,4,5 };
	std::cout << larger(data_array, std::size(data_array)) << std::endl;//output 5

	const std::vector<int> data_array1{ 1,2,3,4,5 };
	std::cout << larger(data_array1) << std::endl;//output 5

	std::cout << "Larger of " << small_int << " and 19.6 is "
		<< *larger(&small_int, &big_int) << std::endl; // Outputs 17011983

}

// Template for functions to return the larger of two values
template <typename T>
T larger(T a, T b)
{
	return a > b ? a : b;
}

int* larger(int* a, int* b)
{
	return *a > *b ? a : b;
}
#include <iostream>
#include <string>
#include <algorithm>//defines std::max() and std::min()

int main()
{
	int a{ 2 }, b{ 10 };
	std::string str_a{ "A" };
	std::string str_b{ "Z" };

	std::cout << "Larger of int a and b is " << std::max(a, b) << std::endl;
	std::cout << "Smaller of int a and b is " << std::min(a, b) << std::endl;

	std::cout << "Larger of string str_a and str_b is " << std::max(str_a, str_b) << std::endl;
	std::cout << "Smaller of string str_a and str_b is " << std::min(str_a, str_b) << std::endl;
}
// Using decltype() inside a function template
#include <iostream>
#include <vector>
#include <algorithm>	// for std::min()

// Template that computes a so-called "inner product" of two vectors.
// Both vectors are supposed to be equally long, 
// but the function will cope if they aren't.
template<typename T1, typename T2>
auto vector_product(const std::vector<T1>& data1, const std::vector<T2>& data2)
{
	// safeguard against vectors of different sizes
	const auto count = std::min(data1.size(), data2.size());

	decltype(data1[0] * data2[0]) sum{};
	for (size_t i{}; i < count; ++i)
		sum += data1[i] * data2[i];

	return sum;
}

int main()
{
	// Take the product of vectors with different types. Deduced return type: double
	std::vector<int> integers{ 1, 2, 3 };
	std::vector<double> doubles{ 1.1, 1.1, 1.1 };
	std::cout << vector_product(integers, doubles) << std::endl;

	// decltype() does not evaluate the expression, so this will work
	std::vector<bool> empty;
	std::cout << vector_product(empty, empty) << std::endl;
}
// Using decltype() inside a function template
#include <iostream>
/*Using a trailing decltype() syntax, the compiler will deduce the type from the return statements in the function
body.*/
template <typename T1, typename T2>
auto larger(T1 a, T2 b) -> decltype(a > b ? a : b)
{
	return a > b ? a : b;
}

int main()
{
	double num1{ 1.2 };
	int num2{ 3 };
	std::cout << larger(num1, num2) << std::endl;//output 3
}
// Using decltype() inside a function template
#include <iostream>
/*Using this syntax, the compiler will again deduce the type from the return statements in the function
body.*/
template <typename T1, typename T2>
decltype(auto) larger(T1 a, T2 b)
{
	return a > b ? a : b;
}

int main()
{
	double num1{ 1.2 };
	int num2{ 3 };
	std::cout << larger(num1, num2) << std::endl;//output 3
}
// Default Values for Template Parameters
#include <iostream>

template <typename TReturn = int, typename TArg1, typename TArg2>
TReturn larger(const TArg1 a, const TArg2 b)

{
	return a > b ? a : b;
}

int main()
{
	double num1{ 1.2 };
	double num2{ 3.5 };
	std::cout << larger(num1, num2) << std::endl;//output 3

}

// Default Values for Template Parameters
#include <iostream>

template <typename TArg, typename TReturn = TArg>
TReturn larger(const TArg a, const TArg b)

{
	return a > b ? a : b;
}

int main()
{
	double num1{ 1.2 };
	double num2{ 3.5 };
	std::cout << larger(num1, num2) << std::endl;//output 3.5

}
#include <iostream>
/*This template has a type parameter, T, and two nontype parameters, lower and upper, that are both
of type int.*/
template <int lower, int upper, typename T>
bool is_in_range(const T& value)
{
	return (value <= upper) && (value >= lower);
}

int main()
{
	int value{};
	std::cout << "Please enter a number." << std::endl;
	std::cin >> value;
	std::cout << value << (is_in_range<0, 500>(value) ? " is " : " isn't ") << "in the range from 0 to 500" << std::endl; // OK – checks 0 to 500

}
// Defining templates for functions that accept fixed-size arrays
#include <iostream>

template <typename T, size_t N>
T average(const T(&array)[N]);

int main()
{
	double doubles[2]{ 1.0, 2.0 };
	std::cout << average(doubles) << std::endl;

	double moreDoubles[]{ 1.0, 2.0, 3.0, 4.0 };
	std::cout << average(moreDoubles) << std::endl;

	/*Even though arrays and pointers are mostly equivalent,
	the compiler has no way of deducing the array size from a pointer.*/
	// double* pointer = doubles;
	// std::cout << average(pointer) << std::endl;      /* will not compile *

	//passing the brace-enclosed list as an argument.
	std::cout << average({ 1.0, 2.0, 3.0, 4.0 }) << std::endl;

	int ints[] = { 1, 2, 3, 4 };
	std::cout << average(ints) << std::endl;
}

template <typename T, size_t N>
T average(const T(&array)[N])
{
	T sum{};                            // Accumulate total in here
	for (size_t i{}; i < N; ++i)
		sum += array[i];                   // Sum array elements
	return sum / N;                      // Return average
}
// Defining a function template for adding numbers, 
// and an overload that works for pointer types.
// Extra: also make plus() work with string literals...
#include <iostream>
#include <string>
#include <string_view>

template <typename T>
T plus(const T a, const T b)
{
	return a + b;
}

// Overload with another template for pointer types
template <typename T>
T plus(const T* a, const T* b)
{
	return *a + *b;
}

std::string plus(const char* a, const char* b)
{
	return std::string{ a } +b;
}

int main()
{
	int n{ plus(3, 4) };
	std::cout << "plus(3, 4) returns " << n << std::endl;

	double d{ plus(3.2, 4.2) };
	std::cout << "plus(3.2, 4.2) returns " << d << std::endl;

	std::string s1{ "aaa" };
	std::string s2{ "bbb" };
	auto s3 = plus(s1, s2);
	std::cout << "With s1 as " << s1 << " and s2 as " << s2 << std::endl;
	std::cout << "plus(s1, s2) returns " << s3 << std::endl;

	// The extra part:
	std::string s{ plus("he", "llo") };
	std::cout << "plus(\"he\", \"llo\") returns " << s << std::endl;
}
// Your very own interpretation of std::size()
#include <iostream>
#include <array>
#include <vector>

// Look ma, no sizeof()!
template <typename T, size_t N>
inline size_t my_size(const T(&array)[N]) { return N; }

// Overload with two other templates for std::vector<> and array<> 
template <typename T>
inline size_t my_size(const std::vector<T>& vector) { return vector.size(); }

template <typename T, size_t N>
inline size_t my_size(const std::array<T, N>& array) { return N; }  // or array.size();

int main()
{
	int array[]{ 4, 8, 15, 16, 23, 42 };
	std::cout << "Size of numbers is " << my_size(array) << std::endl;//output 6

	// A string literal is also an array:
	std::cout << "Size of life lesson is "
		<< my_size("Always wear a smile. One size fits all.") << std::endl;//output 40

	std::vector<int> vector{ 4, 8, 15, 16, 23, 42 };
	std::cout << "Size of vector is " << my_size(vector) << std::endl;//output 6

	std::array<int, 6> array_object{ 4, 8, 15, 16, 23, 42 };
	std::cout << "Size of array_object is " << my_size(array_object) << std::endl;//output 6
}
	/*check for how many times one specific instance of a function template
	for any given argument type has been called.*/
	// The easiest way to verify this is using a static variable.
	// If indeed each function is only instantiated once, 
	// then all calls should share the same static variable...
	// You should see this being reflected in the program's output
	// as main() starts by calling the function for doubles twice.
#include <iostream>
#include <string>

template<typename T> T larger(T a, T b);    // Function template prototype

int main()
{
	std::cout << "Larger of 1.5 and 2.5 is " << larger(1.5, 2.5) << std::endl;
	std::cout << "Larger of 3.5 and 4.5 is " << larger(3.5, 4.5) << std::endl;

	int big_int{ 17011983 }, small_int{ 10 };
	std::cout << "Larger of " << big_int << " and " << small_int << " is "
		<< larger(big_int, small_int) << std::endl;

	std::string a_string{ "A" }, z_string{ "Z" };
	std::cout << "Larger of \"" << a_string << "\" and \"" << z_string << "\" is "
		<< '"' << larger(a_string, z_string) << '"' << std::endl;
}

// Template for functions to return the larger of two values
template <typename T>
T larger(T a, T b)
{

	static size_t counter{};
	std::cout << "This instantation has now been called " << ++counter << " time(s)\n";
	return a > b ? a : b;
}
// Exercise 9-6 A Quicksort funtion template

// The top level sort() template with one parameter calls the sort() template with three parameters
// The sort() function template uses the swap() function template
// The list() template outputs all elements in a vector.

#include <iostream>
#include <iomanip>
#include <vector>

// Swap two vector elements
template<typename T>
inline void swap(std::vector<T>& data, size_t first, size_t second)
{
	T temp{ data[first] };
	data[first] = data[second];
	data[second] = temp;
}

// Sort a range of vector elements
template<typename T>
void sort(std::vector<T>& data, size_t start, size_t end)
{
	// Start index must be less than end index for 2 or more elements
	if (!(start < end))
		return;

	// Choose middle value to partition set
	swap(data, start, (start + end) / 2);         // Swap middle value with start

	// Check data against chosen value
	size_t current{ start };                      // The index of the last element less than the chosen element (after partitioning)
	for (size_t i{ start + 1 }; i <= end; ++i)
	{
		if (data[i] < data[start])                  // Is value less than chosen element?
			swap(data, ++current, i);                 // Yes, so swap to the left
	}

	swap(data, start, current);                   // Swap the chosen value with last in

	if (current) sort(data, start, current - 1);  // Sort left subset if exists
	sort(data, current + 1, end);                 // Sort right subset if exists
}

// Sort all vector elements
template<typename T>
inline void sort(std::vector<T>& values)
{
	if (!values.empty())
		sort(values, 0, values.size() - 1);
}

// Output vector elements
template<typename T>
void list(const std::vector<T>& values, size_t width = 5)
{
	for (auto value : values)
		std::cout << std::setw(width) << value;
	std::cout << std::endl;
}

int main()
{
	std::vector<int> numbers{ -2, 4, -5, 6, 10, -40, 56, 4, 67, 45 };
	list(numbers);
	sort(numbers);
	std::cout << "\nSorted integers:\n";
	list(numbers);

	std::cout << "\nCharacters to be sorted:\n";
	std::vector<char> letters{ 'C', 'd', 'a', 'z', 't', 'S', 'p', 'm', 'D', 'f' };
	list(letters, 2);
	sort(letters);
	std::cout << "\nSorted characters:\n";
	list(letters, 2);

	std::cout << "\nFloating-point values to be sorted:\n";
	std::vector<double> values{ -2.5, 1.4, -2.55, 6.3, 10.1, -40.5, 56.0, 4.7, 67.3, 45.0 };
	list(values, 10);
	sort(values);
	std::cout << "\nSorted floaating-point values:\n";
	list(values, 10);
}

 

百度地图街景图像批量获取 由于个人研究需要在最近试了一下使用API获取百度地图街景图片,在此介绍一下基于我个人需求的百度街景批量获取方法,本文以Python编码作为主要方法,如果对零编码工具有需要的可以关注一下UA城市分析小组刚刚发布的街景图片获取工具。 街景图像用作区域细粒度空间指标测度需要解决几个问题。 1. 首先就是调用额度的限制,现在百度对于个人认证开发者每天可用的街景图像调用额度为100张,在不考虑购买额度上限(1万张600元,其实有经费的前提下可以直接考虑钞能力)的情况下动辄几千上万张的爬取数量会大大增加需要爬取的时间周 阅读详情

相关推荐

论文选题难?7种AI加持的万能选题法(全专业通用+工具实测)

AI时代学术选题新范式:7大智能方法破解研究痛点 传统选题方法面临范围失控、资料难寻、创新不足三大痛点,本文提出AI赋能的7种选题方法论: 1.现象+群体法:自动抓取热点与人群特征 2.理论+场景法:经典理论与新兴场景匹配 3.技术+问题法:前沿技术与领域痛点结合 4.对比+变量法:时空维度与变量关系分析 5.热点+理论法:事件传播与理论深挖融合 6.微调+变量法:已有选题变量置换创新 7.AI全流程法:关键要素智能迭代生成 同时警示AI使用的伦理边界,需规避虚构文献、伪造数据等风险,并提供针对本科生、研究

Conan_0728的博客 1906

The beginning of C++ standard template library study

今天看代码看得真心沮丧,觉得之前学的C语言的知识真是完全不够用,在网上搜到这本 The C++ standard library - a tutorial and reference,决心要好好地系统学习C++这门语言,从今天开始。 这本书既可以当作学习的tutorial,同时也能作为编程时的工具书参考,是一本十几年来都销售得很好的关于C++的书籍,希望我能在这个过程中真正理解C++,欢迎大家的批

wgoodman的博客 310

算法设计与分析 0-1背包问题 动态规划解法【超详细】

0-1背包问题 问题描述 给定i个物品和一个容量为的背包,物品的重量是Wi,其价值为Vi 物品个数为i,背包容量为C。 如何选择装入背包内的物品,使得装入背包中的物品的总价值最大? 其中,每种物品只有全部装入背包或不装入背包两种选择。 物品不能分割,不能重复使用。 动态规划算法思路 首先建立一个数组B[i][c],最上面一行表示背包容量,最左边一列表示物品编号,中间填充的数值表示当当前背包容量为C...

寒泉 4万+

C++ Templates 第二版 附录B

最近开始看C++ Templates (第二版)的英文版,自行翻译其中一些章节,给自己将来回顾的时候看,需要配合英文版服用 附录B 值的种类 表达式是C++语言的基石,提供了表达计算的主要机制。每一个表达式都有一个类型,描述了其计算产生的值的静态类型。表达式“7”的类型是int,和表达式"5+2"一样,以及和表达式”x”一样,如果x的int类型变量的话。每一个表达式也有一个“值的种类”, 这描述了其值是如何组成的,并影响表达式的行为。 B.1 传统的左值和右值 在历史上,曾经只有左值和右值两种.

u012848330的博客 867

RAGFlow 离线部署:解决cl100k_base.tiktoken问题

摘要 本文详细解决了RAGFlow离线部署时tiktoken缓存文件下载失败的问题。通过分析tiktoken库的缓存机制,一次性不改代码解决.

baidu_25691461的博客 844

汽车LED驱动芯片TPS92682-Q1 SPI接口配置与调试实战指南

SPI(串行外设接口)作为一种高速、全双工的同步串行通信总线,广泛应用于微控制器与外围设备之间的数据交换。其工作原理基于主从架构,通过时钟、数据线和片选信号实现设备间的精准时序控制。在汽车电子和工业控制领域,SPI因其简单高效、易于扩展的特性,成为配置复杂电源管理芯片、传感器和存储器的关键技术。通过SPI接口,工程师能够动态调整设备参数、实时读取状态信息,并实现复杂的故障诊断与保护策略,极大地提升了系统的灵活性与可靠性。本文以汽车级双通道LED恒流驱动器TPS92682-Q1为例,深入解析如何通过SPI接口

weixin_29185493的博客 303

C++17 新特性介绍

C++17 新特性介绍

langshanglibie的专栏 8720

华为od统一考试B卷【单词搜索】C++ 实现

找到它是一个小游戏,你需要在一个矩阵中找到给定的单词。假设给定单词 HELLOWORD,在矩阵中只要能找到 H->E->L->L->O->W->O->R->L->D连成的单词,就算通过。意区分英文字母大小写,并且您只能上下左右行走,不能走回头路。

MISAYAONE的博客 724

C++17入门经典

C++17入门经典第1章 基本概念基本数据类型处理基本数据类型决策数组和循环指针和引用操作字符串定义函数函数模板程序文件和预处理指令 第1章 基本概念 基本数据类型 处理基本数据类型 决策 数组和循环 指针和引用 操作字符串 定义函数 函数模板 程序文件和预处理指令 ...

CC977的博客 5370

ABAP Platform 2025 到来之后,我们该怎样重新理解 SAP S/4HANA 2025 的开发边界

2025年10月,SAP正式发布S/4HANA Cloud Private Edition 2025及S/4HANA 2025,其技术底座ABAP Platform 2025全面支持SAP HANA数据库。新版平台强调Clean Core理念升级为四级成熟度模型,推动ABAP Cloud成为扩展开发首选方案。AI能力正式融入ABAP开发全流程,包括代码生成、迁移辅助等场景。RAP框架显著增强,支持跨业务对象协作、树形视图等复杂业务场景。ADT开发工具持续进化,新增内存分析、单元测试等专业功能,形成完整开发工

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 338

《UDS协议从入门到精通》系列——图解0x23:通过地址读取内存

如何读取ECU中指定内存地址的数据?图解UDS 0x23服务带你快速上手!!

车载系统攻城狮 2636

c++17好用的新特性总结

自从C++11开始,标志着c++的复兴和现代C++的开端。尽管有各种新语言层出不穷,但涉及性能和系统层面c++仍然是首选,地位无可撼动。 比如最近很火的鸿蒙系统框架代码,很多就是基于c++做的底层、运行时和应用框架封装。c++11后续又有11,14,17,20等众多新版本。哪个是主流? 截至目前来说,c++17应该是新项目的首选。C++14在11的基础上查缺补漏,并未加入许多新特性,而C++17作为C++11后的第一个大版本,标志着现代C++逐渐走向成熟。 这里总结下C++17在业务代

猫哥 的沉淀、积累、总结。天天学习,好好向上...c/c++,嵌入式 linux,Android,HarmonyOS,AIOT) 1万+

论文阅读:Query-Dependent Video Representation for Moment Retrieval and Highlight Detection

近年来,随着视频理解需求的急剧增加,视频时刻检索和高亮检测(MR/HD)成为人们关注的焦点。MR/HD的主要目标是对给定的文本进行视频时刻定位和内容一致性估计,即显著性得分。尽管最近基于transformer的模型带来了一些进步,但我们发现这些方法并不能完全利用给定查询的信息。例如,在预测时刻及其显著性时,有时会忽略文本查询与视频内容之间的相关性。为了解决这个问题,我们引入了基于查询的DETR (QD-DETR),这是一种为MR/HD量身定制的检测transfomer。

qq_42681787的博客 927

【现代C++开发】使用现代的C++快速开发一款串口读写软件

最近关于C++闹出来的动静态势挺大的,主要是由于爱美丽卡开始抵制C++,最近有不少文章都报道了这件事,比如即使C++到了这个时候,C++也还是值得学习的,起码不管你用起来体验怎么样,反正我开发的爽。现阶段仍然有很多地方都是用C++写的,因此还是需要用到的,尽管现在都开始使用Rust来代替C++了,但Rust的别扭也是众所周知的,在Rust中文社区就看到不少人因此选择放弃Rust转回C++了(反正不可能是我,我C++主要还是嵌入式和玩)。

专注Rust编程与技术分享,涵盖C++、Python等开发实战,新手友好,专注实战,助力编程成长。 1万+

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

深入探索C++17文件系统库:std::filesystem全面解析

在 C++17 之前,C++ 标准库缺乏对文件系统进行操作的原生支持,开发者通常依赖于操作系统特定的 API 或第三方库。这导致代码的可移植性差,且容易出错。C++17 引入了 `std::filesystem` 库,为文件和目录操作提供了一种跨平台、类型安全且易于使用的方式。本文将深入探讨 `std::filesystem` 库的各个方面,包括路径操作、文件操作、目录操作以及一些高级用法,并提供丰富的示例代码,帮助你快速掌握这个强大的工具。

专注Rust编程与技术分享,涵盖C++、Python等开发实战,新手友好,专注实战,助力编程成长。 1万+

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

现代c++白皮书(C++17)

现代c++白皮书

记录我目之所及的世界 2062

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

【C++】C++17 特性详细总结

C++17 显著提升了代码的可读性与性能,使其更适合大型项目和高性能计算场景。建议优先掌握结构化绑定、并行算法和文件系统操作,以充分释放现代C++的潜力。

浩瀚之水的专栏 2576

全面盘点17个C++17的高级特性

全面盘点17个C++17的高级特性C++17是目前比较常用的版本之一,今天花时间来梳理一下17个重要特性,所有的特性也不止这么点。目录1. 并行算法2. If Initializers3. 类模板参数推导(CTAD)4. template `<auto>`5. std::optional 和 std::variant6. 折叠表达式7. 结构化绑定8.模板模板参数9. 内联变量10. ...

Francis的博客 2279

C++17

正常情况下,lambda表达式中访问类的对象成员变量需要捕获this,但是这里捕获的是this指针,指向的是对象的引用,正常情况下可能没问题,但是如果多线程情况下,函数的作用域超过了对象的作用域,对象已经被析构了,还访问了成员变量,就会有问题。C++17增加std::variant实现类似union的功能,但却比union更高级,举个例子union里面不能有string这种类型,但std::variant却可以,还可以支持更多复杂类型,如map等。

m0_62807361的博客 1270

【C++】C++17的那些新特性

学习C++17的新特性。

慕雪华年的博客 8230

C++17新语法及标准库

在C++ 17中,引入了一个新的auto规则来改进直接列表初始化(direct-list-initialization)的行为。在此前,使用auto声明变量并对其进行直接列表初始化时,结果类型通常为std::initializer_list。这意味着该变量只能表示一个具有可复制语义的对象集合,并且很难用于处理其他类型的值。在上面的示例中,我们使用了新的auto规则来声明变量并对其进行直接列表初始化。在第一个示例中,由于列表只包含单个元素,因此结果类型为int。

飞鹤的程序员人生 9362

【C++知识点】C++17 常用新特性总结

📣专栏定位:整理一下 C++ 相关的知识点,供大家学习参考~🎏唠叨唠叨:在这个专栏里我会整理一些琐碎的 C++ 知识点,方便大家作为字典查询~

Newin2020的博客 1170

现代C++编程:C++ 17新特性个人总结

函数位于头文件<utility>中。它接受一个引用作为参数,并返回参数的const类型引用。这个函数非常简单,但在需要确保不修改传入对象的情境下非常有用。

C/C++Linux、音视频、DPDK 1399

C++17--继 C++11 之后又一次实质性增强的 C++ 标准

C++17是C++标准的重要更新版本,聚焦于提升开发效率和代码安全性。主要特性包括:结构化绑定简化多值解包;类模板参数推导减少冗余代码;if/switch初始化语句优化作用域管理;文件系统库提供跨平台操作支持;并行算法加速数据处理;新增std::optional、std::variant等安全容器类型。这些改进使C++17成为生产环境的理想选择,在保持高性能的同时显著提升了开发体验。该版本已获主流编译器全面支持,是现代化C++开发的推荐基准。

聚焦前沿科技与硬核技术底层拆解,深耕 AI、量子计算、WebAssembly 等核心领域,拒绝碎片化科普。以通俗语言讲透复杂原理,用干货内容拆解技术逻辑,助力从业者吃透核心、把握趋势,每篇都是可落地的深度技术沉淀。 1万+

跨越十年的C++演进:C++17新特性全解析

c++17新特性

m0_73443478的博客 1334
上一篇: 《Beginning C++17》-学习笔记-Chapter 08-Defining Functions
下一篇: 《Beginning C++17》-学习笔记-Chapter 10-Program Files and Preprocessing Directives
CodingIsFun
博客等级 码龄8年 37粉丝 240原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址