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

The Archive Base Latest Questions

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

I have following structure enum quality { good = 0, bad, uncertain }; struct

  • 0

I have following structure

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

    struct Value {
       int time;
       int value;
       quality qual;
    };

    class MyClass {

public:
    MyClass() {
        InsertValues();
    }

       void InsertValues();

       int GetLocationForTime(int time);

private:

    vector<Value> valueContainer;
};

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


int MyClass::GetLocationForTime(int time)
{

   // How to use lower bound here.
   return 0;
}

In above code I have been thrown with lot of compile errors. I think I am doing wrong here I am new to STL programming and can you please correct me where is the error? Is there better to do this?

Thanks!

  • 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-13T02:30:27+00:00Added an answer on June 13, 2026 at 2:30 am

    The predicate needs to take two parameters and return bool.

    As your function is a member function it has the wrong signature.

    In addition, you may need to be able to compare Value to int, Value to Value, int to Value and int to int using your functor.

    struct CompareValueAndTime
    {
       bool operator()( const Value& v, int time ) const 
       {
           return v.time < time;
       }
    
       bool operator()( const Value& v1, const Value& v2 ) const 
       {
           return v1.time < v2.time;
       }
    
       bool operator()( int time1, int time2 ) const
       {
           return time1 < time2;
       }
    
       bool operator()( int time, const Value& v ) const
       {
          return time < v.time;
       }
    };
    

    That is rather cumbersome, so let’s reduce it:

    struct CompareValueAndTime
    {
       int asTime( const Value& v ) const // or static
       {
          return v.time;
       }
    
       int asTime( int t ) const // or static
       {
          return t;
       }
    
       template< typename T1, typename T2 >
       bool operator()( T1 const& t1, T2 const& t2 ) const
       {
           return asTime(t1) < asTime(t2);
       }
    };
    

    then:

    std::lower_bound(valueContainer.begin(), valueContainer.end(), time,
       CompareValueAndTime() );
    

    There are a couple of other errors too, e.g. no semicolon at the end of the class declaration, plus the fact that members of a class are private by default which makes your whole class private in this case. Did you miss a public: before the constructor?

    Your function GetLocationForTime doesn’t return a value. You need to take the result of lower_bound and subtract begin() from it. The function should also be const.

    If the intention of this call is to insert here, then consider the fact that inserting in the middle of a vector is an O(N) operation and therefore vector may be the wrong collection type here.

    Note that the lower_bound algorithm only works on pre-sorted collections. If you want to be able to look up on different members without continually resorting, you will want to create indexes on these fields, possibly using boost’s multi_index

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

Sidebar

Related Questions

I have following structure struct { int myData; int myAnotherData; }Value; struct AnotherStructure {
I have the following structure public class MyClass : MyBaseClass<System.Int32> { } In a
I have following data structure (simplified): A giant list of the class TestClass public
I have the following structure: <div class=parent> <div id=child1>Content here</div> <div class=child2>Content here</div> </div>
I have the following structure: class User include Mongoid::Document end class Resource include Mongoid::Document
I have the following structure: Model public class EventEntry : LogEntry { public EventType
I have the following structure. <ul id=browser class=filetree> <li><span class=folder>Folder 1</span> <ul> <li><span class=file>Item
I have a table with the following structure leave_id int(10) No user_id int(10) No
I have following structure of an HTML document: <body> <div class=main> <div class=left></div> <div
I have following structure for check-boxes <input type=checkbox name=reg_field['first_name'] value=1 /> First Name<br />

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.