在lambda中捕获各种向量使元素const
Capturing array of vectors in lambda makes elements const
#include <vector>
void main() {
std::vector<int> test[2];
auto funct = [test](){ test[0].push_back(1); };
funct();
}
结果我得到了
main.cc:5:45:错误:传递'const std :: vector'as'void std :: vector&lt; _tp,_alloc> :: push_back(std :: vector&lt; _tp,_tp,_alloc,_alloc,_alloc,_alloc,_alloc,_alloc,_alloc,_alloc,_alloc> :: value_type&amp;&amp;([with _tp = int;_alloc = std ::分配器;std :: vector&lt; _tp,_Alloc> :: value_type = int]’丢弃预选赛[-fpermissive] auto funct = test {test [0] .push_back(1(;};
如何在不制作其值const
的情况下捕获test
指针?除了使其成为vector<vector<int>>
以外,还有其他方法吗?为什么它甚至成为const?
您可以尝试此。
#include <vector>
int main() {
std::vector<int> test[2];
auto funct = [&test](){ test[0].push_back(1); };
funct();
return 0;
}
#include <vector>
int main() {
std::vector<int> test[2];
auto funct = [test]() mutable { test[0].push_back(1); };
funct();
}
相关文章:
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 在 C++ 中输出枚举类类型的向量元素
- 用向量的向量元素初始化向量的空向量
- 为什么不允许使用可变长度数组作为向量元素?
- 在函数中传递向量元素
- 使用 Rcpp 加速替换迭代算法中的列表和向量元素是否合法?
- 返回向量元素的 l 值的正确方法是什么?
- 当向量增长时,指向向量元素的C++指针是否无效
- 如何在C++中从一个向量元素减去std::unorderede_map向量元素并更新它
- C++:向量元素赋值导致访问冲突
- 将向量元素与字符串元素进行比较,而不初始化向量
- 从2D矢量中找出最小尺寸的向量元素的更好方法
- 当我尝试将一个向量元素的值分配给另一个向量元素时,为什么我的应用程序会崩溃
- 使用递归C++向量元素的总和
- 如何查找为数组中的最低值编制索引的向量元素
- 限制指针访问STL ::向量元素
- 访问索引指向向量元素时的seg故障 - 做错了什么
- 如何使用指针访问向量元素的各个元素
- 向量元素数据损坏了Find()操作
- 将字符值分配给向量元素.C