简体   繁体   English

在C ++中的模板类中对类成员进行排序

[英]Sorting class members in a template class in C++

I am pretty inexperienced in C++ and I have a very strange problem when sorting a vector which is of type "T" that is a class member/attribute in a template class. 我在C ++中经验不足,对一个类型为“ T”的向量进行排序时遇到一个非常奇怪的问题,该向量是模板类中的类成员/属性。 The program compiles and run but calling "sort" over that attribute does work properly: it is not ordered at all after calling. 该程序可以编译并运行,但是对该属性调用“ sort”确实可以正常工作:调用后根本没有排序。 However, I can create a local vector of type T inside a method and get ir ordered properly. 但是,我可以在方法内部创建T类型的局部向量,并使ir正确排序。 What am I doing wrong? 我究竟做错了什么?

I also include a simple and fast example of this situation. 我还提供了这种情况的简单快速示例。 This is the template class (TempClass.h): 这是模板类(TempClass.h):

#include <vector>
#include <stdio.h>
#include <algorithm>
#include <functional>

template <class T> class TempClass{
    public:
        TempClass(T a, T b, T c){
            container.clear();
            container.reserve(3);
            container[0] = a; container[1] = b; container[2] = c;
        }

        void modifyAttribute(){
            printf("Previous state:\n");
            for(int i = 0; i<3; i++){
                printf("[%d] -> %d\n", i, container[i].read());
            }
            sort(container.begin(), container.end(), std::greater<T>());
            printf("Final state:\n");
            for(int i = 0; i<3; i++){
                printf("[%d] -> %d\n", i, container[i].read());
            }
        }

        void testLocally(){
            std::vector<T> localContainer(3);
            localContainer[0] = T(14); localContainer[1] = T(97); localContainer[2] = T(42);
            printf("Previous state:\n");
            for(int i = 0; i<3; i++){
                printf("[%d] -> %d\n", i, localContainer[i].read());
            }
            sort(localContainer.begin(), localContainer.end(), std::greater<T>());
            printf("Final state:\n");
            for(int i = 0; i<3; i++){
                printf("[%d] -> %d\n", i, localContainer[i].read());
            }
        }

    private:
        std::vector<T> container;
};

And a possible simple usage of it (Tester.cpp): 以及它的可能简单用法(Tester.cpp):

#include "TempClass.h"

class Value{
    public:
        Value(){
            this->val = 0;
        }

        Value(int val){
            this->val = val;
        }

        Value(const Value& reference){
            this-> val = reference.val;
        }

        bool operator >(const Value& other) const{
            printf("Ok...\n");
            return this->val > other.val;
        }

        int read(){
            return this->val;
        }

    private:
        int val;
};

int main(){
    TempClass<Value> object(Value(6), Value(17), Value(43));
    object.testLocally();
    object.modifyAttribute();
    return 0;
}

I do not really know what is happening :( Thank you very much in advance for your help. 我真的不知道发生了什么事:(非常感谢您的帮助。

Regards 问候

Looks like you are calling reserve instead of resize and going out of bounds in your TempClass constructor here. 看起来你调用reserve ,而不是resize和你去出界TempClass这里的构造函数。 See this thread for more details on the two functions. 有关两个功能的更多详细信息,请参见此线程

Other than that it seems to be working, unless the code you are using is different than what you posted here. 除此之外,它似乎是可行的,除非您使用的代码与您在此处发布的代码不同。

On a side note, this->val is unnecessary. 附带说明,此- this->val是不必要的。 just use val . 只需使用val

When you call container.reserve(3); 当您调用container.reserve(3); you increase the capacity of the vector (size of the internal storage), but the vector remains empty. 您可以增加引导程序的容量(内部存储的大小),但是引导程序仍为空。 operator[] doesn't insert elements, so when you do this: operator[]不会插入元素,因此在执行此操作时:

container.reserve(3);
container[0] = a; container[1] = b; container[2] = c;

You're accessing some elements that don't actually exist in the vector, the vector is still empty after this. 您正在访问向量中实际上不存在的某些元素,此后向量仍为空。

The method that does what you want is resize() , which actually increases the size of the vector. 做您想要的方法是resize() ,实际上增加了向量的大小。

The constructor that you call in your testLocally method sets the initial size of the vector, not it's initial capacity, that's why in that method it works as you expect it to work. 您在testLocally方法中调用的构造函数设置了向量的初始大小,而不是初始容量,这就是为什么在该方法中它可以按您期望的那样工作。

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

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