'operator []'不匹配(操作数类型是'std :: unique_ptr <std :: vector <int>&gt;'和'int')

时间:2018-05-04 12:42:47

标签: c++ vector std smart-pointers

我有[],我正在尝试使用std::unique_ptr运算符访问元素。如何访问#include <memory> #include <vector> int main() { std::unique_ptr<std::vector<int>> x; x[0] = 1; } 中包含的向量的特定索引?

var device = navigator.userAgent.toLowerCase();
var ios = device.match(/(iphone|ipod|ipad)/)
if (ios) {
   $('a').on('click touchend', function(e) {
       var el = $(this);
       var link = el.attr('href');
       window.location = link;
   });
}

由于

1 个答案:

答案 0 :(得分:3)

你有一个指向矢量的指针,所以你必须取消引用它

(*x)[0] = 1;

x->at(0) = 1;

但是,我很好奇为什么你需要动态分配std::vector?该容器已经动态分配了底层数组,因此我只需要x成为std::vector<int>

如果保留指向矢量的指针,至少要确保在使用之前分配对象

auto x = std::make_unique<std::vector<int>>();
相关问题