list question

Started by
16 comments, last by pbivens67 7 months, 4 weeks ago

my question is how do I find the maximum value in a list, I get an error with largest[0] line of code

#include <iostream>
#include <list>

using namespace std;

int main()
{
	int n = 0, num = 0;
	list<int> largest{};

	cout << "Enter number of values (1-100): ";
	cin >> n;
	cout << endl;

	for (int i = 0; i < n; i++)
	{
		cout << "Enter number: " << i + 1 << " : ";
		cin >> num;
		largest.push_back(num);
	}

	for (int i = 1; i < n; i++)
	{
		if (largest[0] < largest[i])
		{
			largest[0] = largest[i];
		}
	}

	return 0;
}
Advertisement

pbivens67 said:
my question is how do I find the maximum value in a list, I get an error with largest[0] line of code

List has no operator[], due to how a list is defined/implemented you cannot randomly access elements in a list with an index. You need to use iterators/range-based for to iterate over the loop (you already know how those look), or algorithms (https://en.cppreference.com/w/cpp/algorithm/max_element).

Juliean said:

pbivens67 said:
my question is how do I find the maximum value in a list, I get an error with largest[0] line of code

List has no operator[], due to how a list is defined/implemented you cannot randomly access elements in a list with an index. You need to use iterators/range-based for to iterate over the loop (you already know how those look), or algorithms (https://en.cppreference.com/w/cpp/algorithm/max_element).

the link does not work​

can you show me how iterators work

Oh this MFing forum-software and links…

for (auto itr = largest.begin(); itr != largest.end(); ++itr)
{
	const auto element = *itr;
}

Note that list is an extremely choice for your algorithm. std::vector has operator[] and not a single disadvantage over the list here (which in turn has significiant overhead in terms of memory and performance; even if you get it to work) - unless you absolutely need list (or know that it's a benefit for your specific situation), never use list!

I am studying lists in my book

is this how to merge two lists? list<int> list1=list2;

std::list<int> List1{1,2,3};

std::list<int> List2{4,5,6};

List1.insert(List1.end(), List2.begin(), List2.end());

I agree with Juliean; don't use lists. I've been C++ programming for 10 years, I don't think I've used a linked list in anything I've created.

C++ std::list is a great solution to problems we rarely encounter in building games.

It offers great performance on certain operations for data manipulation that we usually don't need. If you know they are present, and you know you have a need for it, it's a great tool in the toolbox. Are you splicing, splitting, and joining tremendous amounts of data that needs to be immovable with references never invalidated, that's great, but it means you're probably not making games.

scott8 said:

std::list<int> List1{1,2,3};

std::list<int> List2{4,5,6};

List1.insert(List1.end(), List2.begin(), List2.end());

I agree with Juliean; don't use lists. I've been C++ programming for 10 years, I don't think I've used a linked list in anything I've create.

I know, but it is in the book I am reading.

pbivens67 said:

scott8 said:

std::list<int> List1{1,2,3};

std::list<int> List2{4,5,6};

List1.insert(List1.end(), List2.begin(), List2.end());

I agree with Juliean; don't use lists. I've been C++ programming for 10 years, I don't think I've used a linked list in anything I've create.

I know, but it is in the book I am reading.

Read a better book. At least if you want to learn about list, have examples where lists make sense. Lists for example have fast erase-speed when you have the iterator. Build some examples around that. Just using list in cases where vector can be exchanged or is easier to use won‘t teach you nothing about lists at all.

This topic is closed to new replies.

Advertisement