std::list c 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17897943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:31:43  来源:igfitidea点击:

array of std::list c

c++arrayslist

提问by Marco Pietrosanto

I can't find any human explaination for this: how can I create an array of lists like

我找不到对此的任何人工解释:如何创建一个列表数组,例如

std::list<int> mylist[size] ?

If I put this in my program it compiles but creates some problems since it doesn't execute any code (the rest of the code works just fine if I write mylist w/out [size])

如果我把它放在我的程序中,它会编译但会产生一些问题,因为它不执行任何代码(如果我写 mylist w/out [size],其余的代码工作得很好)

I was reading somewhere that using primitive arrays of C is not recommended; what are the alternatives?

我在某处读到不推荐使用 C 的原始数组;有哪些选择?

Thanks sorry for newbness

谢谢 对不起

回答by Kevin Anderson

Your declaration is incorrect, and not supported in C++. You're trying to declare a variable-sized array at run-time, which IS a C99 feature, but is not in C++. See here: Array size at run time without dynamic allocation is allowed?

您的声明不正确,并且在 C++ 中不受支持。您正在尝试在运行时声明一个可变大小的数组,这是 C99 功能,但不在 C++ 中。请参阅此处:允许运行时没有动态分配的数组大小?

You have this:

你有这个:

std::list<int> mylist[size]

What you need is this:

你需要的是这个:

std::list<int> mylist[] = new std::list<int>[size];

But this is still a bad idea, as you need to de-allocate it later with delete []. As others have said, you can do it a couple of different ways, all of which are better for c++:

但这仍然是一个坏主意,因为您稍后需要使用delete []. 正如其他人所说,您可以通过几种不同的方式来实现,所有这些方式都更适合 C++:

std::list< std::list< int > > myListOfLists;      // Linked list of linked lists
std::vector< std::list< int > > myVectorOfLists;  // Better, a vector is more like an array
std::array<std::list<int>, 10> this_would_do;     // From above, only works if you know the array size at compile-time

I hope this helps and is clear for you.

我希望这对您有所帮助并且很清楚。

回答by sehe

More specifically,

进一步来说,

#include <array>
#include <list>

int main() {
     std::array<std::list<int>, 10> this_would_do;
}

回答by P0W

//Codes                               // |  Being Human
typedef list<int> L;                  // |  L is now "alias" for list of integers 
const int arr_size=100;               // |  array needs a constant size, list don't
array<L,arr_size> myArrayOfList;      // |  An Array as elements L
list<L> myListOfList;                //  |  A List of elements L 

//A STL list is doubly linked list

回答by Ivan Mushketyk

Vectorcan be a good alternative to C array:

Vector可以很好地替代 C 数组:

#include <vector>
...
std::vector<std::list<int> > vectorOfLists;

回答by A.B.

The syntax to declare a list is as follows

声明列表的语法如下

std::list<T> my_list;

T is a placeholder for any type, including another list, so you can replace T with std::list<int>which gives the following

T 是任何类型(包括另一个列表)的占位符,因此您可以将 T 替换std::list<int>为以下内容

std::list<std::list<int>> my_list;

That declares a list that can hold lists that can hold integers. Note that some compilers may complain about >>, in that case change it to > >

这声明了一个可以容纳可以容纳整数的列表的列表。请注意,某些编译器可能会抱怨>>,在这种情况下将其更改为> >

回答by Mats Petersson

If you actually have a fixed size (e.g. you are having lists of items that have been given a "score" of 0-9), then using a fixed size array is perfectly fine:

如果您实际上有一个固定大小(例如,您有一个“分数”为 0-9 的项目列表),那么使用固定大小的数组就完全没问题了:

std::list<item> scored_items[10]; 

Of course, it's a poor idea if you don't know beforehand how many you actually want. Then either std::list < std::list < item > > myLists;or a vector std::vector < std::list < item> my_lists;would be suitable solutions.

当然,如果你事先不知道你真正想要多少,这是一个糟糕的主意。那么要么std::list < std::list < item > > myLists;或向量std::vector < std::list < item> my_lists;将是合适的解决方案。

The "correct" solution depends on what your ultimate goal is.

“正确”的解决方案取决于您的最终目标是什么。