- 经常遇到字符串分割问题,但是相对于c++而言实现比较麻烦,直接遍历一遍也很冗余

- 另外也适用于,在字符串中找到某个字符的所有位置

//函数功能:将输入字符串s,以字符串c(;)进行拆分,拆分结果放在v中
//函数参数说明:s为输入字符串;c为拆分的字符串;v为拆分结果
//函数返回值:正常返回0
int split_string(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
    std::string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while (std::string::npos != pos2)
    {
        v.push_back(s.substr(pos1, pos2 - pos1));

        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if (pos1 != s.length())
        v.push_back(s.substr(pos1));
    return 0;
}

相关文章:

  • 2021-06-11
  • 2022-12-23
  • 2021-11-22
  • 2022-12-23
  • 2022-01-18
  • 2022-12-23
  • 2021-06-17
猜你喜欢
  • 2022-02-10
  • 2021-06-28
  • 2022-12-23
  • 2018-07-14
  • 2021-10-31
  • 2021-09-12
  • 2022-12-23
相关资源
相似解决方案