+ 2

How can we make list<> in c++

list<> is present in c#.

20th Feb 2018, 12:56 PM
Muneeb Zubair Khan
Muneeb Zubair Khan - avatar
4 ответов
+ 6
Be careful there! The C++ std::list<> actually resembles a linked list. What you're searching for - the actual C++ equivalent of the C# list - is the std::vector<>. It works exactly the same, just replace the word "list" with "vector" in Kinshuk's code.
20th Feb 2018, 2:27 PM
Chris
Chris - avatar
+ 6
No problem. I didn't even know that std::list exists until I read your answer :D
20th Feb 2018, 2:30 PM
Chris
Chris - avatar
+ 5
Steps: 1) Include <list> 2) Define a list object with the element type inside the <>, for the template. 3) Do stuff with the list. E.g. : #include<iostream> #include<list> using namespace std; int main() { list<int> ls; //List of integers. for(int i = 0; i<5; i++) ls.push_back(i+1); // Push 5 integers into 'ls'. for(int i:ls) cout<<i<<" "; // Print 'ls'. } For more information, check out: www.cplusplus.com/reference/list/list
20th Feb 2018, 1:03 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
@Chris Sorry, I thought C#'s list was a linked list as well, but it turns out to be a dynamic array.
20th Feb 2018, 2:28 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar