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 8709999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:30:41+00:00 2026-06-13T04:30:41+00:00

I have following program which crashed at upper bound call. I am not getting

  • 0

I have following program which crashed at upper bound call. I am not getting why there is a crash. Any reason why I am having a crash. Thanks for your help and time.

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

using namespace std;

enum quality { good = 0, bad, uncertain };

struct sValue {
   int time;
   int value;
   int qual;
};

struct CompareLowerBoundValueAndTime { 
    bool operator()( const sValue& v, int time ) const {
        return v.time < time;
    } 

    bool operator()( const sValue& v1, const sValue& v2 ) const { 
        return v1.time < v2.time; 
    }

    bool operator()( int time1, int time2 ) const { 
        return time1 < time2; 
    }   

    bool operator()( int time, const sValue& v ) const  { 
        return time < v.time;    
    } 
}; 

struct CompareUpperBoundValueAndTime { 
    bool operator()( const sValue& v, int time ) const {
        return v.time > time;
    } 

    bool operator()( const sValue& v1, const sValue& v2 ) const { 
        return v1.time > v2.time; 
    }

    bool operator()( int time1, int time2 ) const { 
        return time1 > time2; 
    }   

    bool operator()( int time, const sValue& v ) const  { 
        return time > v.time;    
    } 
}; 

class MyClass {

public:
    MyClass() {
        InsertValues();
    }

       void InsertValues();

       int GetLocationForTime(int time);

       void PrintValueContainer();

private:

    vector<sValue> valueContainer;
};

void MyClass::InsertValues() {
    for(int num = 0; num < 5; num++) {
        sValue temp;
        temp.time = num;
        temp.value = num+1;
        temp.qual = num % 2;
        valueContainer.push_back(temp);
    }
}

void MyClass::PrintValueContainer()
{
    for(int i = 0; i < valueContainer.size(); i++) {
        std::cout << i << ". " << valueContainer[i].time << std::endl;
    }
}




int MyClass::GetLocationForTime(int time)
{
    std::vector< sValue >::iterator lower, upper;
    lower =  std::lower_bound(valueContainer.begin(), valueContainer.end(), time, CompareLowerBoundValueAndTime() );

    upper =  std::upper_bound(valueContainer.begin(), valueContainer.end(), time, CompareUpperBoundValueAndTime() ); // Crashing here.

    std::cout << "Lower bound: " << lower - valueContainer.begin() << std::endl;
    std::cout << "Upper bound: " << upper - valueContainer.begin() << std::endl;

   return lower - valueContainer.begin();
}



int main()
{
    MyClass a;
    a.PrintValueContainer();
    std::cout << "Location received for 2: "  << a.GetLocationForTime(2) << std::endl;
    return 0;
}
  • 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-13T04:30:43+00:00Added an answer on June 13, 2026 at 4:30 am

    lower_bound and upper_bound work on a sorted sequence. The sequence has to be sorted using the same comparing function that you pass to both functions.

    When you insert the elements in InsertValues you insert them in ascending order, so your CompareLowerBoundValueAndTime is a correct way to compare them.

    But for upper_bound you’re passing a different compare function. Pass CompareLowerBoundValueAndTime() and it should work.

    Note that CompareLowerBoundValueAndTime is a misleading name. It should be something along the lines of CompareValueAndTimeAscending.

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

Sidebar

Related Questions

I have the following program which I'd like to get it fixed. Not sure
I have the following c program which launches a Gtk Program on ubuntu: #include
I have a following program but for some reason it is throwing an error
I am cross compiling linux kernel. I have the following program which compiles perfectly
i have the following program which goes to database and gets the value, i
I have the following program in which i want to insert the values in
I am getting a hard to reproduce error in the following program in which
I have the following assembly program which displays the letter 'z' and then exits:
I have following program in which I am adding few number to set and
I have the following C99 program which measures performance of simple division operations relative

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.