// first: 5.5 15 // first: 15 5.5 // first: 10 10

map<float, int> m = { {5.5, 15}, {15, 5.5}, {10, 10} };

map<float, int> m2; for(auto it = m.begin(); it != m.end(); ++it) { m2[it->second] = it->first; m2[it->first] = it->second; cout << 'first: ' << it->first << ' ' << it->second << endl; }

cout << endl;

for(auto it = m2.begin(); it != m2.end(); ++it) { cout << 'first: ' << it->first << ' ' << it->second << endl; }

// 输出结果: // first: 5.5 15 // first: 15 5.5 // first: 10 10

// first: 15 5.5 // first: 5.5 15 // first: 10 10

C++ STL map 反转键值对:实现和示例

原文地址: https://www.cveoy.top/t/topic/lgIW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录