There is
emplace_back
, for which one would need a constructor. I wouldn't have the AddUser function, make the ctor instead.
Consider the following:
Rename UserData to User and make it a struct instead of a class;
Have a class named UserData with a private std::vector<User> ;
Implement functions that populate (emplace_back) and retrieve data to/from the UserData vector;
User could be a plain struct instead of a class because it has no invariants. It is OK because the collection of User is in a private variable in UserData.
With functions:
|
void AddUser(string, string, string);
|
Provide identifiers (names) for the parameters. One needs a way of referring to each argument, you do this in the implementation, but in my mind knowing that a function takes 3 strings is insufficient for readability.
Make the parameters
const
references, put the
&
next to the type - this is the constructor:
1 2 3 4 5 6 7 8
|
User(const string& NameArg,
const string& PassArg,
const string& AccessArg)
: // introduces initialiser list
Name {NameArg},
Pass {PassArg},
Access {AccessArg}
{}
|
Notice the way I have formatted the code - easier to read IMO. Also that the order is the same as in the class/struct definition.
The Arg part of the identifier is a personal preference; I could have done this:
1 2 3 4
|
: // introduces initialiser list
Name {Name},
Pass {Pass},
Access {Access}
|
Did you mention a need to erase items in the container? If so, a
std::map
may be better. However, note that performance will probably not be measurable in a toy program with a small amount of data, one usually needs millions of items to measure any change in performance.
Also note that std::vector is amazing performance wise compared to other containers. I once did an exercise comparing std::vector with std::map, vector was faster at creating &
sorting, than map was at just creating. I had to put 8 million items into a map before it was quicker than vector. There is a bunch of technical reasons why this is so.
But
std::map
has advantages with
insert
and
emplace
and
try_emplace
in that it checks if there is already a member with that key, which I imagine is what one wants for a container or User names.