Jump to content

[C++] Using std::max_element on a vector of pairs

Shoob
Go to solution Solved by Shoob,
1 hour ago, as96 said:

The code you wrote doesn't look wrong at all, and should work perfectly and as far as performance goes I don't see any major issue, however if you HAVE to use the algorithm library for whatever reason you can use the sort function:

http://www.cplusplus.com/reference/algorithm/sort/

and write a function like this to pass to the sort method:


bool myfunction (int prod1, int prod2) 
{ 
	return (prod1.GetPrice() < prod2.GetPrice()); 
}

Then take the last item in the vector.

 

PS: max_element would work in the same way.

Thanks! I didn't think of using another method. 

 

After tinkering with it I came to the conclusion that it's practical to use a lambda expression based on the method you suggested.

 

Here's the code I wrote if anyone has a similar problem in the future:

Product Client::GetMostExpensiveProduct()
{
	auto mostExpensive = max_element(cProducts.begin(), cProducts.end(), [](pair<Product, int>& prod1, pair<Product, int>& prod2) 
	{ 
		return (prod1.first.GetPrice() < prod2.first.GetPrice()); 
	});
	return mostExpensive->first;
}

 

I have a vector of pairs that contains an object from another class and an integer. 

vector<pair <Product, int>> cProducts;

One of the methods I have to make is to find the most expensive product that the client has bought. My initial solution was to not use std::max_element and simply iterate through the vector storing the object with the highest price.

Product Client::GetMostExpensiveProduct()
{
	double max = 0.0f;
	Product mostExpensive;
	for (auto& it : cProducts)
	{
		if (it.first.GetPrice() > max)
		{
			mostExpensive = it.first;
		}
	}
	return mostExpensive;
}

However, it's suggested to use methods from <algorithm> for this assignment. Is there an easy way to use std::max_element on this container (possibly using a lambda expression?) or should I switch to a completely different one for the task?

From salty to bath salty in 2.9 seconds

 

Link to comment
Share on other sites

Link to post
Share on other sites

56 minutes ago, Pandalf said:

I have a vector of pairs that contains an object from another class and an integer. 


vector<pair <Product, int>> cProducts;

One of the methods I have to make is to find the most expensive product that the client has bought. My initial solution was to not use std::max_element and simply iterate through the vector storing the object with the highest price.


Product Client::GetMostExpensiveProduct()
{
	double max = 0.0f;
	Product mostExpensive;
	for (auto& it : cProducts)
	{
		if (it.first.GetPrice() > max)
		{
			mostExpensive = it.first;
		}
	}
	return mostExpensive;
}

However, it's suggested to use methods from <algorithm> for this assignment. Is there an easy way to use std::max_element on this container (possibly using a lambda expression?) or should I switch to a completely different one for the task?

The code you wrote doesn't look wrong at all, and should work perfectly and as far as performance goes I don't see any major issue, however if you HAVE to use the algorithm library for whatever reason you can use the sort function:

http://www.cplusplus.com/reference/algorithm/sort/

and write a function like this to pass to the sort method:

bool myfunction (int prod1, int prod2) 
{ 
	return (prod1.GetPrice() < prod2.GetPrice()); 
}

Then take the last item in the vector.

 

PS: max_element would work in the same way.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, as96 said:

The code you wrote doesn't look wrong at all, and should work perfectly and as far as performance goes I don't see any major issue, however if you HAVE to use the algorithm library for whatever reason you can use the sort function:

http://www.cplusplus.com/reference/algorithm/sort/

and write a function like this to pass to the sort method:


bool myfunction (int prod1, int prod2) 
{ 
	return (prod1.GetPrice() < prod2.GetPrice()); 
}

Then take the last item in the vector.

 

PS: max_element would work in the same way.

Thanks! I didn't think of using another method. 

 

After tinkering with it I came to the conclusion that it's practical to use a lambda expression based on the method you suggested.

 

Here's the code I wrote if anyone has a similar problem in the future:

Product Client::GetMostExpensiveProduct()
{
	auto mostExpensive = max_element(cProducts.begin(), cProducts.end(), [](pair<Product, int>& prod1, pair<Product, int>& prod2) 
	{ 
		return (prod1.first.GetPrice() < prod2.first.GetPrice()); 
	});
	return mostExpensive->first;
}

 

From salty to bath salty in 2.9 seconds

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×