声明变量以保存字符串列表时的内存分配

问题描述 投票:-2回答:1
std::vector<std::string> myList = new std::vector<std::string>();

如上所述分配内存会不会错?我知道内存是动态分配的,但只是想知道是否可以使用new运算符分配内存。

c++ c++11 stl containers
1个回答
1
投票

是的因为new std::vector<std::string>()返回一个指针,而在左侧你没有指针。

这没什么不对

std::vector<std::string> myList;

不需要new

© www.soinside.com 2019 - 2024. All rights reserved.