找回密码
 立即注册
搜索

std::string分割

[复制链接]

std::string分割

[复制链接]
admin

108

主题

4

回帖

1万

积分

管理员

积分
18115
2023-10-29 20:10:21 | 显示全部楼层 |阅读模式
  1. #include <vector>
  2. using namespace std;
  3. std::vector<std::string> vStringSplit(const  std::string& s, const std::string& delim = ",")
  4. {
  5.         std::vector<std::string> elems;
  6.         size_t pos = 0;
  7.         size_t len = s.length();
  8.         size_t delim_len = delim.length();
  9.         if (delim_len == 0) return elems;
  10.         while (pos < len)
  11.         {
  12.                 int find_pos = s.find(delim, pos);
  13.                 if (find_pos < 0)
  14.                 {
  15.                         elems.push_back(s.substr(pos, len - pos));
  16.                         break;
  17.                 }
  18.                 elems.push_back(s.substr(pos, find_pos - pos));
  19.                 pos = find_pos + delim_len;
  20.         }
  21.         return elems;
  22. }
复制代码
  1. int main()
  2. {
  3.         auto out1 = vStringSplit("www.mtctp.com", ".");
  4.         for (int i = 0; i < out1.size(); i++)
  5.         {
  6.                 std::cout << out1[i] << std::endl;
  7.         }
  8. }
复制代码


QQ|Archiver|小黑屋|mtctp |

GMT+8, 2024-5-8 13:07 , Processed in 0.050955 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表