list in cpp
//code by Soumyadeep Ghosh
//ig: @soumyadepp
#include <bits/stdc++.h>
using namespace std;
void display_list(list<int>li)
{
//auto variable to iterate through the list
for(auto i:li)
{
cout<<i<<" ";
}
}
int main()
{
//definition
list<int>list_1;
int n,x;
cin>>n;
//taking input and inserting using insert function
for(int i=0;i<n;i++)
{
cin>>x;
list_1.insert(x);
}
//if list is not empty display it
if(list_1.empty()==false)
{
display_list(list_1);
}
list_1.sort(); //sorts the list
list_1.reverse(); //reverses the list
list_1.pop_back(); //deletes last element of the list
list_1.pop_front(); //deletes the first element of the list
display_list(list_1); //function to display the list
return 0;
}
//in addition , you can use nested lists such as list<list<int>> or list<vector<list>> etc
4.4
5
#include <algorithm>
#include <iostream>
#include <list>
int main()
{
// Create a list containing integers
std::list<int> l = { 7, 5, 16, 8 };
// Add an integer to the front of the list
l.push_front(25);
// Add an integer to the back of the list
l.push_back(13);
// Insert an integer before 16 by searching
auto it = std::find(l.begin(), l.end(), 16);
if (it != l.end()) {
l.insert(it, 42);
}
// Print out the list
std::cout << "l = { ";
for (int n : l) {
std::cout << n << ", ";
}
std::cout << "};\n";
}
Thank you!
5
0
3.83
6
template < class T, class Alloc = allocator<T> > class list;
Thank you!
6
0
Are there any code examples left?
New code examples in category C++
-
C++ 2023-04-28 17:44:25
-
C++ 2022-03-27 19:20:39 lists occurrences of characters in the string c++
-
C++ 2022-03-27 18:00:14 variabili in c++
-
C++ 2022-03-27 17:10:08 repeat character n times c++
-
C++ 2022-03-27 15:50:07 delete an array c++
-
C++ 2022-03-27 15:40:12 C++ pointer to base class
-
C++ 2022-03-27 12:15:21 find the graph is minimal spanig tree or not
-
C++ 2022-03-27 11:30:15 multi variable assignment cpp
-
C++ 2022-03-27 11:05:17 c++ pi float
-
C++ 2022-03-27 10:20:12 why exceptions can lead to memory leaks