std :: for_each没有已知的从'std :: pair <const point,int>'到'std :: pair <point,int>&'的参数1的转换

paw*_*l_j 2 c++ std

我无法理解,因为这两种情况看起来都很相似,在第一次for_each我无法获得对的引用,而在第二种情况下,我得到的引用没有大惊小怪.有人可以向我解释一下吗?

#include <unordered_map>
#include <cstring>
#include <algorithm>
#include <iostream>

struct table_info_t {
    char name[32] = {0};
    int posx;
    int posy;

    table_info_t(const char *name, int posx, int posy) : posx(posx), posy(posy) {
        strncpy(this->name, name, 31);
    }
};

struct point {
    int posx;
    int posy;

    point(int posx, int posy) : posx(posx), posy(posy) { }
};

size_t hashstruct(const char* hptr, size_t size) {
    size_t h = 31;
    for (size_t i = 0; i < size; i++) {
        h = (h + *hptr) * 31;
        hptr++;
    }
    return h;
}

#define MAP_OPERATORS(typ)\
namespace std {\
\
    template<>\
    struct hash<typ> {\
        std::size_t operator()(const typ &k) const { return hashstruct((const char*)&k, sizeof(typ)); }\
    };\
\
    bool operator==(const typ& one, const typ& other) { return memcmp(&one, &other, sizeof(typ)) == 0; }\
};

MAP_OPERATORS(point); //hash structure and operator==
MAP_OPERATORS(table_info_t); //hash structure and operator==

int main(int argc, char** argv) {
    std::unordered_map<point, int> sp;
    sp[point(3, 4)] = 7;
    std::for_each(sp.begin(), sp.end(),
                  [](std::pair<point, int> pair) {
                      std::cout << pair.first.posx << "+" <<pair.first.posy << "=" << pair.second << "\n";
                  });

    std::unordered_map<table_info_t, const char *> m;
    m[table_info_t("make my day", 3, 14)] = "make my day 3,14";

    std::for_each(m.begin(), m.end(),
                  [](std::pair<const table_info_t, const char * >& pair)
                  {
                      std::cout << pair.first.name << pair.first.posx << pair.first.posy << " " << pair.second << "\n";
                  }
    );

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

两种结构都没有太大区别,但在编译时,我得到了这个错误:

In file included from .../4.8.2/include/c++/4.8.2/algorithm:62:0,
                 from .../src/main/c/hashtable.cpp:10:
.../4.8.2/include/c++/4.8.2/bits/stl_algo.h: In instantiation of '_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = std::__detail::_Node_iterator<std::pair<const point, int>, false, true>; _Funct = main(int, char**)::__lambda0]':
.../src/main/c/hashtable.cpp:45:24:   required from here
.../4.8.2/include/c++/4.8.2/bits/stl_algo.h:4417:14: error: no match for call to '(main(int, char**)::__lambda0) (std::pair<const point, int>&)'
  __f(*__first);
              ^
.../src/main/c/hashtable.cpp:43:24: note: candidates are:
                       [](std::pair<point, int> &pair) {
                        ^
In file included from .../4.8.2/include/c++/4.8.2/algorithm:62:0,
                 from .../src/main/c/hashtable.cpp:10:
.../4.8.2/include/c++/4.8.2/bits/stl_algo.h:4417:14: note: void (*)(std::pair<point, int>&) <conversion>
  __f(*__first);
              ^
.../4.8.2/include/c++/4.8.2/bits/stl_algo.h:4417:14: note:   candidate expects 2 arguments, 2 provided
.../src/main/c/hashtable.cpp:43:53: note: main(int, char**)::__lambda0
                       [](std::pair<point, int> &pair) {
                                                     ^
.../src/main/c/hashtable.cpp:43:53: note:   no known conversion for argument 1 from 'std::pair<const point, int>' to 'std::pair<point, int>&'   
Run Code Online (Sandbox Code Playgroud)

我需要删除引用.但是对于第二个for_each中的编译器,对<const table_info_t,const char*>的引用是完全正确的.

Jar*_*d42 5

迭代时std::unordered_map<Key, Value>,
迭代std::pair<const KEY, VALUE>

在第二种情况下,你std::pair<const KEY, VALUE>&这样做是好的.您甚至可以添加const,因为您不更改对:const std::pair<const KEY, VALUE>&.

在第一种情况下,您使用其他类型std::pair<KEY, VALUE>&. std::pair<KEY, VALUE>可以构建std::pair<const KEY, VALUE>.但是,临时不能绑定非const lvalue-reference.所以使用std::pair<KEY, VALUE>&无效.使用std::pair<KEY, VALUE>有效,但需要额外的副本.有关详细信息,请在地图上查看带有foreach的意外副本.

如果您可以访问C++ 14,则可以使用通用lambda来简化它:

[](const auto& p) {
    std::cout << p.first.posx << "+" << p.first.posy << "=" << p.second << "\n";
});
Run Code Online (Sandbox Code Playgroud)

此外std::for_each,还可以替换为基于范围的for循环(即使在C++ 11中):

for(const auto& p : sp) {
    std::cout << p.first.posx << "+" << p.first.posy << "=" << p.second << "\n";
};
Run Code Online (Sandbox Code Playgroud)