如何使用多个分隔符拆分char指针&在c ++中返回char指针数组?

时间:2015-08-07 17:18:43

标签: c++ arrays pointers winapi

在这个问题Split char* to char * Array的副本中,建议使用字符串而不是char *。但我需要使用LPWSTR。由于它是char *的typedef,我更喜欢使用char *。我尝试使用以下代码,它输出错误:

char**splitByMultipleDelimiters(char*ori,char deli[],int lengthOfDelimiterArray)
{
    char*copy = ori;
    char** strArray = new char*[10];
    int j = 0;
    int offset = 0;
    char*word = (char*)malloc(50);
    int length;
    int split = 0;
    for(int i = 0; i < (int)strlen(ori); i++)
    {
        for(int k = 0; (k < lengthOfDelimiterArray) && (split == 0);k++)
        {
            if(ori[i] == deli[k])
            {
                split = 1;
            }
        }
        if(split == 1)//ori[i] == deli[0]
        {
            length = i - offset;
            strncpy(word,copy,length);
            word[length] = '\0';
            strArray[j] = word;
            copy = ori + i + 1;
            //cout << "copy: " << copy << endl;
            //cout << strArray[j] << endl;
            j++;
            offset = i + 1;
            split = 0;
        }
    }
    strArray[j] = copy;
   // string strArrayToReturn[j+1];
    for(int i = 0; i < j+1; i++)
    {
        //strArrayToReturn[i] = strArray[i];
        cout << strArray[i] << endl;
    }
    return strArray;
}

void main()
{
        char*ori = "This:is\nmy:tst?why I hate";
        char deli[] = {':','?',' ','\n'};

        int lengthOfDelimiterArray = (sizeof(deli)/sizeof(*deli));
        splitByMultipleDelimiters(ori,deli,lengthOfDelimiterArray);
}

还有其他方法可以拆分LPWSTR吗?

1 个答案:

答案 0 :(得分:-1)

等等,你在说什么?我在代码中的任何地方都没有看到LPWSTR。你想转换到LPWSTR吗?如果是这样,那就有standard library功能。还有一个用于拆分多个字符的standard library-based解决方案。总而言之,您的代码可能如下所示:

#include <codecvt>
#include <cstdio>
#include <locale>
#include <sstream>
#include <string>

using std::string;
using std::wstring;

wstring toWide(const string &original)
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
    return converter.from_bytes(narrow_utf8_source_string);
}

std::vector<wstring> splitMany(const string &original, const string &delimiters)
{
    std::stringstream stream(original);
    std::string line;

    while (std::getline(original, line)) 
    {
        std::size_t prev = 0, pos;
        while ((pos = line.find_first_of(delimeters, prev)) != std::string::npos)
        {
            if (pos > prev)
                wordVector.push_back(line.substr(prev, pos-prev));
            prev = pos + 1;
        }
        if (prev < line.length())
            wordVector.push_back(line.substr(prev, std::string::npos));
    }
}

int main()
{
    string original = "This:is\nmy:tst?why I hate";
    string separators = ":? \n"

    std::vector<wstring> results = splitMany(original, separators);
}

此代码使用标准库来实现这些功能,并且比手动执行它更不容易出错。

祝你好运!

修改:要明确wstring == LPWSTR == wchar_t*

修改2 :要将string转换为wstring

#include <codecvt>
#include <locale>
#include <string>

using std::string;
using std::wstring;

string toMultiByte(const wstring &original)
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
    return converter.to_bytes(original);
}