Method declared in struct in C ++ (STL)

I am trying to understand the syntax used in STL for a class. Our teacher pointed us to this site ( http://www.sgi.com/tech/stl/Map.html ), where I copied the following code:

struct ltstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) < 0;
  }
};

int main()
{
  map<const char*, int, ltstr> months;

  months["january"] = 31;
  months["february"] = 28;
  months["march"] = 31;
  months["april"] = 30;
  months["may"] = 31;
  months["june"] = 30;
  months["july"] = 31;
  months["august"] = 31;
  months["september"] = 30;
  months["october"] = 31;
  months["november"] = 30;
  months["december"] = 31;

  cout << "june -> " << months["june"] << endl;
  map<const char*, int, ltstr>::iterator cur  = months.find("june");
  map<const char*, int, ltstr>::iterator prev = cur;
  map<const char*, int, ltstr>::iterator next = cur;    
  ++next;
  --prev;
  cout << "Previous (in alphabetical order) is " << (*prev).first << endl;
  cout << "Next (in alphabetical order) is " << (*next).first << endl;
}

I did not know that you could declare methods in structures. How it works?

I assume that with this, when you declare a card with the name of the month, using the gloss in the Comparison field of the card in alphabetical order displays the card. But still not sure how this works with structure syntax. Thank.

+3
source share
3 answers

++ a struct - , public .

,

struct ltstr
{
    // ...
};

class ltstr
{
public:
    // ...
};

, protected private .

, struct ++, , .

+15

++

+1

The structure does not add any functionality compared to the class, except in cases of default by public users. Thus, none of these functions is structure dependent.

0
source

Source: https://habr.com/ru/post/1754812/


All Articles