0

如果我有一个程序读取包含类似于“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_"但我对字符串的理解不够好,无法知道如何设置它。

4

1 回答 1

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`
        }
    }
}
于 2013-10-29T22:54:02.000 回答