开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
3 Star 10 Fork 6

secondtonone1/cpplearn

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
cpplearn
/
src
/
template_.cpp
cpplearn
/
src
/
template_.cpp
template_.cpp 5.52 KB
一键复制 编辑 原始数据 按行查看 历史
secondtonone1 提交于 2022年07月28日 22:17 +08:00 . modified: src/template_.cpp
#include "../inc/template_.h"
#include <iostream>
#include <vector>
#include <list>
#include <typeinfo>
#include <cxxabi.h>
using namespace std;
/*
1 typename 定义模板类型,可以定义多个类型,中间用,隔开
template<typename T, typename U>
2 非类型参数 用已知类型定义变量 template<unsigned U, unsigned M>
3
*/
void use_template()
{
int res = compare(3, 4);
cout << "compare(3,4) res is " << res << endl;
vector<int> v1 = {1, 3, 5};
vector<int> v2 = {2, 4};
res = compare(v1, v2);
cout << "compare(v1, v2) res is " << res << endl;
printData(3.4, "hello world");
res = compareArray("hello zack", "nice to meet u");
cout << "compareArray("
<< "hello zack "
<< ", nice to meet u"
<< ") res is " << res << endl;
}
void string_upper(std::string &str)
{
for_each(str.begin(), str.end(), [](char &c)
{
if(c <= 'z' && c >= 'a'){
c = c - 32;
} });
}
void use_classtemp()
{
Blob<int> ia;
Blob<int> ia2 = {0, 1, 2, 3, 5};
Blob<string> ia3 = {"hello ", "zack", "nice"};
for (size_t i = 0; i < ia2.size(); i++)
{
ia2[i] = i * i;
}
for (size_t i = 0; i < ia2.size(); i++)
{
cout << ia2[i] << endl;
}
for (size_t i = 0; i < ia3.size(); i++)
{
string_upper(ia3[i]);
}
for (size_t i = 0; i < ia3.size(); i++)
{
cout << ia3[i] << endl;
}
const auto &data = ia3.back();
cout << data << endl;
ia3.pop_back();
const auto &data2 = ia3.back();
cout << data2 << endl;
}
void use_debugdel()
{
double *p = new double;
DebugDelete d;
//调用DebugDelete的仿函数,delete p
d(p);
//析构多种类型
int *np = new int;
//构造DebugDelete对象后调用仿函数析构np
DebugDelete()(np);
//作为删除器析构智能指针
// p 被delete时会执行DebugDelete的仿函数进行析构
unique_ptr<int, DebugDelete> p3(new int, DebugDelete());
// 用DebugDelete 的仿函数析构string的指针
unique_ptr<string, DebugDelete> sp(new string, DebugDelete());
}
void use_tempmemfunc()
{
int ia[] = {0, 1, 2, 3, 4};
vector<long> vi = {7, 6, 5, 4};
list<const char *> w = {"now", "zack", "lov u"};
// Blob<T> T被实例化为int,
//函数模板It被实例化为 int *
Blob<int> a1(begin(ia), end(ia));
// It为vi的迭代器类型vector<long>::iterator T为long类型
Blob<long> a2(vi.begin(), vi.end());
//实例化Blob<string>以及list<const char *>::iterator参数
Blob<string> a3(w.begin(), w.end());
}
std::string getClearName(const char *name)
{
int status = -1;
char *clear_name = abi::__cxa_demangle(name, NULL, NULL, &status);
const char *demangle_name = (status == 0) ? clear_name : name;
std::string ret_val(demangle_name);
free(clear_name);
return ret_val;
}
void use_tempmove()
{
int i = 100;
cout << "my_move(i) type is " << getClearName(typeid(my_move(i)).name()) << endl;
//推断规则
/*
1 T被推断为int &
2 remove_reference<int &>的type成员是int
3 my_move 的返回类型是int&&
4 推断t类型为int& && 通过折叠规则t为int&类型
5 最后这个表达式变为 int && my_move(int &t)
*/
auto rb = my_move(43);
cout << "my_move(43) type is " << getClearName(typeid(my_move(i)).name()) << endl;
//推断规则
/*
1 T被推断为int
2 remove_reference<int>的type成员是int
3 my_move 的返回类型为int&&
4 my_move 的参数t类型为int &&
5 最后这个表达式变为 int && my_move(int && t)
*/
}
// template <typename F, typename T1, typename T2>
// void flip1(F f, T1 t1, T2 t2)
// {
// f(t2, t1);
// }
void ftemp(int v1, int &v2)
{
cout << v1 << " " << ++v2 << endl;
}
void gtemp(int &&i, int &j)
{
cout << "i is " << i << " j is " << j << endl;
}
void use_ftemp()
{
int j = 100;
int i = 99;
ftemp(42, i);
flip1(ftemp, j, 42);
cout << "i is " << i << " j is " << j << endl;
//模板为T1&&类型,当传递int值给flip2时,T变为int&
//通过折叠t1与t2变为int&类型,实现了绑定外部实参的效果
flip2(ftemp, j, 42);
cout << "i is " << i << " j is " << j << endl;
// flip2(gtemp, j, 42) 会报错
// 因为42作为右值纯递给flip2,t2会被折叠为int类型
// t2传递给gtemp第一个参数时,int&&无法绑定int类型
flip(gtemp, i, 42);
cout << "i is " << i << " j is " << j << endl;
}
void use_printtemp()
{
int i = 100;
string s = "hello zack!!!";
print(cout, i, s, 42);
}
void test_strvectmp()
{
auto strVec = StrVecTemp<string>();
strVec.push_back("hello zack");
StrVecTemp<string> strVec2(strVec);
strVec2.push_back("hello rolin");
StrVecTemp<string> strVec3 = strVec;
for_each(strVec.begin(), strVec.end(), [](auto data)
{ cout << data << " "; });
cout << endl;
for_each(strVec2.begin(), strVec2.end(), [](auto data)
{ cout << data << " "; });
cout << endl;
strVec3.emplace_back(10, 'b');
for_each(strVec3.begin(), strVec3.end(), [](auto data)
{ cout << data << " "; });
cout << endl;
}
//特例化
template <>
int compare(const char *const &p1, const char *const &p2)
{
cout << "use compare<> const char * const " << endl;
return strcmp(p1, p2);
}
void testcompare()
{
const char *p1 = "h1";
const char *p2 = "mom";
//调用特例化版本
compare(p1, p2);
//调用第二个版本
compare("hi", "mom");
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

cpp 学习笔记,项目源码
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/secondtonone1/cpplearn.git
git@gitee.com:secondtonone1/cpplearn.git
secondtonone1
cpplearn
cpplearn
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /