Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. cpplinq - How to use the aggregate function - Get longest string a string array

cpplinq - How to use the aggregate function - Get longest string a string array

Scheduled Pinned Locked Moved C / C++ / MFC
csharptutorialc++visual-studiolinq
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    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?

    G 1 Reply Last reply
    0
    • M Mc_Topaz

      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?

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      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.)

      M 1 Reply Last reply
      0
      • G Graham Breach

        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.)

        M Offline
        M Offline
        Mc_Topaz
        wrote on last edited by
        #3

        Thank you! Man! I would have never guessed the porting would look like that :omg:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups