简体   繁体   English

使用模板对字符串和字符进行排序

[英]Using templates to sort strings and characters

I am trying to sort different types of arrays from the smallest to largest order using templates. 我试图使用模板从最小到最大顺序对不同类型的数组进行排序。

Though I got the int array to sort correctly, I couldn't get the character or string arrays from doing sorting. 尽管我可以正确地对int数组进行排序,但是我无法通过排序获得字符或字符串数​​组。 I kept getting an error that said "no matching function for call to bsort(char[10], int)" and "bsort(std::string[10], int)". 我一直收到一个错误,说“没有匹配的函数可以调用bsort(char [10],int)”和“ bsort(std :: string [10],int)”。 What am I doing wrong? 我究竟做错了什么?

In my template, I thought I had it accommodating to all the different types by the "Object" declaration. 在我的模板中,我认为我可以通过“对象”声明来适应所有不同的类型。

#include <iostream>
#include <string>
using namespace std;


template <class Object>
void bsort(Object a[], Object n)
{
    for (int i=0; i<n-1; i++)
    {
        for (int j=i+1; j<n; j++)
            {
                if(a[i]>a[j])
                {
                    Object item;
                    item=a[i];
                    a[i]=a[j];
                    a[j]=item;
                }
            }
    }
 }


 int main ()
 {
    int intarray[10]= {50, 10, 20, 15, 62, 32, 6, 80, 90, 100};
    char chararray[10]= {'a', 'f', 'v', 'b', 'c', 's', 'm', 'i', 'j', 'i'};
    string stringarray[10]= {"hi", "how", "are", "you", "today", "love", "eating", "food", "brownies", "icecream"};

cout<<"The intarray consists of"<<endl;
for (int i=0; i<10; i++)
    cout<<intarray[i]<<endl;

cout<<"The sorted intarray sorted is"<<endl;
bsort(intarray, 10);
for (int i=0; i<10; i++)
    cout<<intarray[i]<<endl;

cout<<"Sorted char array"<<endl;
bsort(chararray, 10);
for (int i=0; i<10; i++)
    cout<<chararray[i]<<endl;

cout<<"The sorted stringarray is"<<endl;
bsort(stringarray, 10);
for (int i=0; i<10; i++)
    cout<<stringarray[i]<<endl;

return 0;

} }

**edit, I tried that a[] at first, but it still did not do anything to change the sorting/errors that it gave me ** edit,我起初尝试了a [],但它仍然没有做任何改变它给我的排序/错误的操作

void bsort(Object *array, Object n)

should be 应该

void bsort(Object *array, std::size_t n)

Demo 演示版

You can also take advantage of template deduction so you don't have to provide a type or size every time. 您还可以利用模板推导的优势,因此不必每次都提供类型或大小。

template <class Object>
void bsort(Object *array, int n) {
    for (int i = 0; i < n - 1; ++i) {
        for (int j = i + 1; j<n; ++j){
            if (array[i] > array[j]) {
                Object item;
                item = array[i];
                array[i] = array[j];
                array[j] = item;
            }
        }
    }
}

This is fine, but you have to provide a size each time. 很好,但是每次都必须提供一个大小。 This could be annoying if you declared it like this: 如果这样声明,可能会很烦人:

intarray[] = {3, 1, 5, 2, 0, 8, 6, 9, 4, 7}; // do you really want to count these?

For this, you can create a very simple template wrapper (I put two): 为此,您可以创建一个非常简单的模板包装器(我放置了两个):

template<class Object, size_t N>
void bsort(Object(&o)[N]) {
    return bsort<Object>(o, N);
}

template<class Object, size_t N>
void bsort(Object(&o)[N], size_t &size) {
    size = N;
    return bsort<Object>(o, N);
}

The reason for the second one is that you can pass a size_t ref into it and it will set that as the size. 第二个原因是您可以将size_t ref传递给它,并将其设置为大小。 For example, you could run either of these: 例如,您可以运行以下任何一个:

int intarray[] = {3, 1, 5, 2, 0, 8, 6, 9, 4, 7};
bsort(intarray);
bsort<int>(intarray, 10); // <int> is rather unnecessary
size_t size = 0;
bsort(intarray, size);

The reason you may want to use the last one is that now you have a way of printing the correct size. 您可能要使用最后一个的原因是,现在您可以打印正确的尺寸。

int intarray[] = {3, 1, 5, 2, 0, 8, 6, 9, 4, 7};
size_t size = 0;
bsort(intarray, size);
for(size_t i = 0; i < size; ++i)
    std::cout << intarray[i] << "\n";

Of course this specific template will only work on stack-based arrays, not dynamically allocated ones, but you can always use the other call. 当然,此特定模板仅适用于基于堆栈的数组,不适用于动态分配的数组,但您始终可以使用另一个调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM