C++ 类中的 reference_wrapper
C++ 中的 reference_wrapper 是一个类模板,通过封装一个引用到类型为 T 的可复制和可赋值对象的引用来提供帮助。std::reference_wrapper 的实例基本上是对象,但它们可以转换成 T&。所以我们可以将它们作为参数用于需要引用类型作为参数的函数。
例子代码
#include <iostream>
#include <functional>
using namespace std;
int main () {
char a = 'h', b = 'e', c = 'l', d = 'l', e = 'o' , f = 'W', g = 'o', h = 'r', i = 'l', j = 'd';
reference_wrapper<char> ref[] = {a, b, c, d, e, f, g, h, i, j}; //creating reference array
for (char& s : ref)
cout << s;
cout <<endl;
return 0;
}输出
soumyadeep@soumyadeep-VirtualBox:~$ ./a.out helloWorld soumyadeep@soumyadeep-VirtualBox:~$
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP