2017-01-13 1 views
-2

안녕하세요 저는 다음과 같은 코드를 가지고 있습니다. 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() 함수에 코드를 작성해 주시겠습니까? 아마 나는이 방법을 더 이해할 수 있었다. 그리고 왜 내가 수업에서 문자열을 사용하지 않는지 말하지 말아라, 이것은 나의 대학이 요구하는 것 (char)이다. 감사합니다.

+0

"검색"기능을 호출하는 방법에 대해 생각할 수도 있습니다. 'void Search (void)'는 정말 이상한 서명입니다 ... BTW, 사용자 정의'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 컨테이너가 아닌 기본 STL 컨테이너 벡터를 사용하십시오. 더 나은 목록이 될 것입니다. #include <list>