To get the first element in a C++ std::list, you can use the `front()` method.
In this example,
for std::list and
for output.numbers
with integer elements.front()
method.#include <iostream>
#include <list>
int main() {
std::list<int> numbers = {10, 20, 30, 40, 50};
int firstElement = numbers.front();
std::cout << "First element is: " << firstElement << std::endl;
return 0;
}
First element is: 10
In this example,
for std::list and
for output.names
with string elements.front()
method.#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::string> names = {"Alice", "Bob", "Charlie", "David"};
std::string firstElement = names.front();
std::cout << "First element is: " << firstElement << std::endl;
return 0;
}
First element is: Alice
In this tutorial, we learned How to get First Element in a List in C++ language with well detailed examples.