C++ c++;结构中的快速查找
我有个小问题。我必须写一个函数,我不知道怎么写。 我什么都试过了C++ c++;结构中的快速查找,c++,find,C++,Find,我有个小问题。我必须写一个函数,我不知道怎么写。 我什么都试过了 #include <iostream> #include <stdlib.h> #include <string> #include <list> #include <algorithm> using namespace std; struct sBase { string NAME; string AGE; }; list <sBase&g
#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;
}
如果您能帮助我,我将不胜感激。稍微重构一下,删除(大部分)大写的变量名,留下以下内容:
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;
}
structsbase
{
字符串名;
弦年龄;
};
结构具有类似的名称
{
具有类似的名称(const std::string和name)
:_姓名(姓名)
{}
布尔运算符()(常量sBase和sBase)常量
{
返回(sbase.name==\u name);
}
私人:
std::string _name;
};
列表查找名称(常量列表和输入列表、常量字符串名称、布尔部分)
{
列出结果;
list::const_iterator iter=find_if(input_list.begin(),
input_list.end(),
有像(名字)一样的名字;
if(iter!=input_list.end()){
结果:推回(*iter);
}
返回结果;
}
int main()
{
list mylist_one;//包括人的列表。我使用了另一个函数将人添加到此列表中。
字符串名;
库特
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;
}