查找第一个不连续的键

huangapple go评论65阅读模式
英文:

map find the first non consecutive key

问题

我有一个以整数为键的地图。
使用STL,我想找到第一个具有非连续键的元素。

它应该是这样的:

map<int, int> m {
    {1, 1},
    {2, 2},
    {3, 3},
    {5, 5},
    {6, 6},
    {8, 8},
};

bool func(const pair<int, int>& a, const pair<int, int>& b){
    return (a.first + 1) != b.first;
}

int main()
{
    cout << find_first_of(m.begin(), m.end(), 1, func)->first; //gives 3
    cout << find_first_of(m.begin(), m.end(), 2, func)->first; //gives 3
    cout << find_first_of(m.begin(), m.end(), 5, func)->first; //gives 6 

    return 0;
}

我在STL中找不到任何可以执行这项任务的函数,是否可以提供帮助?

英文:

I have a map with integer as keys.
Using stl, I would like to find the first element that has not consecutive keys.

I should look like that:

map&lt;int, int&gt; m {
    {1, 1},
    {2, 2},
    {3, 3},
    {5, 5},
    {6, 6},
    {8, 8},
};

bool func(const pair&lt;int, int&gt;&amp; a, const pair&lt;int, int&gt;&amp; b){
    return (a.first + 1) != b.first;
}

int main()
{
    cout &lt;&lt; find_first_of(m.begin(), m.end(), 1, func)-&gt;first; //gives 3
    cout &lt;&lt; find_first_of(m.begin(), m.end(), 2, func)-&gt;first; //gives 3
    cout &lt;&lt; find_first_of(m.begin(), m.end(), 5, func)-&gt;first; //gives 6 
    
    return 0;
}

I couldn't find any function doing this in stl, any help ?

答案1

得分: 1

你可以使用std::adjacent_find来搜索地图中连续的键。

以下是一个示例。

#include <map>
#include <iostream>
#include <algorithm>

std::map<int, int> m{
    {1, 1},
    {2, 2},
    {3, 3},
    {5, 5},
    {6, 6},
    {8, 8},
};

int main()
{
    auto iter = std::adjacent_find(m.begin(), m.end(), [](cont auto& pr1, const auto& pr2)
        {
            return (pr1.first  != pr2.first - 1);
        });
    if (iter != m.end())
    {
        std::cout << "The last consecutive element is {" << iter->first << ", " << iter->second << "}\n";
        std::advance(iter, 1);
        std::cout << "The first non-consecutive element is {" << iter->first << ", " << iter->second << "}\n";
    }
    else
        std::cout << "All pairs are consecutive";
}

输出:

The last consecutive element is {3, 3}
The first non-consecutive element is {5, 5}

Live Example

英文:

You can use std::adjacent_find to search consecutive keys in the map.

Here is an example.

#include &lt;map&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;

std::map&lt;int, int&gt; m{
    {1, 1},
    {2, 2},
    {3, 3},
    {5, 5},
    {6, 6},
    {8, 8},
};

int main()
{
    auto iter = std::adjacent_find(m.begin(), m.end(), [](cont auto&amp; pr1, const auto&amp; pr2)
        {
            return (pr1.first  != pr2.first - 1);
        });
    if (iter != m.end())
    {
        std::cout &lt;&lt; &quot;The last consecutive element is {&quot; &lt;&lt; iter-&gt;first &lt;&lt; &quot;, &quot; &lt;&lt; iter-&gt;second &lt;&lt; &quot;}\n&quot;;
        std::advance(iter, 1);
        std::cout &lt;&lt; &quot;The first non-consecutive element is {&quot; &lt;&lt; iter-&gt;first &lt;&lt; &quot;, &quot; &lt;&lt; iter-&gt;second &lt;&lt; &quot;}\n&quot;;
    }
    else
        std::cout &lt;&lt; &quot;All pairs are consecutive&quot;;
}

Output:

The last consecutive element is {3, 3}
The first non-consecutive element is {5, 5}

Live Example

huangapple
  • 本文由 发表于 2023年2月24日 00:18:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定