2014-12-13 121 views
-3

我有一個小問題。我必須寫一個函數,我不知道如何。 我嘗試了一切。C++快速查找結構

#include <iostream> 
#include <stdlib.h> 
#include <string> 
#include <list> 
#include <algorithm> 

using namespace std; 

struct sBase 
{ 
    string NAME; 
    string AGE; 

}; 

list <sBase> FIND_NAME(list <sBase> &MYLIST_ONE, const string Name, bool PART) 
{ 
    list <sBase> NEW; 
    list <sBase>::iterator SOLUTION = find(MYLIST_ONE.begin(), MYLIST_ONE.end(), Name); // FULL OF ERRORS . I NEED HELP HERE 
    NEW.push_back(*SOLUION); 

    return NEW; 
} 

int main() 
{ 
    list <sBase> MYLIST_ONE;//List including people. I used another function that add people to this. 

    string Name 
    cout << "Type a name : "; 
    getline(cin, Name); 

    string ASK; 
    bool PART; 
    string yes = "YES", no = "NO"; 

    cout << "Is name only a part of a text ?(YES OR NO) :"; 
    getline(cin, ASK); 

    if (ASK = yes) 
     PART = true; 
    if (ASK = no) 
     PART = false; 

    list <sBase> MYLIST_ONE = FIND_NAME(list <sBase> &MYLIST, const string Name, bool PART) //FUCTON SHOULD RETURN A NEW LIST INCLUDING ONLY STRUCTURE OF THIS ONE PEARSON 


    system("pause"); 
    return 0; 
} 

IF部分是真的,那意味着它僅僅是文本。例如片段我把約翰,PART true,所以結果可以Johnaka alaka,或約翰花。如果你能幫助我,我將不勝感激。

回答

0

重構了一下,去除(大部分)的大寫變量名離開這個:

struct sBase 
{ 
    string name; 
    string age; 

}; 

struct has_name_like 
{ 
    has_name_like(const std::string& name) 
    : _name(name) 
    {} 

    bool operator()(const sBase& sbase) const 
    { 
     return (sbase.name == _name); 
    } 

private: 
    std::string _name; 
}; 

list <sBase> find_name(const list <sBase>& input_list, const string Name, bool PART) 
{ 
    list <sBase> result; 
    list <sBase>::const_iterator iter = find_if(input_list.begin(), 
               input_list.end(), 
               has_name_like(Name)); 
    if(iter != input_list.end()) { 
     result.push_back(*iter); 
    } 
    return result; 
} 

int main() 
{ 
    list <sBase> mylist_one;//List including people. I used another function that add people to this. 

    string Name; 
    cout << "Type a name : "; 
    getline(cin, Name); 

    string ASK; 
    bool PART = false; 
    static const string yes = "YES", no = "NO"; 

    cout << "Is name only a part of a text ?(YES OR NO) :"; 
    getline(cin, ASK); 

    if (ASK == yes) 
     PART = true; 
    if (ASK == no) 
     PART = false; 

    list <sBase> mylist_two = find_name(mylist_one, Name, PART); 


    system("pause"); 
    return 0; 
} 

將至少編譯。您可能需要重新考慮您嘗試實現的邏輯。你真的想提取一個包含0或1個sBase拷貝的列表嗎?或者你只是想找到它,如果存在的話(在這種情況下只需使用find_if的結果)。