std::set<std::pair<QDateTime, MyStruct*> > No items are deleted;



  • void ThreadAutomaticSend::run()
    {
        qDebug() << "before size: " << globalall->AutoSet.size() << "insert size: " << globalall->AutoSetINSERT.size() << "del size: " << globalall->AutoSetDELETE.size();
    
        while(!globalall->AutoSetINSERT.empty()){
            std::pair<QDateTime, AutoSend*> pr = *globalall->AutoSetINSERT.begin();
            globalall->AutoSetINSERT.erase(globalall->AutoSetINSERT.find(pr));
            if (globalall->AutoSet.find(pr) == globalall->AutoSet.end()) {
                globalall->AutoSet.insert(pr);
                qDebug() << "araa";
            }
        }
    
        while(!globalall->AutoSetDELETE.empty()){
            std::pair<QDateTime, AutoSend*> pr = *globalall->AutoSetDELETE.begin();
            globalall->AutoSetDELETE.erase(globalall->AutoSetDELETE.find(pr));
            if (globalall->AutoSet.find(pr) != globalall->AutoSet.end()) {
                globalall->AutoSet.erase(globalall->AutoSet.find(pr));
                qDebug() << "aris";
            }
        }
    
        qDebug() << "after size: " << globalall->AutoSet.size() << "insert size: " << globalall->AutoSetINSERT.size() << "del size: " << globalall->AutoSetDELETE.size();
    
        CHildThreadd *childthrd;
    
        for(globalall->AutoSetITerator = globalall->AutoSet.begin(); globalall->AutoSetITerator != globalall->AutoSet.end(); globalall->AutoSetITerator++){
           std::pair<QDateTime, AutoSend*> pr = *globalall->AutoSetITerator;
           if (QDateTime::currentDateTime() >= pr.first) {
               globalall->AutoSetDELETE.insert(pr);
               childthrd = new CHildThreadd();
               connect(childthrd, SIGNAL(finished()), childthrd, SLOT(deleteLater()));
               childthrd->run(pr.second);
           }
        }
    }
    

    AutoSet is Main SET.



  • This problem has been solved:

    struct AutoSend{
        int TCPUID;
        QDateTime When;
        QString Message;
        QString Mobile;
        int TUID;
        int CUID;
    
        AutoSend(int tcpuid, QDateTime when, QString message, QString mobile, int tuid, int cuid) : 
        TCPUID(tcpuid), When(when), Message(message), Mobile(mobile), TUID(tuid), CUID(cuid){}
    };
    
    struct mycomp{
        inline bool operator()(const AutoSend* left, const AutoSend* right){
            return left->When < right->When;
        }
    };
    
        // AutoThread//
            std::set<AutoSend*, mycomp> AutoSet;
            std::set<AutoSend*, mycomp> AutoSetINSERT;
            std::set<AutoSend*, mycomp> AutoSetDELETE;
            std::set<AutoSend*, mycomp> ::iterator AutoSetITerator;
        // AutoThread End.//
    

    But I still need an answer.
    Why not deleted?



  • Now there is another problem:

    I added two new structures in AutoSetINSERT;
    AutoSend * t1 = new (1, QDateTime::currentDateTime, "bbb", "597112233", 2, 3);
    AutoSend * t2 = new (1, QDateTime::currentDateTime, "bbb", "597112233", 4, 3);

    AutoSetINSERT(t1);
    AutoSetINSERT(t2);

    Yes, Now AutoSetINSERT.size() = 2 because i replace std::set to std::multiset

    t1 and t2 are different.
    t1 - > new (1, QDateTime::currentDateTime, "bbb", "597112233", 2, 3);
    t2 - > new (1, QDateTime::currentDateTime, "bbb", "597112233", 4, 3);

    continue:

        while(!globalall->AutoSetINSERT.empty()){
            AutoSend* pr = *globalall->AutoSetINSERT.begin();
            globalall->AutoSetINSERT.erase(globalall->AutoSetINSERT.find(pr));
            if (globalall->AutoSet.find(pr) == globalall->AutoSet.end()) {
                globalall->AutoSet.insert(pr);
                qDebug() << "araa";
            }
        }
    

    first item inserted at the AutoSET, because dont fint it. BUT The second thing tells me that he is already there.

    but they are different from each other:
    t1 and t2 are different.
    t1 - > new (1, QDateTime::currentDateTime, "bbb", "597112233", 2, 3);
    t2 - > new (1, QDateTime::currentDateTime, "bbb", "597112233", 4, 3);

    How can I solve this problem? So that any difference can be found?


  • Moderators

    @Taz742

    Wild guess, because your comparison struct mycomp is only on When the QDateTime. When this is used for sorting within your std::set, there is no wonder that with identical date and time the set does not see a difference, because you are not telling the algorithms.



  • @koahnig
    You are talking about?

    struct AutoSend{
        int TCPUID;
        QDateTime When;
        QString Message;
        QString Mobile;
        int TUID;
        int CUID;
    
        AutoSend(int tcpuid, QDateTime when, QString message, QString mobile, int tuid, int cuid) : TCPUID(tcpuid), When(when), Message(message), Mobile(mobile), TUID(tuid), CUID(cuid){}
    
        inline bool operator < (const AutoSend* left){
            return left->When < When;
        }
    
    
        inline bool operator == (const AutoSend* left){
            return (left->CUID == CUID && left->Message == Message && left->Mobile == Mobile && left->TCPUID == TCPUID && left->TUID == TUID && left->When == When);
        }
    
        inline bool operator != (const AutoSend* left){
            return (left->CUID != CUID || left->Message != Message || left->Mobile != Mobile || left->TCPUID != TCPUID || left->TUID != TUID || left->When != When);
        }
    };
    


  • he is working in vector fine

    #include <bits/stdc++.h>
    #include <algorithm>
    #include <vector>
    #include <stack>
    #include <cmath>
    #include <set>
    #include <map>
    
    using namespace std;
    
    const int MAXN = 1e5+1;
    const int  MOD = 1e9+7;
    
    struct abc{
        int a;
        int b;
        int c;
    
    	inline bool operator()(const abc rhs){
    		return rhs.a < a;
    	}	
    
        inline bool operator == (const abc rhs){
    		return rhs.a == a && rhs.b == b && rhs.c == c;
    	}
    	
    	inline bool operator != (const abc rhs){
    		return rhs.a != a || rhs.c != c || rhs.b != b;
    	}
    };
    
    vector<abc> st;
    
    int main(){
    	
    	abc aa, bb;
    	
    	aa.a = 2;
    	aa.b = 2;
    	aa.c = 3;
    	
    	st.push_back(aa);
    	
    	bb.a = 2;
    	bb.b = 3;
    	bb.c = 3;
    	
    	if(std::find(st.begin(), st.end(), bb) != st.end()){
    		cout << "aa aris";
    	}else{
    		cout << "aa ar aris";	
    	}
    	
    	return 0;
    }
    

    but not set.
    please help me.


  • Qt Champions 2016

    @Taz742
    Hi
    Docs says
    "QSet's value data type must be an assignable data type. In addition, the type must provide operator==(), and there must also be a global qHash() function that returns a hash value for an argument of the key's type. "

    So Im wondering how your qHash() looks like ?

    Also place a break point in your == and find out why it thinks they are the same ?


  • Moderators

    @mrjj
    he is using std::set and AFAIK operator< is enough, isn't t?


  • Qt Champions 2016

    @koahnig
    I guess you are right, just wondered with std::pair and a struct pointer


  • Moderators

    @Taz742 said in std::set<std::pair<QDateTime, MyStruct*> > No items are deleted;:

    he is working in vector fine

    #include <bits/stdc++.h>
    #include <algorithm>
    #include <vector>
    #include <stack>
    #include <cmath>
    #include <set>
    #include <map>
    
    using namespace std;
    
    const int MAXN = 1e5+1;
    const int  MOD = 1e9+7;
    
    struct abc{
        int a;
        int b;
        int c;
    
    	inline bool operator()(const abc rhs){
    		return rhs.a < a;
    	}	
    
        inline bool operator == (const abc rhs){
    		return rhs.a == a && rhs.b == b && rhs.c == c;
    	}
    	
    	inline bool operator != (const abc rhs){
    		return rhs.a != a || rhs.c != c || rhs.b != b;
    	}
    };
    
    vector<abc> st;
    
    int main(){
    	
    	abc aa, bb;
    	
    	aa.a = 2;
    	aa.b = 2;
    	aa.c = 3;
    	
    	st.push_back(aa);
    	
    	bb.a = 2;
    	bb.b = 3;
    	bb.c = 3;
    	
    	if(std::find(st.begin(), st.end(), bb) != st.end()){
    		cout << "aa aris";
    	}else{
    		cout << "aa ar aris";	
    	}
    	
    	return 0;
    }
    

    but not set.
    please help me.

    Your problem is not Qt related.
    You better check the difference and features in data handling between different containers Your compare function is incomplete and therefore you are getting unexpected results (see your initial post and my initial reply).

    The same is true for your last post. Now you have added relational operators. Your operator< is handling only part of the data structure. The rest is ignored by your operator and therefore it is ignored within operation of std::set (in case you still using this). You have added more relational operators (operator== and operator!=) but they are not consistent with operator<. This is not healthy programming. In some few cases your code might be sufficient, but as soon as enter the field of containers this is haunting you already.

    Create a data structure or class without operator< and try to use it in std::set or std::map. The compiler will complain that there is no operator< defined. As soon as you define it, the sets and maps will compile. I am not a 100% sure, but AFAIK most other relational operators are not used. Respectively this might be dependent on your actual stl implementation. Sometimes you see in debug mode error messages when the stl find conflicting relational operators, but that is not always the case.



  • This post is deleted!


  • @koahnig said in std::set<std::pair<QDateTime, MyStruct*> > No items are deleted;:

    Your problem is not Qt related.

    yes i know.

    Maybe it's too late. But it works.

    bool ThreadAutomaticSend::MyCompare(const AutoSend *left, const AutoSend *right)
    {
        if (left->Message == right->Message && left->CUID == right->CUID && left->Mobile == right->Mobile && left->TCPUID == right->TCPUID && left->TUID == right->TUID && left->When == right->When) {
            return true;
        }
    
        return false;
    }
    
    bool ThreadAutomaticSend::SortCompare(const AutoSend *left, const AutoSend *right)
    {
        return left->When < right->When;
    }
    
    void ThreadAutomaticSend::run()
    {
        while(!globalall->AutoSetINSERT.empty()){
            AutoSend* pr = *globalall->AutoSetINSERT.begin();
            globalall->AutoSetINSERT.erase(globalall->AutoSetINSERT.begin());
            bool ind = false;
            for(globalall->iter = globalall->AutoSet.begin(); globalall->iter != globalall->AutoSet.end(); globalall->iter++){
                if (MyCompare(pr, *globalall->iter)) {
                    ind = true;
                }
            }
            if (!ind) {
                globalall->AutoSet.push_back(pr);
            }
        }
    
        while(!globalall->AutoSetDELETE.empty()){
            AutoSend* pr = *globalall->AutoSetDELETE.begin();
            globalall->AutoSetDELETE.erase(globalall->AutoSetDELETE.begin());
            for(globalall->iter = globalall->AutoSet.begin(); globalall->iter != globalall->AutoSet.end(); globalall->iter++){
                if (MyCompare(pr, *globalall->iter)) {
                    globalall->AutoSet.erase(globalall->iter);
                    break;
                }
            }
        }
    
        std::sort(globalall->AutoSet.begin(), globalall->AutoSet.end(), SortCompare);
    
        CHildThreadd *childthrd;
    
        for(globalall->iter = globalall->AutoSet.begin(); globalall->iter != globalall->AutoSet.end(); globalall->iter++){
           AutoSend* pr = *globalall->iter;
           if (QDateTime::currentDateTime() >= pr->When) {
               childthrd = new CHildThreadd();
               connect(childthrd, SIGNAL(finished()), childthrd, SLOT(deleteLater()));
               childthrd->run(pr);
           } else {
               break;
           }
        }
    }
    


  • @Taz742 said in std::set<std::pair<QDateTime, MyStruct*> > No items are deleted;:

    bool ThreadAutomaticSend::SortCompare(const AutoSend *left, const AutoSend *right)
    {
    return left->When < right->When;
    }

    sorry its not work.

    
        std::sort(globalall->AutoSet.begin(), globalall->AutoSet.end(), [](AutoSend *aa, AutoSend *bb){return aa->When < bb->When;});
    
    

  • Qt Champions 2016

    @Taz742

    So the lambda works?



  • @mrjj

    std::sort(globalall->AutoSet.begin(), globalall->AutoSet.end(), [](AutoSend *aa, AutoSend *bb){return aa->When < bb->When;});
    

    its yes.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.