2017-01-13 29 views
-2

你好,我有一個類似於下面的代碼,並且我希望我的程序向用戶顯示(如我在主要使用ostream),如果我用cin輸入的字符串匹配一個用戶的用戶名用戶的矢量;在一個對象的向量中查找對象

#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()函數中寫代碼來完成任務嗎?也許我可以更多地理解這種方式。請不要說我爲什麼不在課堂上使用字符串,這是我的大學要求的(字符)。 謝謝。

+0

也許你至少可以考慮如何調用'Search'函數。 'void Search(void)'真的是一個奇怪的簽名...順便說一句,如果你使用自定義的'Vector'實現,'#include '是什麼意思? –

回答

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>