如何为函数模板(字符串的冒泡排序数组)编写显式专用化代码
how to code an explicit specialization for a function template (bubble sorting array of strings )
首先,我对c++还很陌生。我必须编写一个具有显式专门化的函数模板来对整数数组和字符串数组进行排序。
我的主程序
#include <iostream>
#include "sort.h"
int main() {
int a[] = { 1234, 546, 786, 2341 };
char* c[6] = { "Harry", "Jane", "Anne", "John" };
sort(a, 4);
sort(c, 4);
for (int i = 0; i < 4; i++)
std::cout << a[i] << std::endl;
std::cout << std::endl;
for (int i = 0; i < 4; i++)
std::cout << c[i] << std::endl;
std::cout << std::endl;
}
sort.h文件(包含模板的文件)
template<typename T, typename T1>
void sort(T* items, T1 count) {
T1 t;
for (int a = 1; a<count; a++)
for (int b = count - 1; b >= a; b--)
if (items[b - 1] > items[b]) {
t = items[b - 1];
items[b - 1] = items[b];
items[b] = t;
}
}
问题就在这里:它说:"错误C2912:显式专门化'void sort(char*,int)'不是函数模板的专门化"我对"char*c[]"以及如何对此类型进行专门化感到困惑。
template<>
void sort<char ** >(char** p, int count)
{
}
您必须与主模板签名完全匹配,例如:
template <>
void sort<char *, int>(char ** p, int count)
{
// ...
}
因此CCD_ 1和CCD_。
相关文章:
- .cpp和.h文件中的模板专用化声明
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 两种专用方法中的相同代码
- 在两个派生类之间执行专用代码
- C++ 代码生成和模板专用化
- C++ - 模板类专用化是否有办法包含常规模板中的代码
- 是否允许在模板专用化时从int转换为long(应该编译此代码)
- 如何使用type_traits生成依赖于是否存在类专用化的代码?
- 热以初始化专用模板代码中的静态 const 成员
- 模板部分专用化:如何避免代码重复
- 如何重用函数专用化代码
- 基于标记调度的专用化,用于C++代码中的平台识别和仿真
- 为什么我不能调用以下代码的C++显式专用化版本?
- 为什么允许此模板代码违反C++的专用访问说明符?
- 代码重复和模板专用化(当专用函数具有不同的返回类型时)
- 如何为函数模板(字符串的冒泡排序数组)编写显式专用化代码
- 避免在多个 C++ 模板专用化上重复代码