如果我有一个节目阅读的文件包含“Boss1 Dave Jim Boss3 Coleen Boss 7 Harris Todd Elaine”的内容,我试图将输入分成一个老板列表和一个名单,我会检查输入的每个字符串,但我怎么能判断这个人是否是老板?我会设置这样的东西开始:
void read(std::istream& is, Bosses<std::string>& bossList, Employees<std::string> empList){
std::string name;
while(!is.eof()){
is >> name;
}
}
但我需要检查while循环是否有name=="Boss_"
这样的东西,但我不太了解字符串,知道如何设置它。
答案 0 :(得分:0)
您可以使用插入运算符来解析名称字符串,因为您的字符串是以空格分隔的,并且插入运算符会跳过空格(默认情况下):
void read(std::istream& is, Bosses<std::string>& bossList, Employees<std::string> empList) {
std::string temp;
std::vector<std::string> all_names;
//the insertion operator reads the string and bypasses all whitespace by default
while (is >> temp) {
//find bosses by using `std::string
if (temp.find("Boss") != std::string::npos) {
boosList.push_back(temp);//you didn't state what container `Bosses` uses but, `push_back is valid for `lists`, `vectors` and `deques`
}
else {
empList.push_back(temp);//you didn't state what container `Employees` uses but, `push_back is valid for `lists`, `vectors` and `deques`
}
}
}