简体   繁体   English

多维列表 C++ STL 列表

[英]multi dimensional list c++ STL list

whats wrong with this simple program.这个简单的程序有什么问题。 I want to create multi list and insert using c++ STL.我想创建多列表并使用 C++ STL 插入。 its giving segmentation fault.它给出了分段错误。

在此处输入图片说明

#include <iostream>
#include <list>
using namespace std;
int main(){
        list<int> *l;
        l[0].push_back(1);
        l[10].push_back(12);
        cout<<endl;
        return 0;
}

Why are you using a pointer to a list?为什么要使用指向列表的指针? You didn't allocate memory for the list.您没有为列表分配内存。 You could use a container to store multiple lists, eg std:array for static number of elements or std::vector for dynamic number of elements:您可以使用容器来存储多个列表,例如 std:array 用于静态元素数量或 std::vector 用于动态元素数量:

#include <array>
#include <iostream>
#include <list>
#include <vector>
using std::array;
using std::vector;
using std::list;
using std::cout;

int main(){
        std::array<list<int>, 11> l;
        l[0].push_back(1);
        l[10].push_back(12);


        std::vector<list<int>> l2(11);
        l2[0].push_back(1);
        l2[10].push_back(12);
        cout << '\n';
        return 0;
}

list<int> *l; makes l a pointer to a list<int> but it doesn't actually create a list<int> - and definitely not an array of list<int> which you are trying to access.使l成为指向list<int>的指针,但它实际上并没有创建list<int> - 而且绝对不是您要访问的list<int>数组。

Possible solutions.可能的解决方案。

Plain C fixed size arrays of list<int> : list<int>普通 C 固定大小数组:

#include <iostream>
#include <list>

int main() {
    std::list<int> l[11];            // place for 11 list<int>`'s
    l[0].push_back(1);
    l[10].push_back(12);
}

Using the C++ fixed sized std::array:使用 C++ 固定大小的 std::array:

#include <array>
#include <list>

int main() {
    std::array<std::list<int>, 11> l; // place for 11 list<int>'s
    l[0].push_back(1);
    l[10].push_back(12);
}

Using a C++ std::vector that allows for dynamicially adding more list<int> 's:使用允许动态添加更多list<int>的 C++ std::vector

#include <list>
#include <vector>

int main() {
    std::vector<std::list<int>> l(11); // starts with place for 11 list<int>'s
    l[0].push_back(1);
    l[10].push_back(12);
}

list<int> *l; is a pointer to list, it is not initialized, so adding elements to it has undefined behaviour.是一个指向列表的指针,它没有被初始化,所以向它添加元素具有未定义的行为。 Either you initialize it to a variable like:要么将其初始化为一个变量,如:

list<int> l;
l.push_back(1);
l.push_back(12);

In this case you can only access the elements that already have data, l[0] or l[1].在这种情况下,您只能访问已经有数据的元素,l[0] 或 l[1]。

Or you need to allocate space to the number of elements you need in your list.或者您需要为列表中所需的元素数量分配空间。

For instance:例如:

list<int> l[20];
l[0].push_back(1);
l[10].push_back(12);

In list<int> *l;list<int> *l; is creating a pointer to a list .正在创建一个指向list的指针。 Before accessing l you need to assign valid address to it.在访问l之前,您需要为其分配有效地址。

Something like this,像这样的东西,

list<int> l;
list<int> *l2 = &l;

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

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