G***G 发帖数: 16778 | 1 一个班有1000个学生,分两组
string [] groupa={'stua','stub',....'stu500'}
如何写下面的判断语句
如果学生名在第一组,那么
string name="wangfei";
if (name belong to groupa) ?????如何写这个判断
{
} | d*****a 发帖数: 110 | 2 StrSet strSet;
//input 1000 students to strSet
string name("wangfei");
if(strSet.find(name) != strSet.end())
{
....
}
【在 G***G 的大作中提到】 : 一个班有1000个学生,分两组 : string [] groupa={'stua','stub',....'stu500'} : 如何写下面的判断语句 : 如果学生名在第一组,那么 : string name="wangfei"; : if (name belong to groupa) ?????如何写这个判断 : { : }
| c*****t 发帖数: 1879 | 3 1000 people isn't a lot. No need fansy algorithms, just compare
1 by 1 is enough. If you want something more fansy, then consider
qsort, then do binary search if you are using C. With C++, can use
container. If the set of string is known at compile time,
may consider perfect hash.
【在 G***G 的大作中提到】 : 一个班有1000个学生,分两组 : string [] groupa={'stua','stub',....'stu500'} : 如何写下面的判断语句 : 如果学生名在第一组,那么 : string name="wangfei"; : if (name belong to groupa) ?????如何写这个判断 : { : }
| S**I 发帖数: 15689 | 4 #include
...
string [] groupa={'stua','stub',....'stu500'};
string name="wangfei";
if ( find ( groupa, groupa + 500, name ) != groupa + 500 )
{
}
Not efficient, but one line job. :) |
|