我想了解STL中用於類的語法。我們的老師指着我們到這個網站(http://www.sgi.com/tech/stl/Map.html),我複製下面的代碼:在C++(STL)結構中聲明的方法
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;
}
我不知道你能在結構中聲明的方法。這是如何運作的?
我假設用它,當你聲明名爲月的地圖,使用地圖比較字段中的光澤alphabetizes地圖。但仍然不確定它如何與struct語法一起工作。謝謝。
在C++結構
繼承默認也是公開的,這就是爲什麼我使用這麼多結構... – 2010-07-15 13:36:36
@Alexandre:好點 - 我已經將其納入答案。 – 2010-07-15 13:41:54
@Alexandre:Oooops ...應該已經檢查過。這意味着這兩個代碼片段不僅幾乎相當,而且完全相同...... – 2010-07-15 13:53:33