引用和模板
来源:互联网 发布:ap网络课程百度云 编辑:程序博客网 时间:2024/05/16 04:52
引用在C++中主要用于函数参数传递。在template中有很有意义。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// function that prints the passed argument
void print (int elem)
{
cout << elem << ' ';
}
void print1 (int &elem)
{
// cout << elem << ' ';
elem *= 2;
}
void print2 (int elem)
{
// cout << elem << ' ';
elem *= 2;
}
int main()
{
vector<int> coll;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}
// print all elements
for_each (coll.begin(), coll.end(), // range
print); // operation
cout << endl << "start print1" <<endl;
for_each (coll.begin(), coll.end(), // range
print1); // operation
for_each (coll.begin(), coll.end(), // range
print); // operation
cout << endl << "end for print1" <<endl;
cout << endl << "start print2" <<endl;
for_each (coll.begin(), coll.end(), // range
print2); // operation
for_each (coll.begin(), coll.end(), // range
print); // operation
cout << endl << "end for print2" <<endl;
cout << endl << "start print1" <<endl;
for_each (coll.begin(), coll.end(), // range
print1); // operation
for_each (coll.begin(), coll.end(), // range
print); // operation
cout << endl <<"end for print1" <<endl;
cout << endl;
}
得到结果:
1 2 3 4 5 6 7 8 9
start print1
2 4 6 8 10 12 14 16 18
end for print1
start print2
2 4 6 8 10 12 14 16 18
end for print2
start print1
4 8 12 16 20 24 28 32 36
end for print1
在此时,不需要改变template,只须改变被传递的函数定义,大大提高了template的重用机会
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// function that prints the passed argument
void print (int elem)
{
cout << elem << ' ';
}
void print1 (int &elem)
{
// cout << elem << ' ';
elem *= 2;
}
void print2 (int elem)
{
// cout << elem << ' ';
elem *= 2;
}
int main()
{
vector<int> coll;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}
// print all elements
for_each (coll.begin(), coll.end(), // range
print); // operation
cout << endl << "start print1" <<endl;
for_each (coll.begin(), coll.end(), // range
print1); // operation
for_each (coll.begin(), coll.end(), // range
print); // operation
cout << endl << "end for print1" <<endl;
cout << endl << "start print2" <<endl;
for_each (coll.begin(), coll.end(), // range
print2); // operation
for_each (coll.begin(), coll.end(), // range
print); // operation
cout << endl << "end for print2" <<endl;
cout << endl << "start print1" <<endl;
for_each (coll.begin(), coll.end(), // range
print1); // operation
for_each (coll.begin(), coll.end(), // range
print); // operation
cout << endl <<"end for print1" <<endl;
cout << endl;
}
得到结果:
1 2 3 4 5 6 7 8 9
start print1
2 4 6 8 10 12 14 16 18
end for print1
start print2
2 4 6 8 10 12 14 16 18
end for print2
start print1
4 8 12 16 20 24 28 32 36
end for print1
在此时,不需要改变template,只须改变被传递的函数定义,大大提高了template的重用机会
- 引用和模板
- 函数模板和模板函数(引用)
- 函数模板和模板函数(引用)
- 右值引用:模板参数推导和引用折叠
- 模板实参推断和引用以及move和forward详解
- Freemarker和jquery tmpl模板同时引用是冲突解决
- smarty模板引擎中的判断句式、循环、文件引用和对象的引用
- zencart 引用模板语句
- zencart 引用模板语句
- 自定义htmlm模板引用
- twig引用模板
- Gin html模板引用
- 引用和const引用
- 引用和const引用
- 引用和引用参数
- 引用和const引用
- 引用和常量引用
- c++ 模板数组的引用
- 文件操作
- SQL Server数据库开发的二十一条军规
- 游戏通讯
- 基于NiosⅡ的图像采集和显示的实现
- .net
- 引用和模板
- Hibernate主键生成 Key Generator
- SQL 2005 动态创建数据库表 表名为参数
- 没时间+没意思
- 在struts2中使用拦截器(Interceptor)控制登录和权限
- 毕业七年后的同学聚会
- tomcat日志
- 网址收藏(不断更新)
- 是否有人真的关心Java桌面应用?