在地图前面循环

时间:2013-06-24 06:54:30

标签: c++ map stl

我有一个地图,其中时区作为键,指向指定的客户端。并非所有的时间段都有一个指定的客户端,它可能是一个稀疏和密集的群体,所以我坚持使用map<int, int>实现。如果存在分配,则密钥仅存在

计数器从插槽1到插槽x计数,并在每个插槽中检查分配。

e.g。

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,检查是否存在并指定 - &gt;如果是,则获取已分配的节点Y
  2. 搜索地图以寻找引用Y的X之后的下一个位置。
  3. 第2部分是我不确定的部分 - 如果在槽480(500个中)引用Y,并且引用Y的下一个槽是槽20(我在无限循环中运行槽号),然后如何让它返回20?

    我对地图.begin()和'.end()`的理解是它是文字的 - 即在这种情况下它不会向我返回20,因为它会到达终点。

4 个答案:

答案 0 :(得分:0)

保留对您最初找到的地点的引用i

如果您到达地图的最后但尚未找到所需内容,请返回.start()

答案 1 :(得分: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
          }
    }
  }
 }

答案 2 :(得分:0)

我不完全清楚for(我在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
    }
}

答案 3 :(得分: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成员与地图&lt;&gt;进行比较迭代器指的是。如果使用C ++ 03,可以使用函子或函数指针作为谓词,如果使用C ++ 11,则可以使用lambda。