C++ 关于C++;std::pair
这是代码和错误信息,有什么想法吗?我在删除了这行代码后试过了C++ 关于C++;std::pair,c++,stl,C++,Stl,这是代码和错误信息,有什么想法吗?我在删除了这行代码后试过了Building t=beginedmap[b.id],编译就可以了。但无法找出这一行的错误原因。此行与对无关,但编译错误与对相关 错误消息 Error: required from 'std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with
Building t=beginedmap[b.id]代码>,编译就可以了。但无法找出这一行的错误原因。此行与对无关,但编译错误与对相关
错误消息
Error:
required from 'std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {const int&}; _Args2 = {}; _T1 = const int; _T2 = Building]'
struct Building {
int id;
int pos;
int height;
bool isStart;
Building(int i, int p, int h, int s) {
id = i;
pos = p;
height = h;
isStart = s;
}
};
class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
vector<Building> sortedBuilding;
unordered_map<int, Building> beginEndMap;
vector<pair<int, int>> result;
for (Building b : sortedBuilding) {
Building t = beginEndMap[b.id];
}
return result;
}
};
int main() {
}
错误:
“std::pair::pair(std::分段构造,std::tuple,std::tuple)[with _Args1={const int&};_Args2={};_T1=const int;_T2=Building]需要”
源代码
Error:
required from 'std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {const int&}; _Args2 = {}; _T1 = const int; _T2 = Building]'
struct Building {
int id;
int pos;
int height;
bool isStart;
Building(int i, int p, int h, int s) {
id = i;
pos = p;
height = h;
isStart = s;
}
};
class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
vector<Building> sortedBuilding;
unordered_map<int, Building> beginEndMap;
vector<pair<int, int>> result;
for (Building b : sortedBuilding) {
Building t = beginEndMap[b.id];
}
return result;
}
};
int main() {
}
struct建筑{
int-id;
int pos;
内部高度;
布尔伊斯塔特;
建筑物(内部i、内部p、内部h、内部s){
id=i;
pos=p;
高度=h;
isStart=s;
}
};
类解决方案{
公众:
矢量getSkyline(矢量和建筑){
向量分类构建;
无序地图开始于地图;
矢量结果;
用于(b栋:分类建筑){
建筑物t=起始地图[b.id];
}
返回结果;
}
};
int main(){
}
原因
长话短说,如果使用unordered_map::operator[]
,那么Building
需要是DefaultConstructible
,而不是。因此出现了(脚本)错误
这是因为如果找不到键,operator[]
将执行插入操作
要求如下:
value\u type
(也称为std::pair
(我的注释))必须是可从
std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>()
std::分段构造,std::forward\u as\u tuple(key),std::tuple()
使用默认分配器时,这意味着key\u type
(int
在您的情况下)必须是可复制的
和映射类型
(Building
在您的情况下)必须是DefaultConstructible
解决方案
是要为构建
使用默认构造函数,或者使用无序映射::at
,如果找不到密钥,则会抛出该构造函数,因此它没有此要求
为什么编译错误与pair相关,而与其他相关的内容无关 到未排序的地图 因为
std::pair
在内部用于存储键
-值
对
无序映射是包含键值的关联容器
使用唯一键配对
因为当你有模板的时候会出现脚本错误。C++概念正在进行中,这将有助于彻底地改善这种错误。
原因 长话短说,如果使用
unordered_map::operator[]
,那么Building
需要是DefaultConstructible
,而不是。因此出现了(脚本)错误
这是因为如果找不到键,operator[]
将执行插入操作
要求如下:
value\u type
(也称为std::pair(我的注释))必须是可从
std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>()
std::分段构造,std::forward\u as\u tuple(key),std::tuple()
使用默认分配器时,这意味着key\u type
(int
在您的情况下)必须是可复制的
和映射类型
(Building
在您的情况下)必须是DefaultConstructible
解决方案
是要为构建
使用默认构造函数,或者使用无序映射::at
,如果找不到密钥,则会抛出该构造函数,因此它没有此要求
为什么编译错误与pair相关,而与其他相关的内容无关 到未排序的地图 因为
std::pair
在内部用于存储键
-值
对
无序映射是包含键值的关联容器
使用唯一键配对
因为当你有模板的时候会出现脚本错误。C++概念正在进行中,这将有助于彻底地改善这种错误。
但是我的
钥匙类型
是int
而不是建筑
,请参见定义无序地图开始地图
,您的意思是价值类型
而不是钥匙类型
?谢谢,您的回答对我来说很有意义,但是为什么编译错误与pair有关,而不是与未排序的_映射有关?@LinMa好的,我想现在我明白了。对不起,我犯了错误,我有点累了。此编辑现在应该是正确的。感谢所有帮助,将您的答复标记为答案。放松一下!但是我的key\u type
是int
而不是Building
,请参见定义无序地图beginedmap
,您的意思是value\u type
而不是key\u type
?谢谢,您的回答对我来说很有意义,但是为什么编译错误与pair相关,而不是与未排序地图相关的东西?@LinMa ok,我想现在我明白了。对不起,我犯了错误,我有点累了。此编辑现在应该是正确的。感谢所有帮助,将您的答复标记为答案。放松一下!