L
L
LamerFromSpace2019-04-06 19:25:13
OOP
LamerFromSpace, 2019-04-06 19:25:13

How to use the class vector?

There is a class with methods sortByField and search, sort and look for values ​​in the vector of users, respectively.

class User
{
public:
  void sortByField(vector<User>& arr, int field);
  void search(vector<User>& arr, int field, string str);
  ...
private:
  string fullName;
  string birthDate;
  ...

The vector itself is declared in the main file. Something seems to me that this approach is crooked, because the class method works with a vector that is not in the class itself, can the vector be transferred to the class itself as a field?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly, 2019-04-06
@LamerFromSpace

Well, only such methods should not belong to the user class. It would be better to create a new class like Database which is used to store users. And this class can have methods like find or sort. Only the signature of these methods already of course does not contain any vectors: the vector is an implementation detail in this case, which can be changed later.

H
h0w4rd, 2019-04-06
@h0w4rd

As I understand it, functions should work with user data (i.e. vector<User>& arr). So you need to take it out as a field and it is advisable not to forget about adding a new parameter to the class initialization. Well, otherwise, if these functions simply sort vectors or whatever, then remove them from the class.

V
vanyamba-electronics, 2019-04-07
@vanyamba-electronics

class UserVector : public std::vector<User>
{
   typedef std::vector<User> ParentT;
public:
   UserVector& sort(int field);
   ParentT::size_type find(int field, const std::string& request);
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question