for(i in 0..500) の目的は完全にはわかりませんが、それが必要な場合:
#include <map>
#include <iostream>
#include <algorithm>
using std::map;
int main(int argc, const char** argv)
{
map<int /*time*/, int /*client*/> slot_info;
slot_info[123] = 1;
slot_info[125] = 2;
slot_info[480] = 3;
slot_info[481] = 1;
slot_info[482] = 3;
for (int timeKey = 0; timeKey <= 500; ++timeKey) {
auto it = slot_info.find(timeKey);
if (it != slot_info.end()) {
auto nextIt = ++it;
nextIt = std::find_if(nextIt, slot_info.end(), [=] (const std::pair<int, int>& rhs) { return rhs.second == it->second; });
if (nextIt != slot_info.end()) {
std::cout << "found " << it->second << " with " << it->first << " and " << nextIt->first << std::endl;
}
}
}
}
しかし、最初に各値をチェックして、マップ全体を反復したいと思う可能性が高いようです。
あなたの質問の 2 番目の部分は、「マップ .begin() と '.end()` についての私の理解は、それがリテラルであるということです。つまり、この場合、最後に到達したため、20 は返されません。」
「begin()」と「end()」は、現在のイテレータに関係なく、絶対です。
#include <map>
#include <iostream>
#include <algorithm>
using std::map;
std::ostream& operator<<(std::ostream& os, const std::pair<int, int>& item) {
std::cout << "[" << item.first << "," << item.second << "]";
return os;
}
int main(int argc, const char** argv)
{
map<int /*time*/, int /*client*/> slot_info;
slot_info[123] = 1;
slot_info[125] = 2;
slot_info[480] = 3;
slot_info[481] = 1;
slot_info[482] = 3;
for (auto it = slot_info.begin(); it != slot_info.end(); ++it)
{
std::cout << "*it = " << *it << ", but *begin = " << *(slot_info.begin()) << std::endl;
}
return 0;
}
したがって、あなたが持っている他のオプションは-かなり費用がかかります
for (int timeKey = 0; timeKey <= 500; ++timeKey) {
auto firstIt = slot_info.find(i); // find a slot for this time.
if (firstIt == slot_info.end())
continue;
auto secondIt = std::find(slot_info.begin(), slot_info.end(), [=](const std::pair<int, int>& rhs) { return firstIt->second == rhs.second && firstIt->first != rhs.first; });
if ( secondIt != slot_info.end() ) {
// we found a match
}
}