In C++, structures and classes are very similar. The way I see it, the best way to deal with structures is to write custom constructors and functions belonging to the structures you define.
This is one way to look at your problem:
Code:
#include <iostream>#include <vector>
#include <string>
using namespace std;
/* Player definition */
struct Player
{
/* Structure fields */
string surname;
string initial;
string firstname;
string club;
int normalgrade;
int rapidgrade;
string gradingnumber;
int dmno;
/* Constructors - you can write as many constructors as you like,
depending on which fields you plan to initialize */
Player(string _surname, string _firstname) : surname(_surname), firstname(_firstname) {}
/* This constructor creates a new Player object with the supplied surname and firstname */
Player() : surname("") {}
/* This constructor creates an "empty" (invalid) player. We can use this to mark
non-existing players in a search, as shown below */
static Player findPlayer(vector<Player> players, string surname, string firstname = "");
/* This function finds a player with the supplied surname and firstname in a vector
of Player objects. The first name is optional - if you do not call the function with
the firstname parameter, it will only search by surname. */
void printPlayer();
/* This function writes a player's credentials to stdout.*/
};
Player Player::findPlayer(vector<Player> players, string surname, string firstname) {
for(vector<Player>::iterator it = players.begin(); it != players.end(); ++it) {
if(firstname == "") {
if((*it).surname == surname) {
return *it;
}
} else if((*it).surname == surname && (*it).firstname == firstname) {
return *it;
}
}
return Player();
}
void Player::printPlayer() {
if(surname == "") {
return;
}
cout << surname << " " << firstname << endl;
}
int main() {
vector<Player> players;
players.push_back(Player(string("Smith"), string("John")));
players.push_back(Player(string("Doe"), string("Jane")));
players.push_back(Player(string("Bar"), string("Foo")));
Player::findPlayer(players, string("Doe"), string("Jane")).printPlayer();
Player::findPlayer(players, string("Brown"), string("Mark")).printPlayer();
return 0;
}
Constructors belonging to the Player structure create Player objects filling their fields with the values supplied as constructor parameters. Furthermore, functions defined within the structure definition operate on Player objects. I hope you'll find this example useful