小编Vis*_*huu的帖子

vector运算符[]和at()之间有什么区别

我正在乱搞一个指针向量的指针

std::vector<int*>* MyVector;
Run Code Online (Sandbox Code Playgroud)

我试图使用这两种方法访问:

MyVector->at(i);    //This works
MyVector[i]         //This says "Expression must be a pointer to a complete object type"
Run Code Online (Sandbox Code Playgroud)

根据我的理解,向量[] operatorat方法之间的区别在于at方法进行了额外的边界检查,所以我的问题是为什么at方法成功访问元素而[] operator不是?

编辑:

整个代码在这里

#include <vector>
#include <iostream>

std::vector<int*>* MyVector;

int main()
{
    MyVector = new std::vector<int*>;
    MyVector->push_back(new int(5));


    for (unsigned int i = 0; i < MyVector->size(); i++)
    {
        delete MyVector->at(i); //This works
        delete MyVector[i];     //This says "Expression must be a pointer to a complete object type
    }

    system("pause");
}
Run Code Online (Sandbox Code Playgroud)

c++ types pointers stdvector dereference

1
推荐指数
1
解决办法
91
查看次数

标签 统计

c++ ×1

dereference ×1

pointers ×1

stdvector ×1

types ×1