任何人都可以解釋我從這個簡單的程序使用std::map
得到的輸出。請注意,我將p
插入到地圖中,但不是q
,但它表示它們都找到了它們,但也說地圖中只有1個元素!std :: map的作用是什麼?
#include <map>
#include <iostream>
struct screenPoint {
float x = 0, y = 0;
screenPoint(float x_, float y_): x{x_}, y{y_}{}
};
bool operator<(const screenPoint& left, const screenPoint& right){
return left.x<right.x&&left.y<right.y;
}
std::map<screenPoint, float> positions;
int main(int argc, const char * argv[]) {
auto p = screenPoint(1,2);
auto q = screenPoint(2,1);
positions.emplace(p,3);
auto f = positions.find(p);
auto g = positions.find(q);
if (f == positions.end()){
std::cout << "f not found";
} else {
std::cout << "f found";
}
std::cout << std::endl;
if (g == positions.end()){
std::cout << "g not found";
} else {
std::cout << "g found";
}
std::cout << std::endl;
std::cout << "number elements: " << positions.size() << "\n";
return 0;
}
輸出:
f found
g found
number elements: 1
條件應該是'left.x
0x499602D2
比較實際上對你有意義嗎(從某種意義上說,你真的附加了哪個對象比另一個對象更小的意思),還是這只是一種使用字典的手段? –
@AmiTavory我只想要一本字典;我不在乎訂購 –