【问题标题】:std::bad_alloc on declaration of a list c++std::bad_alloc 关于列表 c++ 的声明
【发布时间】:2017-01-06 18:26:55
【问题描述】:

我正在尝试使用列表并创建一个包含整数的列表列表。 在我运行代码后,它会抛出一个错误的分配异常:

"在抛出 'std::bad_alloc' 的实例后调用终止 什么():std::bad_alloc

此应用程序已请求运行时以不寻常的方式终止它。 请联系应用程序的支持团队了解更多信息。”

#include <iostream>
#include <list>
#include <string>

int main() {
    std::list<int> l{1,2,3,4};//
    std::list<int> l1{5,6,7,8};
    std::list<std::list<int>> ll;// list of lists<int>
    std::cout << "a"; //does not reach here, terminated before this row
    ll.push_back(l);
    ll.push_back(l1);
    std::list<std::list<int>>::iterator itr;
    for (itr=ll.begin(); itr != ll.end(); itr++)
    {
        std::list<int>tl=*itr;
        std::list<int>::iterator it;
        for (it=tl.begin(); it != tl.end(); it++)
        {
            std::cout<<*it;
        }
        std::cout<<std::endl<<"End"<<std::endl;
    }
    return 0;
}

我正在使用 minGW 在 Windows 10 上的 Clion 中运行它。 我怎样才能解决这个问题?一切似乎都是正确的。

【问题讨论】:

标签: c++


【解决方案1】:

我在 Visual Studio 2015 中编译了你的代码。你的代码编译没有错误,输出如下。

a1234

结束

5678

结束

【讨论】:

  • 嗨,欢迎来到堆栈溢出。请添加代码。照原样,很难说问题是什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
相关资源
最近更新 更多