How to create get minimum, maximum, find element, reverse and print a list container with int numbers in C++

1 Answer

0 votes
#include <iostream>
#include <algorithm>
#include <list>

int main()
{
	std::list<int> lst;

	for (int i = 10; i <= 30; i += 2) {
		lst.push_back(i);
	}
	for (auto elem : lst) {
		std::cout << elem << ' ';
	}
	std::cout << std::endl;

	auto indexvalue16 = find(lst.begin(), lst.end(), 16);
	if (lst.end() == indexvalue16)
	{
		std::cout << "item not found" << std::endl;
	}
	else
	{
		std::cout << "item found" << std::endl;
	}

	reverse(lst.begin(), lst.end());
	for (auto elem : lst) {
		std::cout << elem << ' ';
	}
	std::cout << std::endl;

	std::cout << "max: " << *max_element(lst.begin(), lst.end()) << std::endl;

	std::cout << "min: " << *min_element(lst.begin(), lst.end()) << std::endl;
	
	std::cout << std::endl;

	return 0;
}

/*
run:

10 12 14 16 18 20 22 24 26 28 30
item found
30 28 26 24 22 20 18 16 14 12 10
max: 30
min: 10

*/

 



answered Dec 29, 2017 by avibootz

Related questions

1 answer 164 views
1 answer 152 views
1 answer 216 views
1 answer 152 views
1 answer 184 views
1 answer 205 views
...