Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. std::set<std::pair<QDateTime, MyStruct*> > No items are deleted;
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 5.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Taz742T Taz742

    @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);
        }
    };
    
    Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by
    #6

    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.

    mrjjM K 2 Replies Last reply
    0
    • Taz742T Taz742

      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.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @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 ?

      K 1 Reply Last reply
      0
      • mrjjM mrjj

        @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 ?

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #8

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

        Vote the answer(s) that helped you to solve your issue(s)

        mrjjM 1 Reply Last reply
        0
        • K koahnig

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #9

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

          1 Reply Last reply
          0
          • Taz742T Taz742

            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.

            K Offline
            K Offline
            koahnig
            wrote on last edited by koahnig
            #10

            @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.

            Vote the answer(s) that helped you to solve your issue(s)

            Taz742T 2 Replies Last reply
            3
            • K koahnig

              @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.

              Taz742T Offline
              Taz742T Offline
              Taz742
              wrote on last edited by
              #11
              This post is deleted!
              1 Reply Last reply
              0
              • K koahnig

                @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.

                Taz742T Offline
                Taz742T Offline
                Taz742
                wrote on last edited by
                #12

                @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;
                       }
                    }
                }
                
                Taz742T 1 Reply Last reply
                1
                • Taz742T Taz742

                  @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;
                         }
                      }
                  }
                  
                  Taz742T Offline
                  Taz742T Offline
                  Taz742
                  wrote on last edited by
                  #13

                  @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;});
                  
                  
                  mrjjM 1 Reply Last reply
                  0
                  • Taz742T Taz742

                    @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;});
                    
                    
                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #14

                    @Taz742

                    So the lambda works?

                    Taz742T 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @Taz742

                      So the lambda works?

                      Taz742T Offline
                      Taz742T Offline
                      Taz742
                      wrote on last edited by
                      #15

                      @mrjj

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

                      its yes.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved