簡體   English   中英

std :: for_each沒有從&#39;std :: pair進行參數1的已知轉換 <const point, int> &#39;至&#39;std :: pair <point, int> &”

[英]std::for_each no known conversion for argument 1 from 'std::pair<const point, int>' to 'std::pair<point, int>&'

我無法理解,因為這兩種情況看起來非常相似,在第一個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;
}

兩種結構相差不大,但是在編譯時,出現以下錯誤:

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>&'   

我需要刪除參考。 但是,對於第二個for_each中的編譯器,完全可以引用pair <const table_info_t,const char *>。

當您遍歷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>構造。 但是,臨時不能綁定到非常量左值引用。 因此,使用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";
});

此外, std::for_each也可以由基於范圍的for循環替換(即使在C ++ 11中):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM