C++ Mr.How知其所以然之C++ 代码实现模板
C++ Mr.How知其所以然之C++ 代码实现模板
原创文章,版权所有,搬运文章,转发请注明polyaD,原文链接https://polyad.github.io/tags
如有任何问题或疑惑,请在后面留言或者发邮箱留言polyaluthor@gmail.com,加微信polyad或者qq数学算法开发10群 282642152进行探讨,请备注:Mr.How。
- 理念:依据模块,生成以后所有有关的C++代码,并进行解析。
- 逻辑结构:
- 1.背景信息:作者,联系方式,更新时间,版本,主要功能,主要实现逻辑,使用示例,运行方法
- 2.主程序:包含的标准库信息,C++模板,函数功能实现,主程序,调用处理信息
- 3.次模块:时间运行模块,输出模块,输入检测模块,检测模块,测试模块
- 4.数据如何有序安全传递?数据如何存储并处理?
- 5.问题列表:
/*############################################################################
#Author:polya
#Emalil:polyaluthor@gmail.com
#323_Number_of_Connected_Components_in_an_Undirected_Graph.cpp
#Last updated:2019.10.16
#history version
#Version: 0.1
#function list add template for avoid datatype
#Purpose:
#Main logic principle:
#Usage:
# example: model.cpp
g++ model.cpp -o model -std=gnu++11
make model
./model
#eg
g++ 323_Number_of_Connected_Components_in_an_Undirected_Graph.cpp -o 323 -std=gnu++11
make 323
./323
############################################################################*/
//logic outline:print information
//include input and out library
//logic struture:
//
#include <iostream>
#include <ctime>
#include <vector>
#include <iterator>
#include <sstream>
#include <algorithm>
#include <numeric>
using namespace std;
//define class for solution
template<class T>
class Solution {
private:
const vector<T>& nums_;
const T& target_;
public:
Solution(const vector<T>& nums,const T& target):nums_(nums),target_(target) {}
int SearInsertPos();
};
template<class T>
int Solution<T>::SearInsertPos(){
int i;
for(i = 0; i < nums_.size(); i++) {
if(nums_[i] >= target_) {
return i;
}
}
return i;
}
void Display(const int& data){
cout << data << ' ' ;
}
void Display_float(const float& data){
cout << data << ' ' ;
}
//call main function
int main(int argc, char* argv[])
{
//self test ###################################################################
clock_t begin = clock();
//run programming############################
int arr[] = {1,3,5,6};
vector<int> nums(arr,arr+4);
cout << "The array is:" ;
for_each(nums.begin(),nums.end(),Display);
Solution<int> sol(nums,5);
cout <<endl <<"The target is:" << int(5) << ",The index is:" << sol.SearInsertPos() << endl;
Solution<int> sol2(nums,2);
cout << "The target is:" << int(2) << ",The index is:" << sol2.SearInsertPos() << endl;
Solution<int> sol3(nums,7);
cout << "The target is:" << int(7) << ",The index is:" << sol3.SearInsertPos() << endl;
Solution<int> sol4(nums,0);
cout << "The target is:" << int(0) << ",The index is:" << sol4.SearInsertPos() << endl;
float f_arr[] = {1.1,3,5.1,6};
vector<float> f_nums(f_arr,f_arr+4);
cout << "\nThe array float element is:" ;
for_each(f_nums.begin(),f_nums.end(),Display_float);
Solution<float> f_sol(f_nums,5);
cout <<endl<< "The float target is:" << float(5.5) << ",The index is:" << f_sol.SearInsertPos() << endl;
cout<<"\n==================================================================="<<endl;
//###########################################
cout<<"Hello polya,welcome to the programming world" <<endl;
clock_t end = clock();
double elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
time_t now = time(0);
char* dt = ctime(&now);
cout<<"The programming had finished at time : "<< dt << "==============================" <<endl;
cout <<"The programming running time: "<< elapsed_secs << " s" << endl;
//out test ####################################################################
//unit test ###################################################################
return 0;
}
问题列表:
- 改进方向:同一个问题,多种代码运行加入?
- 如何做成API?
1.时间运行模块
逻辑结构:引进库,主程序:处理程序运行前记录一个时间,处理程序运行后记录一个时间。输出所有记录信息。
#include <ctime>
using namespace std;
int main(int argc, char* argv[])
{
//before programming
clock_t begin = clock();
cout<<"Hello polya,welcome to the programming world" <<endl;
//###########################################
//programming running space
//###########################################
clock_t end = clock();
double elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
time_t now = time(0);
char* dt = ctime(&now);
cout<<"The programming had finished at time : "<< dt << "==============================" <<endl;
cout <<"The programming running time: "<< elapsed_secs << " s" << endl;
//out test ####################################################################
//unit test ###################################################################
return 0;
}
2.c++模板template</class T/>
逻辑结构:模板主要功能针对不同的数据类型进行处理
- 声明模板类
声明类
私有
- 声明私有数据结构 公有
- 声明类的参数
- 类的函数声明
- 实现模板函数 类的函数实现
- 调用类的函数实现
//define class for solution
template<class T>
class Solution {
private:
const vector<T>& nums_;
const T& target_;
public:
Solution(const vector<T>& nums,const T& target):nums_(nums),target_(target) {}
int SearInsertPos();
};
template<class T>
int Solution<T>::SearInsertPos(){
int i;
for(i = 0; i < nums_.size(); i++) {
if(nums_[i] >= target_) {
return i;
}
}
return i;
}
//call main function
int main(int argc, char* argv[])
{
//call int
int arr[] = {1,3,5,6};
vector<int> nums(arr,arr+4);
cout << "The array is:" ;
for_each(nums.begin(),nums.end(),Display);
Solution<int> sol(nums,5);
cout <<endl <<"The target is:" << int(5) << ",The index is:" << sol.SearInsertPos() << endl;
//call float
float f_arr[] = {1.1,3,5.1,6};
vector<float> f_nums(f_arr,f_arr+4);
cout << "\nThe array float element is:" ;
for_each(f_nums.begin(),f_nums.end(),Display_float);
Solution<float> f_sol(f_nums,5);
cout <<endl<< "The float target is:" << float(5.5) << ",The index is:" << f_sol.SearInsertPos() << endl;
}
代码模板II
/************************************************************
#Copyright(C), 2021, polya Tech. Co.,Ltd.
#FileName: 72_Edit_Distance.cpp
#Author:
#contact:polyaluthor@gmail.com
#Description: //
#Version: //
FunctionList: //
1. -------
#History: //
<author> <time> <version > <desc strategy>
polyad 2021/07/07 0.1 first build this moudle vetror and umordered_map
***********************************************************/
#include<iostream>
#include<vector>
#include<ctime>
using namespace std;
/*
*/
class Solution {
public:
int min3(int a, int b, int c){
a = a < b ? a : b;
return a < c ? a : c;
}
int LevenshteinDis_m1(string s, int s_len, string t, int t_len){
int cost;
//
if(s_len == 0)return t_len;
if(t_len == 0)return s_len;
//
if(s[s_len - 1] == t[t_len - 1])cost = 0;
else cost = 1;
return min3(LevenshteinDis_m1(s, s_len - 1, t, t_len) + 1,
LevenshteinDis_m1(s, s_len, t, t_len - 1) + 1,
LevenshteinDis_m1(s, s_len - 1, t, t_len - 1) + cost);
}
int LevenshteinDP_m1(string s, string t){
//levenshtein
int dp[s.length()+1][t.length()+1];//d[i][j]
for(int i = 0; i <= s.length(); i++)dp[i][0] = i;//
for(int j = 1; j <= t.length(); j++)dp[0][j] = j;//
for(int j = 0; j < t.length(); j++){
for(int i = 0; i < s.length(); i++){
if(s[i] == t[j])dp[i+1][j+1] = dp[i][j];//
else dp[i+1][j+1] = min3(dp[i][j+1]+1, dp[i+1][j]+1, dp[i][j]+1);
//
}
}
return dp[s.length()][t.length()];
}
};
/*
main function call processing
define a vector and a target value .
call class sum. and running test the ends.
*/
int main(int argc, char** argv){
string m = "kitadds";
string n = "sitcdew";
Solution s;
clock_t start,finish;
double totaltime;
start=clock();
cout<<"recursion: "<<s.LevenshteinDis_m1(m, m.length(), n, n.length())<<endl;
finish=clock();
totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<"recursion running: "<<totaltime<<" time"<<endl<<endl;
start=clock();
cout<<"Dynamic Programming: "<<s.LevenshteinDP_m1(m, n)<<endl;
finish=clock();
totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<"DP running "<<totaltime<<" time"<<endl<<endl;
return 0;
}
- 函数中const 的功能?
返回顶部
评论:
技术文章推送
知其所以然主题模块分享
微信公众号:How先生polyad