简体   繁体   中英

Finding object in a vector of objects

Hello I have a code like the following, and I want my program to show the user(like i would use ostream in main) if the string i enter with cin matches one user's username from the user's vector;

#include<iostream>
#include<vector>
using namespace std;
class User{

private:

char *username;
char *password;
int age;

public:
User(){..}
User(char *,char *p, int a){...}
~User(){..};
friend ostream &operator<<(ostream &output, User &u)
{
cout<<"User: "<<u.username<<endl;
cout<<"Pass: "<<u.password<<Endl;
}
char* getUsername(){ return username};
char* getPassword(){return password};
};



template <class T> class Vector
{
private:
    T* vect;
    int dim;

public:
    Vector()
    {
        dim = 0;
        vect = NULL;
    }

    Vector(T*vect, int dim)
    {
        this->dim = dim;
        this->vect = new T(this->dim);
        for (int i = 0; i < this->dim; i++)
            this->vect[i] = vect[i];
    }

    Vector(Vector &v)
    {
        this->dim = v.dim;
        this->vect = new T(this->dim);
        for (int i = 0; i < this->dim; i)
            this->vect[i] = v.vect[i];
    }

    ~Vector()
    {
        if (vect != NULL)
            delete[]vect;
    }

    void output_vect()
    {
        cout << "Elements are: " << endl;
        for (int = 0; this->dim; i++)
        {
            cout << this->vect[i] << endl;
            cout << endl;
        }
    }

    void sort_vect()
    {
        T aux;
        for (int i = 0; i<this->dim - 1; i++)
            for (int j = i + 1; j<this->dim; j++)
                if (this->vect[i] < this->vect[j])
                {
                    aux = this->vect[i];
                    this->vect[i] = this->vect[j];
                    this->vect[j] = aux;
                }
    }

    Vector operator+(Vector &v)
    {
        Vector temp;
        temp.dim = this->dim + v.dim;
        temp.vect = new T[temp.dim];
        for (int i = 0; i < this->dim; i++)
            temp.vect[i] = this->vect[i];
        for (int j = 0; j < v.dim; j++)
            temp.vect[j + this->dim] = v.vect[j];
        return temp;
    }


void Search()
{
    //i know this code isn't right in this function, but I really don't know much about templates and stuff;


Vector<Userr> vectuser(users, 2);


    string wanteduser;
    cout << "Type the username you want to find:" << endl;
    cin >> wanteduser;

    vector<User>vstl;


    if (find(vstl.begin(), vstl.end(), wanteduser)!= vstl.end())
        vstl.push_back(users[wanteduser]);
}

void main()

{

User u1("John","34f",20);
User u2("Kim","fdfg",18);

    User users[2] = { u1,u2 };

    Vector<User> vectusers(users,2);
Search();

}

Can you please help me write the code in Search() function to get things done? Maybe I could understand more this way. And please don't say why I don't use strings in class, this is what my university requires(char). Thank you.

What about a function like this? Is it what you searched for?

bool searchUserByName(std::vector<User>& users, User& target, std::string name) {
    for (std::vector<User>::iterator it = users.begin(); it != users.end(); it++) 
        if ((*it).getUsername() == name)  {
            target = *it;
            return (true);
        }

    return (false);
}

And please use the default STL-Container vector, not a custom one. Better would be a list. #include <list>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM