C++ std::for_每个参数1从';std::pair<;常量点,int>';至';std::pair<;点,int>&';
我无法理解,因为这两种情况看起来如此相似,在第一种情况下,我无法获得对pair的引用,而在第二种情况下,我可以毫不费力地获得对pair的引用。谁能给我解释一下吗C++ std::for_每个参数1从';std::pair<;常量点,int>';至';std::pair<;点,int>&';,c++,std,C++,Std,我无法理解,因为这两种情况看起来如此相似,在第一种情况下,我无法获得对pair的引用,而在第二种情况下,我可以毫不费力地获得对pair的引用。谁能给我解释一下吗 #include <unordered_map> #include <cstring> #include <algorithm> #include <iostream> struct table_info_t { char name[32] = {0}; int posx
#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;
}
#包括
#包括
#包括
#包括
结构表信息{
字符名[32]={0};
int-posx;
int-posy;
表信息(const char*name,int posx,int posy):posx(posx),posy(posy){
strncpy(这个->名称,名称,31);
}
};
结构点{
int-posx;
int-posy;
点(intposx,intposy):posx(posx),posy(posy){
};
大小\u t哈希结构(常量字符*hptr,大小\u t大小){
尺寸h=31;
对于(大小i=0;i std::cout当您在std::无序映射上迭代时,
您在std::pair上迭代
在第二种情况下,使用std::pair&
,这样就可以了。
您甚至可以添加常量,因为您不需要更改对:const std::pair&
在第一种情况下,使用另一种类型std::pair&
。
std::pair
可以从std::pair
构造。但是,临时变量不能绑定到非常量左值引用。因此使用std::pair&
无效。使用std::pair
有效,但会产生额外副本。有关详细信息,请参阅
如果您可以访问C++14,可以使用通用lambda对其进行简化:
[](const auto& p) {
std::cout << p.first.posx << "+" << p.first.posy << "=" << p.second << "\n";
});
@Someprogrammerdudeconst table\u info\u t
是正确的,键类型有const,错误来自第一个for\u each
谢谢@passery我用缺少的lt和gt标记修复了错误描述,所以现在更清楚错误是什么了。@user\u pj不要在注释中发布代码!编辑您的问题以确保您的问题确实是正确的ete和可验证。编辑问题时,请将编译器和/或链接器的完整输出复制粘贴到问题正文中。然后标记代码中错误所在的行,例如注释。谢谢@Someprogrammerdude-修复了此问题。如果您有权访问c++14,[](const auto&pair),因此不能忘记const键。在这个例子中,不相关,<代码> STD::FuxYuth/Eng>允许在后面的(香草)C++中执行平凡的并行操作,不同于Auto的范围。@ Max LangHOF:将代码< >范围< /代码>改为<代码> For每个也不难,在这种情况下…
[](const auto& p) {
std::cout << p.first.posx << "+" << p.first.posy << "=" << p.second << "\n";
});
for(const auto& p : sp) {
std::cout << p.first.posx << "+" << p.first.posy << "=" << p.second << "\n";
};