2013-06-24 49 views
2

我有一個地圖,時隙作爲一個鍵,指向分配的客戶端。並非所有的時隙都有一個分配的客戶端,它可能既是稀疏的,又是密集的人羣,所以我堅持使用map<int, int>實現。如果存在分配,則僅存在密鑰循環到地圖前面

計數器從插槽1到插槽x計數,並在每個插槽檢查分配。

例如

map<int, int> slot_info; 

while (1) 
{ 
    for (int i = 1; i =< 500; i++) //iterates through slots - 500 here as an example 
    { 
     map<int /*slot*/, int/*node*/>::iterator it = slot_info.find(i); 

     if (it != slot_info.end()) 
     { 
      int node = it->second; 

      //find the next slot that is assigned to node 
     } 
    } 
} 

我需要做下面的

  1. 在X插槽,檢查是否存在與分配 - >如果是,拿到這是後分配
  2. 搜索地圖下一時隙的節點Y X引用Y.

第2部分是我不確定的部分 - 如果Y在480號插槽(500以外)引用,並且下一個引用Y的插槽是插槽20(我正在運行插槽號碼一個無限循環),那麼我如何獲得它返回20?

我對地圖.begin()和'.end()的理解是它是字面的 - 即在這種情況下它不會返回20,因爲它已經達到了結尾。

+0

你有什麼具體問題? – poolie

+0

@poolie「我在循環回到地圖前端時遇到了問題,當它到達結尾時」,即當我在480號插槽,並且下一個分配插槽是2時,我如何找到它? – sccs

+1

爲什麼需要「循環」:一旦你找到一個值,你繼續通過你的收藏找到第二個實例 - 你知道它不是在第一個之前(或者你已經找到它)。不確定你要求的是什麼... – John3136

回答

0

我並不完全清楚是什麼了(我在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; 
      } 
     } 
    } 
} 

但它也似乎更可能是你可能只想在地圖上迭代檢查每個值。

問題的第二部分「我對地圖.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 
    } 
} 
0

請參考您最初找到的地方i

如果您到達地圖的盡頭並且未找到要查找的內容,請返回.start()

0

爲什麼你不在裏面有另一個for循環?

PS:你有(1),但break條件出來了嗎?

map<int, int> slot_info; 

while (1) 
{ 
    for (int i = 1; i =< 500; i++) 
    { 
    map<int, int>::iterator it = slot_info.find(i); 

    if (it != slot_info.end()) 
    { 
     int node = it->second; 

     //find the next slot that is assigned to node 
     for(map<int, int>::iterator it2 =++it;it2!=slot_info.end();it2++) 
      if(it2->second==node) 
      { 
      //This is the next slot which refers to the same client 
      } 
    } 
    } 
} 
+0

沒有中斷條件 - 它只在程序終止時停止 – sccs

0
// wrapper that performs a `find_if()` on two ranges, returning 
// an iterator to the first match found. 
// 
// If no match is found, `last_2` is returned 
template <typename InputIterator, typename Predicate> 
InputIterator find_if_in_ranges(
        InputIterator first_1, InputIterator last_1, 
        InputIterator first_2, InputIterator last_2, 
        Predicate pred) 
{ 
    InputIterator ret = std::find_if(first_1, last_1, pred); 

    if (ret == last_1) { 
     ret = std::find_if(first_2, last_2, pred); 
    } 

    return ret; 
} 

調用以上兩個範圍:

  • it + 1slot_info.end()
  • slot_info.begin()it

並傳入一個謂詞,該謂詞比較second成員對的一個映射<>迭代器指的。如果您使用的是C++ 03,則可以使用函數或函數指針作爲謂詞,如果您使用的是C++ 11,則可以使用lambda。