Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9126661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:05:32+00:00 2026-06-17T07:05:32+00:00

I am trying to sort a not so small vector of strings with self-defined

  • 0

I am trying to sort a not so small vector of strings with self-defined comparing rule which is here:

  bool lexGraph(string const &str1, string const &str2)
{
    string::const_iterator i1 = str1.begin(), i2 = str2.begin();

    while((i1 < str1.end()) && (i2 < str2.end()))
    {
        if(*i1 == ' ')
        {
            i1++;
            continue;
        }
        if(*i2 == ' ')
        {
            i2++;
            continue;
        }
        if(toupper(*i1) < toupper(*i2))
        {
            return true;
        }
        if(toupper(*i1) > toupper(*i2))
        {
            return false;
        }
        i1++, i2++;
    }
    return (str1.length() <= str2.length());
}

I use it in this loop:

vector<string> subset;
    ifstream fin(input);
    ofstream fout(output);
    string buff;
    for(long i = 0; i < 241; i++) 
    {
        getline(fin,buff);
        buff += '\n';
        subset.push_back(buff);

    }
sort(subset.begin(), subset.end(),lexGraph);

I found out that the overflow error occurs with vectors larger than 240. I found that this number can even become smaller if I use a smaller file. Also, strings are never really big. If I cut my function down to

bool lexGraph(string const &str1, string const &str2)
{
    return (str1.length() <= str2.length());
}

the error still occurs. But it doesnt when I use STL sort without an extra parameter.

So, I cant figure where the leak is and I hope for some hint here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:05:34+00:00Added an answer on June 17, 2026 at 7:05 am

    You need a strict-weak ordering. Your function for ordering must return false when called with equal strings. If you compare with <=, it doesn’t work. BTW: I believe that some standard library implementations have a diagnostic mode that could have caught this error for you. Use this, as there are enough ropes in C++ that you can shoot yourself in the foot with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to sort a Vector in java but my Vector is not a
I am trying to sort all of this information into a small table which
I am trying to sort a list of names followed by another string such
I am trying to sort an array in PHP by date and time which
My sort function is clearly not working, I am trying to sort ALL_VIEWS by
I'm trying to write a small piece of Javascript that will sort through a
I saw an earlier post which is trying to do a similar sort of
My first post here. I am trying to sort values displayed from my products
Trying to sort this array of objects according to (1) depth and (2) weight,
Been trying to sort this one out for a while. I'd really appreciate any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.