2014-12-13 112 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的结果)。