Compare smaller vector to bigger to check if it differs at the end of smaller












13















We have two vectors of size that depends on runtime and need to check if they are equal - differ elements only after the end of smaller size vector. I used std::equal but the issue is that I need to find first which vector is of smaller size which leads to extra line of code :



#include <vector>
#include <iostream>

int main()
{
std::vector<int> a(1000, 3);
std::vector<int> a1(100, 3);

if(a.size() > a1.size())
{
if(std::equal(a1.begin(), a1.end(), a.begin()))
{
std::cout << "Same a gt a1" << std::endl;
}
}

if(a1.size() > a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a1 gt a" << std::endl;
}
}

if(a1.size() == a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a = a1" << std::endl;
}
}
}


Can the code to compare two vectors or differ only at the end of smaller vector be improved?










share|improve this question

























  • C++11 only? No C++14 by any chance?

    – StoryTeller
    Dec 20 '18 at 8:20






  • 1





    If you don't have an actual problem (like performance requirement your current code does not meet, not necessarily a bug), just want code review, then there is codereview.stackexchange.com

    – hyde
    Dec 20 '18 at 14:25
















13















We have two vectors of size that depends on runtime and need to check if they are equal - differ elements only after the end of smaller size vector. I used std::equal but the issue is that I need to find first which vector is of smaller size which leads to extra line of code :



#include <vector>
#include <iostream>

int main()
{
std::vector<int> a(1000, 3);
std::vector<int> a1(100, 3);

if(a.size() > a1.size())
{
if(std::equal(a1.begin(), a1.end(), a.begin()))
{
std::cout << "Same a gt a1" << std::endl;
}
}

if(a1.size() > a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a1 gt a" << std::endl;
}
}

if(a1.size() == a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a = a1" << std::endl;
}
}
}


Can the code to compare two vectors or differ only at the end of smaller vector be improved?










share|improve this question

























  • C++11 only? No C++14 by any chance?

    – StoryTeller
    Dec 20 '18 at 8:20






  • 1





    If you don't have an actual problem (like performance requirement your current code does not meet, not necessarily a bug), just want code review, then there is codereview.stackexchange.com

    – hyde
    Dec 20 '18 at 14:25














13












13








13


2






We have two vectors of size that depends on runtime and need to check if they are equal - differ elements only after the end of smaller size vector. I used std::equal but the issue is that I need to find first which vector is of smaller size which leads to extra line of code :



#include <vector>
#include <iostream>

int main()
{
std::vector<int> a(1000, 3);
std::vector<int> a1(100, 3);

if(a.size() > a1.size())
{
if(std::equal(a1.begin(), a1.end(), a.begin()))
{
std::cout << "Same a gt a1" << std::endl;
}
}

if(a1.size() > a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a1 gt a" << std::endl;
}
}

if(a1.size() == a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a = a1" << std::endl;
}
}
}


Can the code to compare two vectors or differ only at the end of smaller vector be improved?










share|improve this question
















We have two vectors of size that depends on runtime and need to check if they are equal - differ elements only after the end of smaller size vector. I used std::equal but the issue is that I need to find first which vector is of smaller size which leads to extra line of code :



#include <vector>
#include <iostream>

int main()
{
std::vector<int> a(1000, 3);
std::vector<int> a1(100, 3);

if(a.size() > a1.size())
{
if(std::equal(a1.begin(), a1.end(), a.begin()))
{
std::cout << "Same a gt a1" << std::endl;
}
}

if(a1.size() > a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a1 gt a" << std::endl;
}
}

if(a1.size() == a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a = a1" << std::endl;
}
}
}


Can the code to compare two vectors or differ only at the end of smaller vector be improved?







c++ c++11






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 20 '18 at 13:39









sbecker

26519




26519










asked Dec 20 '18 at 8:10









ProgrammerProgrammer

3,0411852105




3,0411852105













  • C++11 only? No C++14 by any chance?

    – StoryTeller
    Dec 20 '18 at 8:20






  • 1





    If you don't have an actual problem (like performance requirement your current code does not meet, not necessarily a bug), just want code review, then there is codereview.stackexchange.com

    – hyde
    Dec 20 '18 at 14:25



















  • C++11 only? No C++14 by any chance?

    – StoryTeller
    Dec 20 '18 at 8:20






  • 1





    If you don't have an actual problem (like performance requirement your current code does not meet, not necessarily a bug), just want code review, then there is codereview.stackexchange.com

    – hyde
    Dec 20 '18 at 14:25

















C++11 only? No C++14 by any chance?

– StoryTeller
Dec 20 '18 at 8:20





C++11 only? No C++14 by any chance?

– StoryTeller
Dec 20 '18 at 8:20




1




1





If you don't have an actual problem (like performance requirement your current code does not meet, not necessarily a bug), just want code review, then there is codereview.stackexchange.com

– hyde
Dec 20 '18 at 14:25





If you don't have an actual problem (like performance requirement your current code does not meet, not necessarily a bug), just want code review, then there is codereview.stackexchange.com

– hyde
Dec 20 '18 at 14:25












3 Answers
3






active

oldest

votes


















8














You only need one call of std::equal if you calculate the smaller size beforehand. I would refactor the code like this:



#include <vector>
#include <iostream>
#include <algorithm>

int main()
{
std::vector<int> a(1000, 3);
std::vector<int> a1(100, 3);

if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin()))
{
std::cout << "Same" << std::endl;
}
return 0;
}


If you need to preserve the second information about which vector is bigger, you could achieve it like this, for instance:



std::cout << "Same " << ((a.size() == a1.size())? "a = a1" : ((a.size() > a1.size())? "a gt a1" : "a1 gt a")) << std::endl;





share|improve this answer





















  • 2





    I'd suggest using const auto size = std::min(a.size(), a1.size());. This also doesn't perform an unsigned to signed conversion.

    – lubgr
    Dec 20 '18 at 8:17











  • Great idea, added it to the post.

    – Blaze
    Dec 20 '18 at 8:18






  • 1





    The above does not include the 2nd information, e.g. if one of the vector is longer than the other, which the user is distinguishing.

    – Fabio
    Dec 20 '18 at 8:20











  • Good point, I added that as an alternative.

    – Blaze
    Dec 20 '18 at 8:27











  • Also possible to eliminate the variable size by doing if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin())) ...

    – Peter
    Dec 20 '18 at 9:17



















23














Since C++14, you can use std::mismatch and check the pair of iterators returned against the end of each range:



auto it = std::mismatch(a.begin(), a.end(), a1.begin(), a1.end());
if (it.first == a.end() || it.second == a1.end()) {
// Equality
}


You also get to know where the elements start to differ, and if they don't, at which point the bigger vector is bigger (the start of the subrange you don't want to compare).






share|improve this answer





















  • 3





    The exact semantic information the OP wanted in a single algorithm. +1

    – StoryTeller
    Dec 20 '18 at 8:41






  • 2





    Very nice. I did not know that this algorithm existed. +1

    – P.W
    Dec 20 '18 at 8:51





















0














Here is a pure C++11 solution that should work for any sequential container (e.g. std::vector, std::list, std::deque). It uses a custom return type, as the type of comparison you show in the original snippets carries more information than a simple boolean value can incorporate.



enum class CombinedCompareResult {
NotEqual, EqualAndFirstLarger, EqualAndSecondLarger, EqualIncludingSize
};

template <class Rng1, class Rng2>
CombinedCompareResult combinedCompare(const Rng1& rng1, const Rng2& rng2)
{
using std::begin;

const auto elementsToCompare = std::min(rng1.size(), rng2.size());

if (!std::equal(begin(rng1), std::next(begin(rng1), elementsToCompare), begin(rng2)))
return CombinedCompareResult::NotEqual;
else if (rng1.size() == rng2.size())
return CombinedCompareResult::EqualIncludingSize;
else if (rng1.size() > rng2.size())
return CombinedCompareResult::EqualAndFirstLarger;
else
return CombinedCompareResult::EqualAndSecondLarger;
}


This can be used like the following and should result in the identical behavior as the code in the question.



const auto cmp = combinedCompare(lst, a);

if (cmp == CombinedCompareResult::EqualIncludingSize)
std::cout << "Same a = a1" << std::endl;
else if (cmp == CombinedCompareResult::EqualAndFirstLarger)
std::cout << "Same a gt a1" << std::endl;
else if (cmp == CombinedCompareResult::EqualAndSecondLarger)
std::cout << "Same a1 gt a" << std::endl;





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53864663%2fcompare-smaller-vector-to-bigger-to-check-if-it-differs-at-the-end-of-smaller%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    8














    You only need one call of std::equal if you calculate the smaller size beforehand. I would refactor the code like this:



    #include <vector>
    #include <iostream>
    #include <algorithm>

    int main()
    {
    std::vector<int> a(1000, 3);
    std::vector<int> a1(100, 3);

    if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin()))
    {
    std::cout << "Same" << std::endl;
    }
    return 0;
    }


    If you need to preserve the second information about which vector is bigger, you could achieve it like this, for instance:



    std::cout << "Same " << ((a.size() == a1.size())? "a = a1" : ((a.size() > a1.size())? "a gt a1" : "a1 gt a")) << std::endl;





    share|improve this answer





















    • 2





      I'd suggest using const auto size = std::min(a.size(), a1.size());. This also doesn't perform an unsigned to signed conversion.

      – lubgr
      Dec 20 '18 at 8:17











    • Great idea, added it to the post.

      – Blaze
      Dec 20 '18 at 8:18






    • 1





      The above does not include the 2nd information, e.g. if one of the vector is longer than the other, which the user is distinguishing.

      – Fabio
      Dec 20 '18 at 8:20











    • Good point, I added that as an alternative.

      – Blaze
      Dec 20 '18 at 8:27











    • Also possible to eliminate the variable size by doing if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin())) ...

      – Peter
      Dec 20 '18 at 9:17
















    8














    You only need one call of std::equal if you calculate the smaller size beforehand. I would refactor the code like this:



    #include <vector>
    #include <iostream>
    #include <algorithm>

    int main()
    {
    std::vector<int> a(1000, 3);
    std::vector<int> a1(100, 3);

    if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin()))
    {
    std::cout << "Same" << std::endl;
    }
    return 0;
    }


    If you need to preserve the second information about which vector is bigger, you could achieve it like this, for instance:



    std::cout << "Same " << ((a.size() == a1.size())? "a = a1" : ((a.size() > a1.size())? "a gt a1" : "a1 gt a")) << std::endl;





    share|improve this answer





















    • 2





      I'd suggest using const auto size = std::min(a.size(), a1.size());. This also doesn't perform an unsigned to signed conversion.

      – lubgr
      Dec 20 '18 at 8:17











    • Great idea, added it to the post.

      – Blaze
      Dec 20 '18 at 8:18






    • 1





      The above does not include the 2nd information, e.g. if one of the vector is longer than the other, which the user is distinguishing.

      – Fabio
      Dec 20 '18 at 8:20











    • Good point, I added that as an alternative.

      – Blaze
      Dec 20 '18 at 8:27











    • Also possible to eliminate the variable size by doing if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin())) ...

      – Peter
      Dec 20 '18 at 9:17














    8












    8








    8







    You only need one call of std::equal if you calculate the smaller size beforehand. I would refactor the code like this:



    #include <vector>
    #include <iostream>
    #include <algorithm>

    int main()
    {
    std::vector<int> a(1000, 3);
    std::vector<int> a1(100, 3);

    if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin()))
    {
    std::cout << "Same" << std::endl;
    }
    return 0;
    }


    If you need to preserve the second information about which vector is bigger, you could achieve it like this, for instance:



    std::cout << "Same " << ((a.size() == a1.size())? "a = a1" : ((a.size() > a1.size())? "a gt a1" : "a1 gt a")) << std::endl;





    share|improve this answer















    You only need one call of std::equal if you calculate the smaller size beforehand. I would refactor the code like this:



    #include <vector>
    #include <iostream>
    #include <algorithm>

    int main()
    {
    std::vector<int> a(1000, 3);
    std::vector<int> a1(100, 3);

    if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin()))
    {
    std::cout << "Same" << std::endl;
    }
    return 0;
    }


    If you need to preserve the second information about which vector is bigger, you could achieve it like this, for instance:



    std::cout << "Same " << ((a.size() == a1.size())? "a = a1" : ((a.size() > a1.size())? "a gt a1" : "a1 gt a")) << std::endl;






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 20 '18 at 9:26

























    answered Dec 20 '18 at 8:15









    BlazeBlaze

    7,1041832




    7,1041832








    • 2





      I'd suggest using const auto size = std::min(a.size(), a1.size());. This also doesn't perform an unsigned to signed conversion.

      – lubgr
      Dec 20 '18 at 8:17











    • Great idea, added it to the post.

      – Blaze
      Dec 20 '18 at 8:18






    • 1





      The above does not include the 2nd information, e.g. if one of the vector is longer than the other, which the user is distinguishing.

      – Fabio
      Dec 20 '18 at 8:20











    • Good point, I added that as an alternative.

      – Blaze
      Dec 20 '18 at 8:27











    • Also possible to eliminate the variable size by doing if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin())) ...

      – Peter
      Dec 20 '18 at 9:17














    • 2





      I'd suggest using const auto size = std::min(a.size(), a1.size());. This also doesn't perform an unsigned to signed conversion.

      – lubgr
      Dec 20 '18 at 8:17











    • Great idea, added it to the post.

      – Blaze
      Dec 20 '18 at 8:18






    • 1





      The above does not include the 2nd information, e.g. if one of the vector is longer than the other, which the user is distinguishing.

      – Fabio
      Dec 20 '18 at 8:20











    • Good point, I added that as an alternative.

      – Blaze
      Dec 20 '18 at 8:27











    • Also possible to eliminate the variable size by doing if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin())) ...

      – Peter
      Dec 20 '18 at 9:17








    2




    2





    I'd suggest using const auto size = std::min(a.size(), a1.size());. This also doesn't perform an unsigned to signed conversion.

    – lubgr
    Dec 20 '18 at 8:17





    I'd suggest using const auto size = std::min(a.size(), a1.size());. This also doesn't perform an unsigned to signed conversion.

    – lubgr
    Dec 20 '18 at 8:17













    Great idea, added it to the post.

    – Blaze
    Dec 20 '18 at 8:18





    Great idea, added it to the post.

    – Blaze
    Dec 20 '18 at 8:18




    1




    1





    The above does not include the 2nd information, e.g. if one of the vector is longer than the other, which the user is distinguishing.

    – Fabio
    Dec 20 '18 at 8:20





    The above does not include the 2nd information, e.g. if one of the vector is longer than the other, which the user is distinguishing.

    – Fabio
    Dec 20 '18 at 8:20













    Good point, I added that as an alternative.

    – Blaze
    Dec 20 '18 at 8:27





    Good point, I added that as an alternative.

    – Blaze
    Dec 20 '18 at 8:27













    Also possible to eliminate the variable size by doing if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin())) ...

    – Peter
    Dec 20 '18 at 9:17





    Also possible to eliminate the variable size by doing if (std::equal(a1.begin(), a1.begin() + std::min(a.size(), a1.size()), a.begin())) ...

    – Peter
    Dec 20 '18 at 9:17













    23














    Since C++14, you can use std::mismatch and check the pair of iterators returned against the end of each range:



    auto it = std::mismatch(a.begin(), a.end(), a1.begin(), a1.end());
    if (it.first == a.end() || it.second == a1.end()) {
    // Equality
    }


    You also get to know where the elements start to differ, and if they don't, at which point the bigger vector is bigger (the start of the subrange you don't want to compare).






    share|improve this answer





















    • 3





      The exact semantic information the OP wanted in a single algorithm. +1

      – StoryTeller
      Dec 20 '18 at 8:41






    • 2





      Very nice. I did not know that this algorithm existed. +1

      – P.W
      Dec 20 '18 at 8:51


















    23














    Since C++14, you can use std::mismatch and check the pair of iterators returned against the end of each range:



    auto it = std::mismatch(a.begin(), a.end(), a1.begin(), a1.end());
    if (it.first == a.end() || it.second == a1.end()) {
    // Equality
    }


    You also get to know where the elements start to differ, and if they don't, at which point the bigger vector is bigger (the start of the subrange you don't want to compare).






    share|improve this answer





















    • 3





      The exact semantic information the OP wanted in a single algorithm. +1

      – StoryTeller
      Dec 20 '18 at 8:41






    • 2





      Very nice. I did not know that this algorithm existed. +1

      – P.W
      Dec 20 '18 at 8:51
















    23












    23








    23







    Since C++14, you can use std::mismatch and check the pair of iterators returned against the end of each range:



    auto it = std::mismatch(a.begin(), a.end(), a1.begin(), a1.end());
    if (it.first == a.end() || it.second == a1.end()) {
    // Equality
    }


    You also get to know where the elements start to differ, and if they don't, at which point the bigger vector is bigger (the start of the subrange you don't want to compare).






    share|improve this answer















    Since C++14, you can use std::mismatch and check the pair of iterators returned against the end of each range:



    auto it = std::mismatch(a.begin(), a.end(), a1.begin(), a1.end());
    if (it.first == a.end() || it.second == a1.end()) {
    // Equality
    }


    You also get to know where the elements start to differ, and if they don't, at which point the bigger vector is bigger (the start of the subrange you don't want to compare).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 20 '18 at 8:43

























    answered Dec 20 '18 at 8:37









    NelfealNelfeal

    5,2021825




    5,2021825








    • 3





      The exact semantic information the OP wanted in a single algorithm. +1

      – StoryTeller
      Dec 20 '18 at 8:41






    • 2





      Very nice. I did not know that this algorithm existed. +1

      – P.W
      Dec 20 '18 at 8:51
















    • 3





      The exact semantic information the OP wanted in a single algorithm. +1

      – StoryTeller
      Dec 20 '18 at 8:41






    • 2





      Very nice. I did not know that this algorithm existed. +1

      – P.W
      Dec 20 '18 at 8:51










    3




    3





    The exact semantic information the OP wanted in a single algorithm. +1

    – StoryTeller
    Dec 20 '18 at 8:41





    The exact semantic information the OP wanted in a single algorithm. +1

    – StoryTeller
    Dec 20 '18 at 8:41




    2




    2





    Very nice. I did not know that this algorithm existed. +1

    – P.W
    Dec 20 '18 at 8:51







    Very nice. I did not know that this algorithm existed. +1

    – P.W
    Dec 20 '18 at 8:51













    0














    Here is a pure C++11 solution that should work for any sequential container (e.g. std::vector, std::list, std::deque). It uses a custom return type, as the type of comparison you show in the original snippets carries more information than a simple boolean value can incorporate.



    enum class CombinedCompareResult {
    NotEqual, EqualAndFirstLarger, EqualAndSecondLarger, EqualIncludingSize
    };

    template <class Rng1, class Rng2>
    CombinedCompareResult combinedCompare(const Rng1& rng1, const Rng2& rng2)
    {
    using std::begin;

    const auto elementsToCompare = std::min(rng1.size(), rng2.size());

    if (!std::equal(begin(rng1), std::next(begin(rng1), elementsToCompare), begin(rng2)))
    return CombinedCompareResult::NotEqual;
    else if (rng1.size() == rng2.size())
    return CombinedCompareResult::EqualIncludingSize;
    else if (rng1.size() > rng2.size())
    return CombinedCompareResult::EqualAndFirstLarger;
    else
    return CombinedCompareResult::EqualAndSecondLarger;
    }


    This can be used like the following and should result in the identical behavior as the code in the question.



    const auto cmp = combinedCompare(lst, a);

    if (cmp == CombinedCompareResult::EqualIncludingSize)
    std::cout << "Same a = a1" << std::endl;
    else if (cmp == CombinedCompareResult::EqualAndFirstLarger)
    std::cout << "Same a gt a1" << std::endl;
    else if (cmp == CombinedCompareResult::EqualAndSecondLarger)
    std::cout << "Same a1 gt a" << std::endl;





    share|improve this answer




























      0














      Here is a pure C++11 solution that should work for any sequential container (e.g. std::vector, std::list, std::deque). It uses a custom return type, as the type of comparison you show in the original snippets carries more information than a simple boolean value can incorporate.



      enum class CombinedCompareResult {
      NotEqual, EqualAndFirstLarger, EqualAndSecondLarger, EqualIncludingSize
      };

      template <class Rng1, class Rng2>
      CombinedCompareResult combinedCompare(const Rng1& rng1, const Rng2& rng2)
      {
      using std::begin;

      const auto elementsToCompare = std::min(rng1.size(), rng2.size());

      if (!std::equal(begin(rng1), std::next(begin(rng1), elementsToCompare), begin(rng2)))
      return CombinedCompareResult::NotEqual;
      else if (rng1.size() == rng2.size())
      return CombinedCompareResult::EqualIncludingSize;
      else if (rng1.size() > rng2.size())
      return CombinedCompareResult::EqualAndFirstLarger;
      else
      return CombinedCompareResult::EqualAndSecondLarger;
      }


      This can be used like the following and should result in the identical behavior as the code in the question.



      const auto cmp = combinedCompare(lst, a);

      if (cmp == CombinedCompareResult::EqualIncludingSize)
      std::cout << "Same a = a1" << std::endl;
      else if (cmp == CombinedCompareResult::EqualAndFirstLarger)
      std::cout << "Same a gt a1" << std::endl;
      else if (cmp == CombinedCompareResult::EqualAndSecondLarger)
      std::cout << "Same a1 gt a" << std::endl;





      share|improve this answer


























        0












        0








        0







        Here is a pure C++11 solution that should work for any sequential container (e.g. std::vector, std::list, std::deque). It uses a custom return type, as the type of comparison you show in the original snippets carries more information than a simple boolean value can incorporate.



        enum class CombinedCompareResult {
        NotEqual, EqualAndFirstLarger, EqualAndSecondLarger, EqualIncludingSize
        };

        template <class Rng1, class Rng2>
        CombinedCompareResult combinedCompare(const Rng1& rng1, const Rng2& rng2)
        {
        using std::begin;

        const auto elementsToCompare = std::min(rng1.size(), rng2.size());

        if (!std::equal(begin(rng1), std::next(begin(rng1), elementsToCompare), begin(rng2)))
        return CombinedCompareResult::NotEqual;
        else if (rng1.size() == rng2.size())
        return CombinedCompareResult::EqualIncludingSize;
        else if (rng1.size() > rng2.size())
        return CombinedCompareResult::EqualAndFirstLarger;
        else
        return CombinedCompareResult::EqualAndSecondLarger;
        }


        This can be used like the following and should result in the identical behavior as the code in the question.



        const auto cmp = combinedCompare(lst, a);

        if (cmp == CombinedCompareResult::EqualIncludingSize)
        std::cout << "Same a = a1" << std::endl;
        else if (cmp == CombinedCompareResult::EqualAndFirstLarger)
        std::cout << "Same a gt a1" << std::endl;
        else if (cmp == CombinedCompareResult::EqualAndSecondLarger)
        std::cout << "Same a1 gt a" << std::endl;





        share|improve this answer













        Here is a pure C++11 solution that should work for any sequential container (e.g. std::vector, std::list, std::deque). It uses a custom return type, as the type of comparison you show in the original snippets carries more information than a simple boolean value can incorporate.



        enum class CombinedCompareResult {
        NotEqual, EqualAndFirstLarger, EqualAndSecondLarger, EqualIncludingSize
        };

        template <class Rng1, class Rng2>
        CombinedCompareResult combinedCompare(const Rng1& rng1, const Rng2& rng2)
        {
        using std::begin;

        const auto elementsToCompare = std::min(rng1.size(), rng2.size());

        if (!std::equal(begin(rng1), std::next(begin(rng1), elementsToCompare), begin(rng2)))
        return CombinedCompareResult::NotEqual;
        else if (rng1.size() == rng2.size())
        return CombinedCompareResult::EqualIncludingSize;
        else if (rng1.size() > rng2.size())
        return CombinedCompareResult::EqualAndFirstLarger;
        else
        return CombinedCompareResult::EqualAndSecondLarger;
        }


        This can be used like the following and should result in the identical behavior as the code in the question.



        const auto cmp = combinedCompare(lst, a);

        if (cmp == CombinedCompareResult::EqualIncludingSize)
        std::cout << "Same a = a1" << std::endl;
        else if (cmp == CombinedCompareResult::EqualAndFirstLarger)
        std::cout << "Same a gt a1" << std::endl;
        else if (cmp == CombinedCompareResult::EqualAndSecondLarger)
        std::cout << "Same a1 gt a" << std::endl;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 20 '18 at 8:43









        lubgrlubgr

        14k32052




        14k32052






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53864663%2fcompare-smaller-vector-to-bigger-to-check-if-it-differs-at-the-end-of-smaller%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bundesstraße 106

            Verónica Boquete

            Ida-Boy-Ed-Garten