简体   繁体   English

声明列表时的std :: bad_alloc C ++

[英]std::bad_alloc on declaration of a list c++

I'm trying to play around with lists and create a list of lists that contain integers. 我正在尝试使用列表并创建包含整数的列表的列表。 After I run the code it thorws a bad alloc exception: 在运行代码后,它会解决一个错误的alloc异常:

"aterminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc “在抛出'std :: bad_alloc'what()实例之后调用的aterminate:std :: bad_alloc

This application has requested the Runtime to terminate it in an unusual way. 该应用程序已请求运行时以一种异常方式终止它。 Please contact the application's support team for more information." 请与应用程序的支持团队联系以获取更多信息。”

#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;
}

I'm running it in Clion on Windows 10 with minGW. 我正在使用minGW在Windows 10的Clion中运行它。 How can I fix this? 我怎样才能解决这个问题? everything seems right. 一切似乎都正确。

I compiled you code inside of Visual Studio 2015. Your code compiled with no errors and the output was as follows. 我在Visual Studio 2015中编译了您的代码。您的代码进行了编译,没有错误,输出如下。

a1234 a1234

End 结束

5678 5678

End 结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM