小编inc*_*u26的帖子

使用模板将char数组复制到另一个时,会出现问题

这是我的代码:

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

template <class T1, class T2>
void copy2(const T1 source[], T2 destination[] , int size){
    for (int i=0 ; i < size ; ++i){
        destination[i]=static_cast<T1>(source[i]);
    }
}



int main() {

    const char one[] = "hello";
    char two[5];

    cout << "one: " << one << endl;
    cout << "two: " << two << endl;

    copy2(one, two, 6);

    cout << "one: " << one << endl;
    cout << "two: " << …
Run Code Online (Sandbox Code Playgroud)

c++ arrays templates copy char

2
推荐指数
1
解决办法
505
查看次数

标签 统计

arrays ×1

c++ ×1

char ×1

copy ×1

templates ×1