0

That's related to some exercise I am trying to do, so my question is specific about iterators: i have a set of data structure that i created (std::set<SomeClass> mySet) and this set have 3 arguments: (int i, int j, ClassA a).

I am want to search based on i, j if something exists, but i do not have the "ClassA a".

And std::set::find - demand me the third key as well.

Is there a way to do that? I mean, without making a custom iterator.

The relevant codes part:

The data structure: (NB is another class that created)

    class Cell {
public:
    Cell(const int i, const int j, NB nb) : m_i(i), m_j(j), m_nb(nb) {}
    bool operator<(const Cell& x)
    {
        if((this->m_i) < (x.m_i)) return true;
        else if((this->m_i) > (x.m_i)) return false;
        else return(this->m_j <= x.m_j);
    }

private:
    const int m_i;
    const int m_j;
    NB m_nb;
};

The function:

bool Board::legalPath(const vector<pair<int, int>> &path) const {
//set<Cell>::const_iterator ie = m_board.begin(); //is that even needed?
for(auto &x : path)
{
    int i = x.first;
    int j = x.second; //now have i, j - how can I see its exist in my set?
}

    return true;
}

Thanks.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Eran
  • 109
  • 1
  • 9
  • Is that even possible to ask a question here without voting it down every single time? at least can i have the reason? or its normal to just get into posts and vote them down without help. – Eran Aug 25 '19 at 09:45
  • You're missing a lot of information in your question. That's probably the reason for the down and close vote. There's no need to leave you a comment about that. – πάντα ῥεῖ Aug 25 '19 at 09:46
  • @πάνταῥεῖ I understand, and sorry if it is not normal to asking about that, But i do think if voting down - a comment can also help, i could give more information. I've tried to be specific, but i think i miss some knowledge maybe. can you help me to know what information can be helped? i will try my best to provide it. Thanks. – Eran Aug 25 '19 at 09:48
  • Also do some research before asking a question, here's a possible solution for your problem: https://stackoverflow.com/questions/7042578/how-to-find-an-object-with-specific-field-values-in-a-stdset – πάντα ῥεῖ Aug 25 '19 at 09:50
  • _"maybe. can you help me to know what information can be helped?"_ Sure, read [here](https://stackoverflow.com/help/how-to-ask). – πάντα ῥεῖ Aug 25 '19 at 09:54
  • `else return(this->m_j <= x.m_j);` looks wrong to me, it should be `<`. To prevent this kind of mistakes, use `std::tie`: `return std::tie(m_i, m_j) < std::tie(x.m_i, x.m_j);`. – HolyBlackCat Aug 25 '19 at 09:56
  • @πάνταῥεῖ i indeed did research here and in google before asking it, i have this question from yesterday, and i have seen this post. problem is, NB class is holding set of neighbors, Lets say - Its a 2D matrix, i have saved the specific paths that exists, and the neighbors. Every cell have it own i, j - position, but also, Cell holding NB - the neighbors of it. My question is: how can i search if i sending only i, j - if the cell exist? – Eran Aug 25 '19 at 09:57
  • @Eran You want a feature called 'transparent comparators'. https://stackoverflow.com/questions/20317413/what-are-transparent-comparators – HolyBlackCat Aug 25 '19 at 09:57
  • @HolyBlackCat I did not know about "tie" method - its in Algorithm library? Thanks. – Eran Aug 25 '19 at 09:59
  • @Eran https://en.cppreference.com/w/cpp/utility/tuple/tie – HolyBlackCat Aug 25 '19 at 09:59
  • 1
    You can't search in a std::set for anything other than the key of the set, except by searching thourgh the whole set one item at a time. If you need to search by a pair of integers then maybe you should have created a std::map instead. – john Aug 25 '19 at 09:59
  • 2
    @john Transparent comparators, man. :P – HolyBlackCat Aug 25 '19 at 10:00
  • @HolyBlackCat Seems I'm out of date, quite a common feeling nowadays. – john Aug 25 '19 at 10:01
  • Thank you guys, map did the job. – Eran Aug 26 '19 at 10:53

0 Answers0