如何根据模板类型生成随机数以填充动态数组

How to generate random numbers to populate a dynamic array according to the template type?

本文关键字:随机数 填充 动态 数组 何根 类型      更新时间:2023-10-16

我一直专注于我的最新编程练习,以实现模板以打印2D数组。我只允许修改实现文件。(即主文件必须保持不变)(我的主文件)我被卡在使用随机数字的模板类型中所使用的随机数字填充数字。是否有一种可行的方法可以利用Rand()根据类型生成随机数?任何帮助将不胜感激。谢谢!

 RowAray.h:
#ifndef ROWARAY_H
#define ROWARAY_H
template<class T>
class RowAray{
    protected:
        int size;
        T *rowData;
    public:
        RowAray(int);
        virtual ~RowAray();
        int getSize()const{return size;}
        T getData(int i)const{
            if(i>=0&&i<size)return rowData[i];
            else return 0;}
        void setData(int,T);
};

#endif /* ROWARAY_H */
Table.h:
#ifndef TABLE_H
#define TABLE_H
#include "RowAray.h"
    template<class T>
    class Table{
        protected:
            int szRow;
            int szCol;
            RowAray<T> **columns;
        public:
            Table(unsigned int,unsigned int);
            Table(const Table<T> &);
            virtual ~Table();
            int getSzRow()const {return szRow;}
            int getSzCol()const {return szCol;}
            T getData(int,int)const;
            void setData(int,int,T);
            Table<T> operator+(const Table<T> &);
    };
    #endif /* TABLE_H */
RowAray.cpp:
#include "RowAray.h"
#include <cstdlib>

template<class T> 
RowAray<T>::RowAray(int r) {
    size = r;
    rowData = new T[r];
    for (int i = 0; i < size; i++) {
        rowData[i] = static_cast<T>((rand() % 90 + 10));
    }
}
template<class T>
RowAray<T>::~RowAray() {
    delete []rowData;
}
template<class T>
void RowAray<T>::setData(int i, T value) {
    rowData[i] = value;
}

Table.cpp:
#include "Table.h"
template<class T>
Table<T>::Table(unsigned int r, unsigned int c) {
    szRow = r;
    szCol = c;
    columns = new RowAray<T>*[r];
    for(int i = 0; i < r; i++) {
        columns[i] = new RowAray<T>(c);
    }
}
    template<class T>
    Table<T>::Table(const Table<T> &t) {
        szRow = t.getSzRow();
        szCol = t.getSzCol();
        columns = new RowAray<T>*[t.getSzRow()];
        for(int i = 0; i < t.getSzRow(); i++) {
            columns[i] = new RowAray<T>(t.getSzCol());
        }
        for (int i = 0; i < t.getSzRow(); i++) {
            for (int j = 0; j < t.getSzCol(); j++) {
                columns[i]->setData(j, t.getData(i, j));
            }
        }
    }
    template<class T>
    Table<T>::~Table() {
        for (int i = 0; i < szRow; i++) {
            delete []columns[i];
        }
        delete []columns;
    }
        template<class T>
        Table<T> Table<T>:: operator+(const Table<T> &t) {
            Table<T> tab;
            tab.szRow = t.getSzRow();
            tab.szCol = t.getSzCol();
            for(int i = 0; i < t.getSzRow(); i++) {
                for (int j = 0; j <t.getSzCol(); j++) {
                    tab.columns[i]->setData(j, this->getData(i,j) + t.getData(i,j));
                }
            }
            return tab;
        }
main.cpp:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;
//User Libraries
#include "Table.h"
//Global Constants
//Function Prototype
template<class T>
void prntRow(RowAray<T> *,int);
template<class T>
void prntTab(const Table<T> &);
//Execution Begins Here!
int main(int argc, char** argv) {
   //Initialize the random seed
   srand(static_cast<unsigned int>(time(0)));
   //Declare Variables
   int rows=3,cols=4;
   //Test out the Row with integers and floats
   RowAray<int> a(3);RowAray<float> b(4);
   cout<<"Test the Integer Row "<<endl;
   prntRow(&a,3);
   cout<<"Test the Float Row "<<endl;
   prntRow(&b,4);
   //Test out the Table with a float
   Table<float> tab1(rows,cols);
   Table<float> tab2(tab1);
   Table<float> tab3=tab1+tab2;
   cout<<"Float Table 1 size is [row,col] = ["
           <<rows<<","<<cols<<"]";
   prntTab(tab1);
   cout<<"Float Table 2 size is [row,col] = ["
           <<rows<<","<<cols<<"]";
   prntTab(tab2);
   cout<<"Float Table 3 size is [row,col] = Table 1 + Table 2 ["
           <<rows<<","<<cols<<"]";
   prntTab(tab3);
   //Exit Stage Right
   return 0;
}
template<class T>
void prntRow(RowAray<T> *a,int perLine){
    cout<<fixed<<setprecision(1)<<showpoint<<endl;
    for(int i=0;i<a->getSize();i++){
        cout<<a->getData(i)<<" ";
        if(i%perLine==(perLine-1))cout<<endl;
    }
    cout<<endl;
}
template<class T>
void prntTab(const Table<T> &a){
    cout<<fixed<<setprecision(1)<<showpoint<<endl;
    for(int row=0;row<a.getSzRow();row++){
        for(int col=0;col<a.getSzCol();col++){
            cout<<setw(8)<<a.getData(row,col);
        }
        cout<<endl;
    }
    cout<<endl;
}

从您的问题中不完全清楚,但是假设您想以不同类型的方式生成随机值,则可以使用模板函数的专业化:

template <class T>
T random_value(); // no definition
template <> 
float random_value<float>()
{
  return float(random()) / RAND_MAX; // number in [0, 1)
}
template <>
int random_value<int>()
{
  return random();
}
template <>
uint64_t random_value<uint64_t>()
{
  return (uint32_t(random()) << 32) | uint32_t(random());
}

然后在RowArray::RowArray内部您只需使用:

rowdata[i] = random_value<T>();

为了将相关的特殊化为因素(例如所有积分类型),请看一下std::enable_if

您可能会这样做的第二种方法是在C 17中使用if constexprstd::is_same_v。例如,在RowArray::RowArray构造函数内部,您可以做类似的操作:

for (int i = 0; i < size; ++i)
{
  if constexpr (std::is_same_v<T, float>)
    row[i] = /* whatever you want for float case */
  else if constexpr (std::is_same_v<T, int>)
    row[i] = /* whatever you want for int case*/
  else
    static_assert(sizeof(T) == 0, "unsupported type");
}

看一下type_traits标头,以查看如何测试所有积分类型等。

最后,看看C random库http://www.cplusplus.com/reference/random/,以避免使用随机值时犯常见错误。