gpt4 book ai didi

C++:处理整数和字符串的函数模板

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:47:35 25 4
gpt4 key购买 nike

我正在做一项编程任务,制作一个可以处理整数和 double 的函数模板。我已经做到了,但为了好玩,我想让它也能够处理字符串。这是下面的功能。我将如何让它处理字符串?

// 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 指定比较方法,就像类型特征类)。

此外,您不应该实现自己的swapstd::swap 已经存在并将对某些类型做一些聪明的事情(例如,交换两个 vector 是恒定时间的,而不是复制 vector 中的每个对象)。

关于C++:处理整数和字符串的函数模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10575143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com