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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:49:08+00:00 2026-06-13T14:49:08+00:00

I was trying to sort a structure using stl sort in C++ . But

  • 0

I was trying to sort a structure using stl sort in C++ . But i get some error . Is it that we cannot sort a structure in c++ using stl sort or is it my fault in implementation , if its my fault do let me know the correction .

Here’s the code (its very small 🙂 )

#include<iostream>
#include<algorithm>
using namespace std;
struct log {
       int sd;
       int ed;
} log[1000];
bool key(int i,int j) {
 return (log[i].ed<log[j].ed);
}
int main() {
int n,i;
cin>>n;
sort(log,log+n,key);
for (i=0;i<n;i++) cin>>log[i].sd>>log[i].ed;
for (i=0;i<n;i++) cout<<log.sd<<","<<log.ed<<endl;
system("PAUSE");
}
  • 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-13T14:49:09+00:00Added an answer on June 13, 2026 at 2:49 pm

    Yes, you can. But your comparator should take two const log & (not int) arguments.

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

Sidebar

Related Questions

I am trying to sort an NSMutableArray for that I am using comparer selector;
I am trying to write a data structure that I can cycle round, sort
I'm trying to set up a custom sort behaviour for my model/view structure in
I am trying to adapt the underlying structure of plotting code (matplotlib) that is
I have 2 databases that have the same structure, but different data. Both are
I'm trying to make a simple website using mvc but I'm a little confused
I cannot sort a compile time error. I am compiling C project. My main.c
I recently did some work modifying a Python gui app that was using wxPython
I am trying to sort a nested list structure based on the last values
I am trying to sort an array of a 2 element data structure alphabetically.

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.