以下是一个用于分割文本的 C++ 函数:

#include <iostream>
#include <cstring>

using namespace std;

const int MAXN = 100;

int splitText(const string& text, const char delimiter, string* result) {
    int len = text.length(), cnt = 0, start = 0;
    for (int i = 0; i < len; i++) {
        if (text[i] == delimiter) {
            result[cnt++] = text.substr(start, i - start);
            start = i + 1;
        }
    }
    result[cnt++] = text.substr(start, len - start);
    return cnt;
}

int main() {
    string t = "你好,我是,懒人,天天,向上";
    string result[MAXN];
    int cnt = splitText(t, ',', result);
    for (int i = 0; i < cnt; i++) {
        cout << result[i] << endl;
    }
    return 0;
}

该函数将文本按照指定的分隔符进行分割,并将结果存储到一个字符串数组中。函数的参数包括待分割的文本、分隔符和存储结果的字符串数组。函数返回值为分割后的字符串数组长度。

在本例中,将字符串 "你好,我是,懒人,天天,向上" 按照逗号进行分割,并将分割结果输出到控制台上

c++ 中写一段用来分割文本的代码 比如有一个文本 QString t=你好我是懒人天天向上 用 进行切割然后返回数组 不要用vector 封装好一个函数给我

原文地址: https://www.cveoy.top/t/topic/cjCE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录