2

我有一张地图,其钥匙是一对std::map<std::pair<int, int>, struct A> myMap。如何找到并访问该对中每个唯一第一个元素的最低对?例如,

struct A a;
myMap.insert(std::make_pair(std::pair<int, int>(1, 200), a));
myMap.insert(std::make_pair(std::pair<int, int>(1, 202), a));
myMap.insert(std::make_pair(std::pair<int, int>(2, 198), a));
myMap.insert(std::make_pair(std::pair<int, int>(2, 207), a));

我想使用的键是 <1, 200> 和 <2, 198>。我不需要它们一起返回,我只需要对它们进行操作即可。

谢谢你的时间!

4

4 回答 4

0

我会选择最直接的解决方案:

auto previous_first = 0;
auto is_first_iteration = true;
for (const auto& key_value : myMap) {
  const auto& key = key_value.first;
  const auto& value = key_value.second;
  if (is_first_iteration || key.first != previous_first) {
    is_first_iteration = false;
    previous_first = key.first;
    // do something here!
  }
}

在这里,您只需简单地遍历每个元素(我们依靠 std::map 的属性来对元素进行排序。并且对本身按第一个元素排序,然后按第二个元素排序)。在每一步中,我们都会记住之前的第一个元素——如果在这一步中相同,我们就跳过这一步。

@AndrewDurward 指出这个问题可以在对数时间内解决。这只是部分正确。首先,这个问题只有在最好的情况下才能在对数时间内解决。如果您有 N 个元素并且每个元素都有不同的first怎么办?答案中有 N 个元素,显然,您不能在对数时间内输出 N 个元素。

于 2013-09-21T19:24:11.903 回答
0

您可以使用自定义比较器

struct comp {
bool operator()(const std::pair<int, int>&x, const std::pair<int, int>& y ) const
{
    return x.second < y.second;
}
};

std::map<std::pair<int, int>, struct A,comp > myMap;

然后找到然后find_if与对的第一个元素一起使用。

在您的情况下std::less<T>,默认情况下按预期排序。

因此,以下将在没有自定义比较器的情况下工作,即只需

std::map<std::pair<int, int>, struct A > myMap;

int search_id=1; //First Element of pair, you can use entire pair too, 
//but that will defeat the purpose of "lowest pair"
auto it=std::find_if(myMap.begin() , myMap.end() , 
                [search_id](const std::pair<std::pair<int, int>, A>& x)
                { return x.first.first == search_id; } 
                );

if(it != myMap.end())
{
std::cout<<it->first.first<<" "<<it->first.second;
}

编辑:您可以将其用作循环遍历所有元素的函数

于 2013-09-21T19:27:02.223 回答
0

由于映射键是按字典顺序排序的,因此它们也可以被视为按其第一个元素排序(尽管有一些重复)。这意味着只要我们提供适当的谓词,我们就可以利用任何在排序范围上运行的标准算法。在这种情况下,比较键的第一个元素的谓词可以写成如下:

    typedef std::map<std::pair<int, int>, struct A> myMap_t;

    auto compare_first = [](
        myMap_t::value_type const & p1,
        myMap_t::value_type const & p2 )
    {
        return p1.first.first < p2.first.first;
    };

完成后,我们只需要选择正确的算法来迭代所需的地图元素。我们想要地图中的第一个元素,然后是与我们的谓词定义的那个不“等效”的第一个元素。这正是这样std::upper_bound做的。

auto e1 = myMap.begin();
auto e2 = std::upper_bound( e1, myMap.end(), *e1, compare_first );

或者我们可以遍历它们:

for( auto it = myMap.begin(); it != myMap.end(); it = std::upper_bound( it, myMap.end(), *it, compare_first ) )
        std::cout << it->first.first << " " << it->first.second << "\n";

完整代码在这里

于 2013-09-21T21:13:45.027 回答
0

使用一个小的重载助手,您可以使用std::lower_boundLive on Coliru

列出的第一个匹配项将是您查找的匹配项(std::pair<>已按顺序排列(first,right),升序排列)。

在这种情况下,助手是:

struct ByKeyFirst final { int value; };
inline bool operator<(Map::value_type const& v, ByKeyFirst target) { 
    return v.first.first < target.value; 
}

如您所见,唯一“增加的复杂性”是检查是否找到了匹配项,但效率应该没问题。而且您始终可以将复杂性隐藏在(可单元测试的)助手中:

Map::const_iterator byKeyFirst(Map const& map, int target)
{
    auto const e = end(map);
    auto lbound = lower_bound(begin(map), e, ByKeyFirst { target });
    if (lbound != e && lbound->first.first == target)
        return lbound;
    return e;
}

现在查找代码变得仅仅是:

int main()
{
    const Map myMap { 
        { { 1, 200 }, {} },
        { { 1, 202 }, {} },
        { { 2, 198 }, {} },
        { { 2, 207 }, {} },
    };

    auto match = byKeyFirst(myMap, 2);

    if (end(myMap) != match)
        std::cout << "Matching key: (" << match->first.first << "," << match->first.second << ")\n";
}

完整演示

#include <map>
#include <tuple>
#include <limits>

using namespace std;

struct A {};

using Pair = pair<int,int>;
using Map  = map<Pair, A>;

namespace /*anon*/
{
    struct ByKeyFirst final { int value; };
    inline bool operator<(Map::value_type const& v, ByKeyFirst target) { return v.first.first < target.value; }

    Map::const_iterator byKeyFirst(Map const& map, int target)
    {
        // you can just find the first match, Pair is already sorted by `first`, then `second`:
        auto const e = end(map);
        auto lbound = lower_bound(begin(map), e, ByKeyFirst { target });
        if (lbound != e && lbound->first.first == target)
            return lbound;
        return e;
    }
}

#include <iostream>

int main()
{
    const Map myMap { 
        { { 1, 200 }, {} },
        { { 1, 202 }, {} },
        { { 2, 198 }, {} },
        { { 2, 207 }, {} },
    };

    auto match = byKeyFirst(myMap, 2);

    if (end(myMap) != match)
        std::cout << "Matching key: (" << match->first.first << "," << match->first.second << ")\n";
}
于 2013-09-21T21:04:39.297 回答