cpplinq - How to use the aggregate function - Get longest string a string array
-
In an array of strings I would like to get the string containing the most characters. There are, of course, many ways to solve this, but coming from C# I find LINQ being an elegant tool. I found out that the cpplinq NuGet package give me the same LINQ library as in C#. For my problem - get the longest string in a string array - I can use the LINQ's aggregate function. In C#, I have looked it up, and it can be solved like this:
using System;
using System.Linq;namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var words = new string[] { "a", "bb", "ccc", "dddd" };
var longest = words.Aggregate("", (max, cur) => max.Length > cur.Length ? max: cur); // Returns dddd
Console.WriteLine(longest);
}
}
}I cannot understand how to do this in C++ with the cpplinq package. Basically just porting the C#'s call to a C++'s call of the aggregate function.
#include "pch.h"
#include "cpplinq.hpp"
#include #include int main()
{
std::string words[4] = { "a", "bb", "ccc", "dddd" };
auto longest = "";
//longest = cpplinq::aggregate(); // ???
std::cout << longest << std::endl;
}I have googled for an example in c++ how to use the Aggregate function in cpplinq package, but with no result :( Any suggestions?
-
In an array of strings I would like to get the string containing the most characters. There are, of course, many ways to solve this, but coming from C# I find LINQ being an elegant tool. I found out that the cpplinq NuGet package give me the same LINQ library as in C#. For my problem - get the longest string in a string array - I can use the LINQ's aggregate function. In C#, I have looked it up, and it can be solved like this:
using System;
using System.Linq;namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var words = new string[] { "a", "bb", "ccc", "dddd" };
var longest = words.Aggregate("", (max, cur) => max.Length > cur.Length ? max: cur); // Returns dddd
Console.WriteLine(longest);
}
}
}I cannot understand how to do this in C++ with the cpplinq package. Basically just porting the C#'s call to a C++'s call of the aggregate function.
#include "pch.h"
#include "cpplinq.hpp"
#include #include int main()
{
std::string words[4] = { "a", "bb", "ccc", "dddd" };
auto longest = "";
//longest = cpplinq::aggregate(); // ???
std::cout << longest << std::endl;
}I have googled for an example in c++ how to use the Aggregate function in cpplinq package, but with no result :( Any suggestions?
I've just tried it out here and it seems easy enough:
using namespace cpplinq;
std::string words[4] = { "a", "bb", "ccc", "dddd" };
auto longest = from_array(words)
aggregate(std::string(), [](const std::string &m, const std::string &c) {
return m.length() > c.length() ? m : c; });(I renamed the variables to "m" and "c" because I got an error message about max being a function.)
-
I've just tried it out here and it seems easy enough:
using namespace cpplinq;
std::string words[4] = { "a", "bb", "ccc", "dddd" };
auto longest = from_array(words)
aggregate(std::string(), [](const std::string &m, const std::string &c) {
return m.length() > c.length() ? m : c; });(I renamed the variables to "m" and "c" because I got an error message about max being a function.)