c++ set with structs, set.find() function making error 'invalid comparator'



  • I make set of structs. My struct has one array with length 8 inside as pointer. If I insert some structs to the set, it's okay. But when I try to find, error('invalid comparator') occurs. Here is my code.

    struct Key {
        int* arr;
    };
    
    bool operator<(const Key& a, const Key& b) {
        for (int i = 0; i < 8; i++) {
            if (a.arr[i] == b.arr[i]) {
                continue;
            }
            else {
                return a.arr[i] < b.arr[i];
            }
        }
    
        return true;
    }
    
    bool operator==(const Key& a, const Key& b) {
        for (int i = 0; i < 8; i++) {
            if (a.arr[i] != b.arr[i]) {
                return false;
            }
        }
    
        return true;
    }
    
    
        set<Key> visit;
        visit.insert(initKey);
    
    ......
    
                    Key key;
                    key.arr = newcandidate;
    
                    visit.find(key); -> trigger error.
    
    

    I want to add arrays to somewhere. Then I want to find some arrays in there fastly. So I try to use 'set' structures. What do I need to do to solve this problem? Thank you very much for your reading.



  • You need to define properly only operator<. The operator== is not relevant to the set. You should return false at last row. The relation is strict. It shouldn't be possible to have a < b and b < a, but your operator is defined to be a < a.


Log in to reply
 

最新帖子

最新内容

  • S

    Here I have one csv file with different values and date_time. I wrote the code and run it. Then it gave me an error time data '8/6/2018 6:45' does not match format ' %d/%m/%Y %H:%M:%S' I want to plot this different three values in one graph. Can anyone help me to solve this problem? Here is my code:

    condition = "" date_time = [] x1 = [] x2 = [] x3 = [] def convertTime(s): tm = time.strptime(s, " %d/%m/%Y %H:%M:%S") return datetime.datetime(date.tm_year,date.tm_mon, date.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec) with open('data43.csv','r') as csv_file: csv_data = csv.reader(csv_file, delimiter=',') row_num = 0 for row in csv_data: if(row_num == 0): condition = row[0] elif(row_num > 2): #Data starts here if(row[0] != ''): date_time.append(convertTime(row[0])) if(row[1] != ''): x1.append(int(row[1])) if(row[2] != ''): x2.append(int(row[2])) if(row[3] != ''): x3.append(int(row[3])) row_num = row_num + 1 fig1 = plt.figure(1) ax = fig1.add_subplot(2,1,1) ax.plot(date_time,x1) ax.stem(date_time,x2,'C1--','C1o',linefmt=None, markerfmt=None, basefmt=None) ax.stem(date_time,x3,'C2--','C2o',linefmt=None, markerfmt=None, basefmt=None) ax.legend() ax.xaxis_date() ax.get_xaxis().set_major_formatter(DateFormatter('%d/%m/%Y %H:%M:%S')) plt.xlabel('t') plt.ylabel('k') leg = plt.legend( loc = 'upper right') plt.draw() # Draw the figure so you can find the positon of the legend. bb = leg.get_bbox_to_anchor().inverse_transformed(ax.transAxes) xOffset = 0.3 bb.x0 += xOffset bb.x1 += xOffset leg.set_bbox_to_anchor(bb, transform = ax.transAxes) plt.rcParams["figure.figsize"] = [20,20] ax.plot(style='.-') plt.show()

    error :

    I put datetime as :

    so I include seconds as :00 after that I read this csv file.

    If I changed time into 12 hours reading then what will happened ? like this

    read more
  • S
    text = 'Details of Test-3K9Y9Y1-My-Node1 give me' s=text.split(' ') for i in s: if '-' in i: print(i.split('-')

    read more
  • S

    You could try to use the following regular expression:

    >>match = re.findall('[^ ]*-[^ ]*', text)

    And you could try it out here

    read more
  • S

    I am trying to get the regex pattern to find whole word if it matches a word\special character

    text = Details of Test-3K9Y9Y1-My-Node1 give me

    I want to get Test-3K9Y9Y1-My-Node1 from the sentence

    I tried

    >>match = re.findall('(?<=-)\w+', text) >>match >>['3K9Y9Y1','My','Node1']

    read more
  • S

    I had the same issue. You have to initiate the other tabs a stack layout as well. Like:

    bottomTabs: { children: [ ........ { stack: { children: [ { component: { screenNames['listReceipt'].name } } ] } }, { stack: { children: [ { component: { screenNames['listCustomer'].name } } ] } } ] }

    See also here: https://github.com/wix/react-native-navigation/issues/4786#issuecomment-467648825

    read more

推荐阅读

  • 1
  • 1
  • 2
  • 8
  • 2
  • 1
  • 2
  • 1