8 Years
Discussion Span
Last Post by skatamatic

A struct can look like this. You will call the struct for sorting in this case, mysort1 as a third argument in a sort, so you pass one a1 and a2 to the struct.

struct mysort1
  bool operator () ( const std::string& a1, const std::string& b1 )
    std::stringstream c1( a1 );
    std::stringstream c2( b1 );
    double d1, d2;
    c1 >> d1;
    c2 >> d2;
    return d1 < d2;

A struct is a simple user-defined data type that can contain other (user defined or standard) datatypes. For example, if you wanted to create a struct of golfer names and scores:

struct SGolfer
  //struct members go here
  char   * _szName;
  int        _iScore;

This can now be called like this:

int main()
   SGolfer Golfer1;
   strcpy(Golfer1._szName, "Tiger Woods");
   Golfer1._iScore = 80;
   //and as an array, and pointer
   SGolfer * pGolfer2;
   SGolfer x[25];

Methods and operator overloading may also be used, like the above post mentions. But I think that's a bit outside the scope of your understanding since you are just being introduced to structs.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.