如何在c / c ++中使用字符串定界符为每个拆分字符串做一个函数?

我正在努力实现以下目标:

  1. 使用定界符分割字符串。
  2. 在每个拆分字符串上使用一个函数。
  3. 到目前为止,这是我的“实现”:
void for_each_split_line(char* str, char delim, std::function<void(char*)> fn) {
    char* line = (char*)"";
    for (size_t i = 0; i < strlen(str); i++) {
        if (str[i]==delim) {
            fn(line);
            line = (char*)"";
        } else {
            size_t len = strlen(line);
            char* str2 = (char*)malloc(len+2);
            strcpy(str2, line);
            str2[len] = str[i];
            str2[len+1] = '\0';
            line = str2;
        }
    }
    fn(line);
}

NOTE: I don't use strtok, because for some reason it ignores any consequent delimiters, for e.g. "HELLo\n\nWORLD" becomes "HELLO\nWORLD", but I need that empty string in between as well, for my usecase.

我不知道该怎么做才能使代码适用于字符串定界符。是否可以更改代码,使其与“-”等分隔符一起使用?