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

  • Home
  • SEARCH
  • 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 6724577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:42:22+00:00 2026-05-26T09:42:22+00:00

Writing a program that should be portable in Linux and Windows enviroments I have

  • 0

Writing a program that should be portable in Linux and Windows enviroments I have found a issue with the STL sort function, when compiling with Visual studio and gcc.
In order to sort a vector of complex data structures I have written an int conversion operator for the structures in that form:

struct result
{ 
public :
  int Gene_a;
  int Gene_b;
  std::vector<int> score;
  float total_score;
  operator int() {return total_score;}
}

In that case I have no problem in visual studio using the standard sort algorithm for integers:

sort(results.rbegin(),results.rend());

But when trying to compile that using GCC ( actually g++ ) that lead to funny errors.
To avoid that seems that I have to write an ordering function:

 inline bool better (result a, result b)
 {
   return a.total_score > b.total_score;
 }

and invoke sort in the form:

sort(results.begin(),results.end(),better);

Was I using something out of the standard C++ or is a lack of the g++ STL implementation ?
Is it possible to let g++ understand that the vector of struct is equivalent to a vector of int ?

Here is a short main to illustrate the error:

#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) 
{
  vector<result> r;           // define a vector of struct
  for (int i=0;i<10;i++)      // fill up with data
  {
    result a;
    a.Gene_a=i;
    a.Gene_b=2*i;
    for(int j=0;j<i;j++)
      a.score.push_back(i); // fill the int vector in the struct
    a.total_score=i;
    r.push_back(a);
  }
  // sort(r.rbegin(),r.rend()); // this line will fail in g++
  sort(r.rbegin(),r.rend(),better);
  for (int i=0;i<10;i++)      // demonstrate that the int operator works
    cout << (int)r[i] << endl;
}// End main
  • 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-05-26T09:42:23+00:00Added an answer on May 26, 2026 at 9:42 am

    The only meaningful difference I see between the two comparison methods is that the second will work when the element is const. The first should probably be:

    //             vvvvv
    operator int() const
    

    Even though you’re modifying the container, your comparison still has the requirement that it work on const objects. In your case, the implementation used that assumption and the error arose.

    But this shouldn’t matter, since to sort the container and its elements needs to be modifiable…

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

Sidebar

Related Questions

Im writing a program that should read input via stdin, so I have the
I'm writing a C program under Windows that should send an ENTER key to
I am writing a program that should have the following layout: I've tried this
I am writing a program that is meant to be extended by some function
I'm writing a Linux program that currently compiles and works fine on x86 and
I am writing a program/script that should check what services are running on a
I'm writing a program that uses the Qt Graphics View framework. I have subclassed
I'm writing a program that should parse and reply to network packets but I'm
I am writing a program that should search through a directory that the user
In my Linux system I'm writing a program that dynamically loads some .so libraries

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.