繁体   English   中英

尝试按升序和降序对向量进行排序

[英]Trying to sort vectors in ascending and descending order

我试图按升序和降序对这些用户定义的向量进行排序。 错误与模板 class T 和汽车有关。 说明:

模板特化或离线模板定义中的无关模板参数列表

对“T”的引用不明确

'T' 阴影模板参数的声明

'auto' 类型说明符是 C++11 扩展 [-Wc++11-extensions]

这是我的代码:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <class T>

template <typename T>
void Ascend(vector<T>& L)
{

    sort(L.begin(), L.end());
    cout << "This is Ascending..." << endl;
    for (auto x : L) {
        cout << x << " ";
    }
}

template <typename T>
void Descend(vector<T>& I)
{

    sort(L.begin(), L.end(), greater<T>());

    cout << "This is Descending..." << endl;
    for (auto y : I) {
        cout << y << " ";
    }
}

int main()
{

    vector<int> Iset(8);
    vector<float> Fset(8);
    vector<char> Cset(8);
    vector<string> Sset(8);

    cout << "Please enter the numbers into this int array." << endl;

    for (int i = 0; i < 8; i++) {

        cin >> Iset[i];
    }

    cout << "Please enter the numbers into this float array." << endl;

    for (int i = 0; i < 8; i++) {

        cin >> Fset[i];
    }

    cout << "Please enter the letters into this char array." << endl;

    for (int i = 0; i < 8; i++) {

        cin >> Cset[i];
    }

    cout << "Please enter the phrases into this string array." << endl;

    for (int i = 0; i < 8; i++) {

        cin >> Sset[i];
    }
}

这是否与我如何定义函数或我如何应用模板有关?

我猜第一个问题是template <class T>行,它不合适。

其次,在Descend(vector<T>& I) function 中,您有一个名为I的向量,但您在下一行对名为L的变量进行排序。

当您删除template <class T>行并对Descend function 中的正确变量进行排序时,它应该可以编译(它对我有用)。

您可以稍微改进代码的另一件事是在基于范围的循环中使用引用:

    for (const auto& y : I) {
        cout << y << " ";
    }

暂无
暂无

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

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