简体   繁体   English

我收到模板错误,我不确定为什么

[英]I'm receiving a template error and I'm not sure why

I was tasked with the following assignment. 我被分配以下任务。 The requirements (simplified) are ... Using STL containers std::list, std::vector, and std::deque, demonstrate the sorting of integers in descending order. 需求(简化)是...使用STL容器std :: list,std :: vector和std :: deque,演示按降序对整数进行排序。 Use std::set to create a data set of unique integers. 使用std :: set创建唯一整数的数据集。 Copy the data set to the other containers. 将数据集复制到其他容器。 Shuffle the containers. 洗净容器。 Display each container's contents. 显示每个容器的内容。 Use appropriate sorting algorithms to sort the data in each container. 使用适当的排序算法对每个容器中的数据进行排序。 Redisplay each container's contents. 重新显示每个容器的内容。 Code must be written in C++ 代码必须用C ++编写

This is the code I came up with but it's throwing an error on line 20 and 30 这是我想出的代码,但在第20行和第30行上引发了错误

'greater is not a template' “更大的不是模板”

. I'm sure it's an easy fix that I am missing but everything I try doesn't work. 我敢肯定,这是一个容易解决的问题,但是我尝试的所有方法都无法正常工作。 Thanks for the help. 谢谢您的帮助。

#include "stdafx.h"
#include <iostream>
#include <set>
#include <iterator>
#include <deque>
#include <list>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;


int main()
{

    set <int, greater <int>> set1;

    set1.insert(4);
    set1.insert(5);
    set1.insert(3);
    set1.insert(6);
    set1.insert(2);
    set1.insert(5);
    set1.insert(1);

    set <int, greater <int> > ::iterator itr;
    cout << "\n/the initial set is : ";
    for (itr = set1.begin(); itr != set1.end(); ++itr)
    {
        cout << '\t' << *itr;
    }
    cout << endl;

    deque <int> deque_ex1;
    list <int> list_ex1;
    vector <int> vec_ex1;

    for (itr = set1.begin(); itr != set1.end(); ++itr)
    {
        vec_ex1.push_back(*itr);
        deque_ex1.push_back(*itr);
        list_ex1.push_back(*itr);
    }
    std::random_shuffle (vec_ex1.begin(), vec_ex1.end());
    std::random_shuffle(deque_ex1.begin(), deque_ex1.end());

    vector<int> V(list_ex1.begin(), list_ex1.end());
    std::random_shuffle(V.begin(), V.end());
    list_ex1.assign(V.begin(), V.end());

    vector <int> ::iterator itr1;
    cout << "The shuffled vector is: ";
    for (itr1 = vec_ex1.begin(); itr1 != vec_ex1.end(); ++itr1)
    {
        cout << '\t' << *itr1;
    }
    cout << endl;

    deque <int> ::iterator itr2;
    cout << "The shuffled deque is: ";
    for (itr2 = deque_ex1.begin(); itr2 != deque_ex1.end(); ++itr2)
    {
         cout << '\t' << *itr2;
    }
    cout << endl;

    list <int> ::iterator itr3;
    cout << "The shuffled list is: ";
    for (itr3 = list_ex1.begin(); itr3 != list_ex1.end(); ++itr3)
    {
        cout << '\t' << *itr3;
    }
    cout << endl;
    cout << endl;

    cout << "sorted data structures (using system defined sort function):" << endl;
    sort(vec_ex1.begin(), vec_ex1.end());
    cout << "The sorted vector is :";
    for (itr1 = vec_ex1.begin(); itr1 != vec_ex1.end(); ++itr1)
    {
        cout << '\t' << *itr1;
    }
    cout << endl;

    sort(deque_ex1.begin(), deque_ex1.end());
    cout << "The sorted Deque is: ";
    for (itr2 = deque_ex1.begin(); itr2 != deque_ex1.end(); ++itr2)
    {
        cout << '\t' << *itr2;
    }
    cout << endl;

    list_ex1.sort();
    cout << "The sorted list is: ";
    for (itr3 = list_ex1.begin(); itr3 != list_ex1.end(); ++itr3)
    {
        cout << 't' << *itr3;
    }
    cout << endl;

    return 0;
}

对于std::greater ,您需要

#include <functional>

暂无
暂无

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

相关问题 COM错误:类未注册(我确定是) - COM Error: Class not registered (I'm sure it is) 我的代码有问题,因为我不知道为什么会收到错误。 这是代码: - I'm having issue with my code, in that I can't figure out why I'm receiving an error. Here is the code: 在默认构造函数中插入数据后,我没有任何输出,但是我不确定为什么不这样做 - I'm not getting any output after I have inserted data by default constructor but I'm not sure why not 引发了C ++读取访问冲突错误,但是我不确定为什么。 瓷砖滑块益智游戏 - C++ Read Access Violation error was thrown, but I'm not sure why. Tile Slider Puzzle Game 不知道为什么我收到关于std :: size_t不包含参数包的编译错误 - Not sure why I'm getting compilation error about std::size_t not containing a parameter pack 对“conclusionaryOutput(number1, number2, isASquare1, isASquare2)”的调用抛出错误,我不知道为什么 - the call to “conclusionaryOutput(number1, number2, isASquare1, isASquare2)” is throwing an error and I'm not sure why 我不确定为什么这段代码不起作用? - I'm not sure why this code isn't working? 我不确定为什么我的QThread模式被阻止 - I'm not sure why my QThread pattern is blocking 程序停止工作,我确定指针错误 - Program stops working, pointer error i'm sure 调用基本副本构造函数时收到错误 - I'm receiving error when calling base copy constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM