如何拆分LPWSTR与多个分隔符&在c ++中返回LPWSTR的数组?

时间:2015-08-08 17:02:06

标签: c++ winapi

我需要将LPWSTR拆分为多个分隔符&在c ++中返回LPWSTR的数组。怎么做? 我尝试从以下问题做到: How to split char pointer with multiple delimiters & return array of char pointers in c++?

但它打印?对于每个wstring。怎么了?

我可以这样做吗?如果是这样,我犯了什么错误?如果没有怎么办?

std::vector<wstring> splitManyW(const wstring &original, const wstring &delimiters)
{
    std::wstringstream stream(original);
    std::wstring line;
    vector <wstring> wordVector;

    while (std::getline(stream, line)) 
    {
        std::size_t prev = 0, pos;
        while ((pos = line.find_first_of(delimiters, prev)) != std::wstring::npos)
        {
            if (pos > prev)
            {
                wstring toPush = line.substr(prev, pos-prev);
                //wstring toPushW = toWide(toPush);
                wordVector.push_back(toPush);
            }
            prev = pos + 1;
        }
        if (prev < line.length())
        {
            wstring toPush = line.substr(prev, std::wstring::npos);
            //wstring toPushW = toWide(toPush);
            wordVector.push_back(toPush);
        }
    }
    for (int i = 0; i< wordVector.size(); i++)
    {
        //cout << wordVector[i] << endl;
        wprintf(L"Event message string: %s\n", wordVector[i]);
    }
    return wordVector;
}

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

    vector<wstring> results = splitManyW(original, separators);

    getchar();
}

3 个答案:

答案 0 :(得分:0)

您应该在调试器下执行它。你会立刻看到你的解析是正确的,你的矢量也是如此。

问题是你正在尝试使用格式为%s的旧C wprintf,它需要一个C字符串(一个空终止的char数组),然后传递一个完全不同的对象的std :: string

你可以:

  • 以C方式执行,获取std :: string所包含的C字符串:

    wprintf(L"Event message string: %s\n", wordVector[i].c_str());
    
  • 使用wcout:

    以C ++方式完成
    wcout << L"Event message string: " << wordVector[i] << std::endl;
    

但是你的返回值是不是一个LPWSTR数组,而是一个std :: string的向量。

首先应该将指针数组分配给char数组,然后单独分配char数组,返回...并且不要忘记释放所有内容。

答案 1 :(得分:0)

在打印最终代币时,您无法正确访问wchar_t*中显示的std::wstring。此外,您的输出格式说明符不正确。根据{{​​1}}文档(see here),特别是“如果使用 wprintf 说明符,参数必须是指向数组初始元素的指针l。“。

进行一些修改并删除一些冗余会产生以下结果:

wchar_t

<强>输出

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::wstring;
using std::vector;

std::vector<wstring> splitManyW(const wstring &original, const wstring &delimiters)
{
    std::wstringstream stream(original);
    std::wstring line;
    vector <wstring> wordVector;

    while (std::getline(stream, line))
    {
        std::size_t prev = 0, pos;
        while ((pos = line.find_first_of(delimiters, prev)) != std::wstring::npos)
        {
            if (pos > prev)
                wordVector.emplace_back(line.substr(prev, pos-prev));

            prev = pos + 1;
        }

        if (prev < line.length())
            wordVector.emplace_back(line.substr(prev, std::wstring::npos));
    }

    return wordVector;
}

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

    vector<wstring> results = splitManyW(original, separators);

    for (auto const& w : results)
        wprintf(L"Event message string: %ls\n", w.c_str());

    getchar();
}

注意:我希望使用Event message string: This Event message string: is Event message string: my Event message string: tst Event message string: why Event message string: I Event message string: hate 使用格式化的流输出,但这与您的问题有点无关。

祝你好运。

答案 2 :(得分:-1)

LPWSTR是wchar_t *,所以基本上你需要的是wcstok。