Start New Discussion within our Software Development Community

Hello! I'm trying to modify opensourced application but due to lack of knowledge, I'm stuck. Explantions are in the comments of the app :-) Thanks in adavance for your support!

void CBaseGame :: FixSlots( )
{
	struct Struct1{
		list<string> UserList;
		list<CGameSlot> PlayerSlots;
	};
	
	list<string>::iterator IT;
	list<CGameSlot>::iterator TI;

	for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); i++ )
	{
		unsigned char PID = (*i)->GetPID( );
	
		if( PID < 13 )
		{
			unsigned char SID = GetSIDFromPID( PID );

			Struct1.UserList.push_back( (*i)->GetName( ) ); // let's say it added "Bob", "Mike", "Jack"
			Struct1.PlayerSlots.push_back( SID ); // let's say it added "0, 1, 2"
		}
	}

	list<Struct1> toSort; // merge ?

	// Here I would like to sort this list by name SO UserList 
	// turns to "Bob", "Jack", "Mike" AND automaticly PlayerSlots to "0, 2, 1"
	toSort.sort( );

	// After this I would like to print them all like ["Bob -> 0", "Jack -> 2", "Mike -> 1"
	// but I have completly no idea how to print from vector struct

}

You should bundle the string and CGameSlot into a struct and make a list of those. Then, you can define the < operator such that structs are compared by the string. For example:

struct Struct1 {
  string User;
  CGameSlot PlayerSlot;
  Struct1(const string& aUser, CGameSlot aPlayerSlot) : User(aUser), PlayerSlot(aPlayerSlot) { };
  bool operator < (const Struct1& rhs) const { return User < rhs.User; };
};

Making a list of the above will allow you to do a sort afterwards by just calling sort() on the list of Struct1 that you generated. To push the elements on the list, you can construct them with:

list<Struct1> my_list;
  for ....
     if ...
       ...
       my_list.push_back(Struct1((*i)->GetName(), SID));
  ...

  my_list.sort();
This article has been dead for over six months. Start a new discussion instead.