如何使模板函数专用化字符数组
How to make a template function specialization for a character array?
我正在尝试为气泡类型的字符数组制作模板函数专用化。但是出于某种原因,当我要定义函数时,我在函数名称上收到一个错误的下划线,我不知道为什么。
template<typename T>
T sort(T* a, T n) {
int i, j;
int temp;
for (i = n - 1; i > 0; i--) {
for (j = 0; j < i; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
template<>
const char* sort<const char*>(const char* a, const char* n) {
}
问题:
将T
替换为 const char *
时,函数签名如下所示:
const char* sort<const char*>(const char** a, const char* n)
// ^^^ T* a -> const char ** a
推荐:
为什么你的签名template<typename T> T sort(T* a, T n)
?你没有归还任何东西,你把n
当作size_t
.我建议将您的签名更改为以下内容:
template<typename T> void sort(T* a, size_t n);
以及您的专长:
template<> void sort<char>(char* a, size_t n);
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- 字符数组中的元素数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 使用字符数组作为 Map 中的键
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 按顺序声明的字符数组重叠
- 在 C++ 中字符串的大小与字符数组的大小
- 寻找一种更好的方法来表示无符号字符数组
- 如何打印 2D 字符数组C++
- 从字符数组的元素中减去'a'是什么意思
- 我是否不正确地集中了这些字符数组?