Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Lambda和map,参数按引用-编译错误_C++_C++11_Lambda - Fatal编程技术网

C++ Lambda和map,参数按引用-编译错误

C++ Lambda和map,参数按引用-编译错误,c++,c++11,lambda,C++,C++11,Lambda,我试图将我的问题缩小到一个最小的例子: #include <algorithm> #include <map> #include <string> #include <vector> int main() { std::vector<int> result; std::map<std::string, std::pair<unsigned int, std::vector<int>>>

我试图将我的问题缩小到一个最小的例子:

#include <algorithm>
#include <map>
#include <string>
#include <vector>

int main()
{
    std::vector<int> result;
    std::map<std::string, std::pair<unsigned int, std::vector<int>>> other;

    if (true)
    {
        std::for_each(other.begin(), other.end(),
            [&](std::pair<std::string, std::pair<unsigned int, std::vector<int>>> & data)
            {
                result.insert(result.end(), data.second.second.begin(), data.second.second.end());
            });
    }

    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
std::向量结果;
std::映射其他;
如果(真)
{
std::for_each(other.begin(),other.end(),
[&](标准::对和数据)
{
insert(result.end(),data.second.second.begin(),data.second.second.end());
});
}
返回0;
}
我得到一个编译器错误:

error C2664: 'void main::<lambda_1b93236899a42921c1aec8d5288e5b90>::operator ()(std::pair<std::string,std::pair<unsigned int,std::vector<int,std::allocator<_Ty>>>> &) const': cannot convert argument 1 from 'std::pair<const _Kty,_Ty>' to 'std::pair<std::string,std::pair<unsigned int,std::vector<int,std::allocator<_Ty>>>> &'
错误C2664:'void main:::operator()(std::pair&)const':无法将参数1从'std::pair'转换为'std::pair&'
据我所知,lambda参数确实是对我们正在迭代的映射所包含的类型的引用。这就是我应该使用的类型,对吗

如果我在
数据
编译之前删除了安培表

为什么?

我不想按值传递每个元素,因为这些集合将在我的实际程序中包含大量数据

如果我将lambda参数替换为
auto&
,它将编译,这使我相信lambda参数中的类型与映射中包含的类型不匹配,但在我看来确实如此。另外,如果类型错误,为什么原始编译没有&呢


我遗漏了什么/不理解什么?

我今天一直在努力解决同样的问题

我解决了在
std::pair
const
中创建键值类型的问题:

[&](std::pair<const std::string, std::pair<unsigned int, std::vector<int>>> & data)
           // ^^^^^
[&](标准::配对和数据)
// ^^^^^
仔细想想,这是很合乎逻辑的:
您不能将
std::map
项的
key\u值更改为其他内容。因此,如果它使用的是
auto
循环和引用,则需要将其指定为
const
值\u type
std::pair
,而不是
std::pair
。不带符号的版本会复制每一对。由于您可以将
常量键
复制到
中,因此一切正常。在lambda的参数类型中添加一个
常量

#include <algorithm>
#include <map>
#include <string>
#include <vector>

int main()
{
    std::vector<int> result;
    std::map<std::string, std::pair<unsigned int, std::vector<int>>> other;

    if (true)
    {
        std::for_each(other.begin(), other.end(),
            [&](std::pair<const std::string, std::pair<unsigned int, std::vector<int>>> & data)
        // Add this const ^^^^^
        {
            result.insert(result.end(), data.second.second.begin(), data.second.second.end());
        });
    }

    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
std::向量结果;
std::映射其他;
如果(真)
{
std::for_each(other.begin(),other.end(),
[&](标准::对和数据)
//加上这个常数^^^^^
{
insert(result.end(),data.second.second.begin(),data.second.second.end());
});
}
返回0;
}

我在原始代码中键入了所有这些内容,我希望在这里的示例中明确说明,以便发现我不知道的任何潜在问题。您缺少键类型的
const
。使用
[&](decltype(其他)::value\u type&)
[&](auto&)
@Oktalist今天花了我几个小时弄清楚:-PIt没有
&
就可以工作,因为
pair
可以隐式转换为
pair
,如果
T
U
可以隐式转换为
V
W
。它与lambda无关。尝试一个普通函数并查看。