【问题标题】:Memory allocation while declaring a variable to hold list of strings声明一个变量来保存字符串列表时的内存分配
【发布时间】:2018-08-12 01:22:44
【问题描述】:
std::vector<std::string> myList = new std::vector<std::string>();

像上面那样分配内存会不会出错?我确实知道内存是动态分配的,但只是想知道是否可以使用 new 运算符分配内存。

【问题讨论】:

  • 编译器会告诉你这行不通
  • 是的,我只是想知道我是否遗漏了什么。谢谢@Matthias247

标签: c++ c++11 stl containers


【解决方案1】:

是的,因为new std::vector&lt;std::string&gt;() 返回一个指针,而左侧没有指针。

没有错

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

new 不需要。

【讨论】:

    猜你喜欢
    • 2014-05-22
    • 2014-08-27
    • 2012-11-02
    • 1970-01-01
    • 2011-11-12
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多