C++:用于处理整数和字符串的函数模板
C++: Function Template to Handle Ints and Strings
我正在做一个编程任务,制作一个可以处理整数和双精度的函数模板。我已经这样做了,但为了好玩,我也想让它能够处理字符串。这是下面的功能。我将如何让它处理字符串?
// This program demonstrates the QuickSort Algorithm.
#include <iostream>
#include <algorithm>
#include <ctype.h> //needed for string handling?
using namespace std;
//**********************************************************
// partition selects the value in the middle of the *
// array set as the pivot. The list is rearranged so *
// all the values less than the pivot are on its left *
// and all the values greater than pivot are on its right. *
//**********************************************************
template <class T>
int partition(T set[], int start, int end)
{
int pivotValue, pivotIndex, mid;
mid = (start + end) / 2;
swap(set[start], set[mid]);
pivotIndex = start;
pivotValue = set[start]; // main.cpp:28: error: cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'int' in assignment
for (int scan = start + 1; scan <= end; scan++)
{
if (set[scan] < pivotValue) // main.cpp:31: error: no match for 'operator<' in '*(((std::string*)(((long unsigned int)scan) * 8ul)) + set) < pivotValue'
{
pivotIndex++;
swap(set[pivotIndex], set[scan]);
}
}
swap(set[start], set[pivotIndex]);
return pivotIndex;
}
//************************************************
// quickSort uses the quicksort algorithm to *
// sort set, from set[start] through set[end]. *
//************************************************
template <class T>
void quickSort(T set[], int start, int end)
{
T pivotPoint;
if (start < end)
{
// Get the pivot point.
pivotPoint = partition(set, start, end);
// Sort the first sub list.
quickSort(set, start, pivotPoint - 1); // main.cpp:56: error: no match for 'operator-' in 'pivotPoint - 1'
// Sort the second sub list.
quickSort(set, pivotPoint + 1, end); // main.cpp:58: error: no match for 'operator+' in 'pivotPoint + 1'
}
}
int main()
{
const int SIZE = 10; // Array size
int count; // Loop counter
// create arrays of various data types
int array[SIZE] = {7, 3, 9, 2, 0, 1, 8, 4, 6, 5};
// string array[SIZE] = {"7", "3", "9", "2","7", "3", "9", "2","a","r"};
double array2[SIZE] = {7.1, 3.3, 9.0, 2.7, 0.2, 1.5, 8.9, 4.5, 6.9, 5.45};
// Display the int array contents.
cout << "Displaying the int array before sorting" << endl;
for (count = 0; count < SIZE; count++)
cout << array[count] << " ";
cout << endl;
// Sort the int array.
quickSort(array, 0, SIZE - 1);
// Display the int array contents.
cout << "Displaying the int array after sorting" << endl;
for (count = 0; count < SIZE; count++)
cout << array[count] << " ";
cout << endl << endl;
// Display the double array contents.
cout << "Diplaying the double array before sorting" << endl;
for (count = 0; count < SIZE; count++)
cout << array2[count] << " ";
cout << endl;
// Sort the double array.
quickSort(array2, 0, SIZE - 1);
// Display the int array contents.
cout << "Displaying the double array after sorting" << endl;
for (count = 0; count < SIZE; count++)
cout << array2[count] << " ";
cout << endl;
return 0;
}
提前感谢,
亚当
如果你使用 std::string
T
,你可能已经非常接近工作了。
如果使用 char*
,则需要提供一个比较函子作为模板参数(或者有其他方法来指定T
的比较方法,例如类型特征类)。
另外,您不应该实现自己的swap
。 std::swap
已经存在,并且会为某些类型做一些聪明的事情(例如,交换两个vector
s是恒定时间,而不是复制向量中的每个对象)。
在 MSVC 中工作,这有点宽松,所以如果你在编译器上遇到问题,请告诉我。
该解决方案使用函子(带有operator()
的类/结构),这意味着可以像函数一样调用对象。它还使用模板专用化 - 看看如果删除LessThanCompare
的template < >
版本会发生什么 - char*
将回退到比较指针值(给出随机结果)。
在更好的实现中,您将使用类来放置快速排序和透视函数 - 然后您可以使用默认模板并避免像quickSort<char*, LessThanCompare<char*> >
一样调用 - 您可以说quicksort
但这有点超出了问题的范围!
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
template <class T>
struct LessThanCompare
{
bool operator()(T lhs, T rhs)
{
return lhs < rhs;
}
};
template < >
struct LessThanCompare<char*>
{
bool operator()(char* lhs, char* rhs)
{
return strcmp(lhs, rhs) == -1; // Note strcmp returns -1 if lhs < rhs
}
};
template <class T, class Comparator>
int partition(T set[], int start, int end)
{
Comparator CompareLessThan; // Declare an instance of the Comparator
T pivotValue;
int pivotIndex, mid; // Is mid an index or a value - use descriptive names!
mid = (start + end) / 2;
swap(set[start], set[mid]);
pivotIndex = start;
pivotValue = set[start];
for (int scan = start + 1; scan <= end; scan++)
{
if (CompareLessThan(set[scan], pivotValue))
{
pivotIndex++;
swap(set[pivotIndex], set[scan]);
}
}
swap(set[start], set[pivotIndex]);
return pivotIndex;
}
//************************************************
// quickSort uses the quicksort algorithm to *
// sort set, from set[start] through set[end]. *
//************************************************
template <class T, class Comparator>
void quickSort(T set[], int start, int end)
{
int pivotPoint;
if (start < end)
{
// Get the pivot point.
pivotPoint = partition<T, Comparator >(set, start, end);
// Sort the first sub list.
quickSort<T, Comparator>(set, start, pivotPoint - 1); // main.cpp:56: error: no match for 'operator-' in 'pivotPoint - 1'
// Sort the second sub list.
quickSort<T, Comparator>(set, pivotPoint + 1, end); // main.cpp:58: error: no match for 'operator+' in 'pivotPoint + 1'
}
}
int main()
{
const int SIZE = 10; // Array size
// Create arrays of strings
char* cstrArr[SIZE] = {
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
std::string strArr[SIZE];
for (int i = 0; i < SIZE; ++i)
{
strArr[i] = std::string(cstrArr[i]);
}
quickSort<char*, LessThanCompare<char*> >(cstrArr, 0, SIZE-1);
quickSort<std::string, LessThanCompare<std::string> >(strArr, 0, SIZE-1);
for (int i = 0; i < SIZE; ++i)
{
cout << cstrArr[i] << "tt" << strArr[i] << 'n';
}
return 0;
}
相关文章:
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 将重载的成员函数传递给函数模板
- C++17中函数模板中的静态数组初始化(MSVC 2019)
- 为什么 gcc 和 clang 为函数模板的实例化生成不同的符号名称?
- 具有常量引用参数的函数模板专用化
- std::span<const T> 作为函数模板中的参数
- 如何编写一个完美的缩写函数模板?
- 仅在函数模板中为那些定义了函数的类型执行函数
- 如何在C++中伪造虚拟可变参数函数模板?
- 以下代码中的函数模板有什么问题?
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 将显式实例化的函数模板与转换匹配
- 字符串函数在目标C++上运行时C++返回空
- 使用定义函数模板别名
- 函数模板返回类型
- 函数模板不适用于字符串文本
- 数值类型和字符串的模板函数总和
- 函数模板,用于将字符串文本转换为 std::array
- C++:用于处理整数和字符串的函数模板
- 如何为函数模板(字符串的冒泡排序数组)编写显式专用化代码