Breaking a single string into multiple strings C++?

后端 未结 3 1091
太阳男子
太阳男子 2020-12-21 21:18

I need to input 3 full names separated by commas

Full Name 1: John, Smith, Flynn
Full Name 2: Walter, Kennedy, Roberts
Full Name 3: Sam, Bass, Clinton

3条回答
  •  一生所求
    2020-12-21 22:01

    I think here what you want to is a split method for string class, the method should like this:

    void SplitName(const string& fullName, const string& delims, vector& names)
    {
        size_t i = 0;
        size_t j = 0;
        while (i < fullName.size())
        {
            i = fullName.find_first_not_of(delims, i);
            j = fullName.find_first_of(delims, i);
            if (i < fullName.size())
            {
                names.push_back(fullName.substr(i, j - i));
            }
            i = j;
        }
    }
    

    you can define the ":," as delims, then names[1] is First Name, names[2] is Middle Name, names[3] is Last Name.

提交回复
热议问题