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 で、存在するかどうかを確認し、割り当て -> 存在する場合は、割り当てられたノード Y を取得します
  2. Y を参照する X の次のスロットをマップで検索します。

パート 2 は不明な部分です。Y がスロット 480 (500 のうち) で参照され、Y を参照する次のスロットがスロット 20 である場合 (スロット番号を無限ループで実行しています)、どうすればよいですか? 20を返すようにしますか?

マップ.begin()と '.end()` についての私の理解は、それがリテラルであるということです。つまり、この場合、最後に到達したため、20 は返されません。

4

4 に答える 4

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;
}

上記を 2 つの範囲で呼び出します。

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

secondmap<> イテレータが参照するペアのメンバーを比較する述語を渡します。C++03 を使用している場合はファンクターまたは関数ポインターを述語として使用でき、C++11 を使用している場合はラムダを使用できます。

于 2013-06-24T08:18:58.597 に答える
0

内部に別の for ループを入れてみませんか?

PS:while(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
          }
    }
  }
 }
于 2013-06-24T07:09:05.627 に答える
0

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
    }
}
于 2013-06-24T07:12:18.457 に答える