C++高级编程----模板的高级特性

更新时间:2023-07-20 21:00:44 阅读: 评论:0

C++⾼级编程----模板的⾼级特性
题⽬要求:假设有⼀个Grid类,定义了⼀个⽹格的的⼤⼩(mWidth, mHeight),每个⽹格对于⼀个值(value),这个值可能是
int,double,string等类型;存储整个⽹格的数据结构可以是vector,可以是deque,可以提供默认初始值;可以⽀持⼆维、三维拓展;对于其中的⼀些函数,希望⽀持变长参数控制,⽀持元组类型的打印控制;
分析:
1)⽀持Grid<int, vector>, Grid<int, deque>, Grid,容器模板类编程⽀持vector、deque和默认为vector,定义temple<typename T, typename Container = std::vector>,⽀持简化版Grid<int, vector>,定义template<typename T, template<typename E, typename Allocator = std::allocator> class Container = std::vector>;
2)⽀持Grid默认填充0,Grid<int, 10>默认填充10,⽀持Grid<classA, objA>定义 template<typename T, const T& DEFAULT> 3)⽀持多维数据Grid[x][y] = z,Grid[i][j][k] = r等,定义循环递归类NDGrid<typename T, size_t N-1>,定义基本条件(模板特例化):template class NDGrid<T, 1>;
4) 变长参数控制函数processValue(1, 2, 3.56, “test”, 1.1f),考虑⾮const引⽤和左值引⽤,设计template
<typename T1,
typename … Tn> + std::forward(args…)⽅法;
5)⽀持打印auto t1 = make_tuple(1, 2, 3.56, “test”, 1.1f); tuple_print(t1),打印元组,定义tuple_print_helper<n - 1, TupleType> tp(t)和tuple_print_helper<0, TupleType>类递归打印元组各个值
⽅法实现和测试
分析1:实现容器模板和默认容器填充、容器Allocator
// ca 1:
/*
@File          :grid.h
@Description:  :template gird support vector/dequeue container
@Date          :2021/12/04 15:22:35
@Author        :不一样的感觉
@version      :1.0
*/
#pragma once
#include<vector>
// 1: basic version:
// template<typename T, typename Container>
// 2:  template defalut, add default container for Grid class
// template<typename T, typename Container = std::vector<T>>
// 3: template template, support Grid<int, vector>, not only Grid<int, vector<int>>
template<typename T,template<typename E,typename Allocator = std::allocator<E>>class Container = std::vector>
class Grid{
public:
// constructor
explicit Grid(size_t inWidth= kDefaultWidth, size_t inHeight = kDefaultHeight);
// t value at (x, y)
void tElement(size_t x, size_t y,const T& inElem);
// get value at (x, y)
T&getElement(size_t x, size_t y);
const T&getElement(size_t x, size_t y)const;
// get grid size with width and height梦见好朋友死了是什么意思
size_t getWidth()const{return mWidth;}
size_t getHeight()const{return mHeight;}
/
/ t default size
static const size_t kDefaultWidth =10;
static const size_t kDefaultHeight =10;
private:
void initalizeCellsContainer();
// 2: std::vector<Container> mCells
std::vector<Container<T>> mCells;
size_t mWidth, mHeight;
};
};
// 2: template defalut
/
/ template<typename T, typename Container>
// 3: template parmeter for template
template<typename T,template<typename E,typename Allocator = std::allocator<E>>class Container> Grid<T, Container>::Grid(size_t inWidth, size_t inHeight):mWidth(inWidth),mHeight(inHeight){ initalizeCellsContainer();
}
template<typename T,template<typename E,typename Allocator = std::allocator<E>>class Container> void Grid<T, Container>::tElement(size_t x, size_t y,const T& inElem){
/*
@description  : t element at (x, y) with value inElem
@param  : location (x, y)
@Returns  : None
*/
mCells[x][y]= inElem;
}
template<typename T,template<typename E,typename Allocator = std::allocator<E>>class Container> T& Grid<T, Container>::getElement(size_t x, size_t y){
/*
@description  : get element at (x, y)
@param  : location (x, y)
@Returns  : value at (x, y)
*/
return mCells[x][y];
}
template<typename T,template<typename E,typename Allocator = std::allocator<E>>class Container> const T& Grid<T, Container>::getElement(size_t x, size_t y)const{
/*
@description  : get const version element at (x, y)
@param  : location (x, y)
@Returns  : const value at (x, y)感动的英语
*/
return mCells[x][y];
}
template<typename T,template<typename E,typename Allocator = std::allocator<E>>class Container> void Grid<T, Container>::initalizeCellsContainer(){
/*
@description  : initalize cells container helper to resize container
@param  : None
@Returns  : None
*/
石蒜花
for(auto& col : mCells){
}
}
// main /
#include<iostream>
#include<vector>
#include<deque>
剥削阶级#include"gridContainer.h"
using namespace std;
int main(){
// ----------------- test ca 1 --------------------------//
// // Grid<T, Constainer>
// Grid<int, vector<int>> myVectorGrid;
// Grid<int, deque<int>> myDequeGrid;
// Grid<int, deque<int>> myDequeGrid;
// myVectorGrid.tElement(1, 2, 3);
/
/ cout << Element(1, 2) << endl;
// myDequeGrid.tElement(0, 0, 3);
// cout << Element(0, 0) << endl;qq邮箱形式
// Grid<int, vector<int>> myVectorGrid2(myVectorGrid);
// cout << Element(1,2);
//error type: Grid<int, int> since the cond template typename does not have the method resize() in initalizeCellsContainer()
// ----------------- test ca 2 --------------------------//
Grid<int> myVectorDefault;//Grid<int, vector<int>>
// ----------------- test ca 3 --------------------------//
Grid<int, vector> myVectorGrid;
myVectorGrid.tElement(1,2,3);
cout << Element(1,2)<< endl;
return0;
}
分析2:实现默认值填充
#pragma once
#include<vector>
// non-typename only support: int, enum, pointer, reference
// template<typename T, const T DEFAULT = T()>
// reference template for other class
template<typename T,const T& DEFAULT>
翁晓萌class Grid{
public:
explicit Grid(size_t inWidth = kDefaultWidht, size_t inHeight = kDefaultHeight);
// tter
void tElement(size_t x, size_t y,const T& inElem);
// getter
T&getElement(size_t x, size_t y);
const T&getElement(size_t x, size_t y)const;
size_t getWidth()const{return mWidth;}
size_t getHeight()const{return mHeight;}
static const size_t kDefaultWidht =10;
static const size_t kDefaultHeight =10;
private:
size_t mWidth;
size_t mHeight;
std::vector<std::vector<T>> mCells;
void initalizeCellsContainer();
};
template<typename T,const T& DEFAULT>
Grid<T, DEFAULT>::Grid(size_t inWidth, size_t inHeight):mWidth(inWidth),mHeight(inHeight){
initalizeCellsContainer();
}
template<typename T,const T& DEFAULT>
void Grid<T, DEFAULT>::tElement(size_t x, size_t y,const T& inElem){
mCells[x][y]= inElem;
}
template<typename T,const T& DEFAULT>
template<typename T,const T& DEFAULT>
T& Grid<T, DEFAULT>::getElement(size_t x, size_t y){
return mCells[x][y];
}
template<typename T,const T& DEFAULT>
const T& Grid<T, DEFAULT>::getElement(size_t x, size_t y)const{
return mCells[x][y];
}
template<typename T,const T& DEFAULT>
void Grid<T, DEFAULT>::initalizeCellsContainer(){
for(auto& col:mCells){
for(auto& elem:col){
elem = DEFAULT;
}
}
}
/
main/
#include<iostream>
#include<vector>
#include"girdElement.h"
#include"SpreadsheetCell.h"
using namespace std;
namespace{
const int defaultInt =11;
SpreadsheetCell defaultCell(1.2);
}
// the cond reference template must be const express, static inner or outter complete object // so add namespace for const default for save
int main(){
// --------------- test default -----------------------//
// Grid<int> myDefaultGrid;
// Grid<int, 10> myDefaultGrid2;
// cout << Element(0, 0) << endl;
// cout << Element(0, 0) << endl;
// error support: Grid(class, obj)
// ----------- test reference template ----------------//
Grid<int, defaultInt> myIntGrid;
cout << Element(0,0)<< endl;
Grid<SpreadsheetCell, defaultCell> mySpreadsheet;
return0;
}
分析3:多维Grid实现
#pragma once
#include<vector>
template<typename T, size_t N>
class NDGrid{
public:
// constructor
explicit NDGrid(size_t inSize = kDefaultSize){resize(inSize);};
// operator
NDGrid<T, N-1>&operator[](size_t x){return mElems[x];};
const NDGrid<T, N-1>&operator[](size_t x)const{return mElems[x];};
void resize(size_t newSize){size(newSize);};
size_t getSize()const{return mElems.size();};
static const size_t kDefaultSize =10;
private:
std::vector<NDGrid<T, N-1>> mElems;
};
template<typename T>
class NDGrid<T,1>{
public:
抓虾网
explicit NDGrid(size_t inSize = kDefaultSize){resize(inSize);};
// operator
T&operator[](size_t x){return mElems[x];};
const T&operator[](size_t x)const{return mElems[x];};
void resize(size_t newSize){size(newSize);};
size_t getSize()const{return mElems.size();};
static const size_t kDefaultSize =10;
private:
std::vector<T> mElems;
};
main /
#include<iostream>
#include<vector>
#include"nDGrid.h"
using namespace std;
int main(){
NDGrid<int,3>threeDim(3);
threeDim[0][0][0]=1;
return0;
}
分析4:变长参数控制与解析

本文发布于:2023-07-20 21:00:44,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1089613.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:容器   模板   控制   默认   定义
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图