在对象矢量中查找对象

时间:2017-01-13 16:34:40

标签: c++ string vector

你好我有一个如下代码,如果我输入cin的字符串与用户的vector中的一个用户的用户名匹配,我希望我的程序显示用户(就像我在main中使用ostream);

#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();

}

你能帮我写一下Search()函数中的代码来完成工作吗?也许我可以这样理解。请不要说为什么我不在课堂上使用字符串,这是我大学要求的(字符)。 谢谢。

1 个答案:

答案 0 :(得分:0)

这样的功能怎么样?这是你搜索的内容吗?

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);
}

请使用默认的STL-Container向量,而不是自定义向量。更好的是一个清单。 #include <list>

相关问题