0

私はC++(Windows環境)を使用しています。私は:

LPCWSTR mystring;

これは動作します:

mystring = TEXT("Hello");

しかし、これを行う方法は?:

mystring = ((((create a new string with text = the content which is in another LPCWSTR 'myoldstring'))))

よろしくお願いします!

PS:

mystring = myoldstring; 

動作しますが、新しい文字列は作成されません。同じポインタになります。新しい文字列を作成したい!

4

3 に答える 3

2

<string>C ++標準文字列を使用するには、ヘッダーを含める必要があります。LPCWSTR(その部分を強調して)処理しているので、ワイド文字を処理しているので、(つまり、の代わりに)Wワイド文字列を使用する必要があります。std::wstringstd::string

#include <string>
#include <iostream>
#include <windows.h>

int main() { 
    LPCWSTR x=L"This is a string";

    std::wstring y = x;
    std::wcout << y;
}
于 2012-12-09T14:50:53.487 に答える
2
LPTSTR mystring;
mystring = new TCHAR[_tcslen(oldstring) + 1];
_tcscpy(mystring, oldstring);

... After you are done ...

delete [] mystring;

これは完全なプログラムです

#include <tchar.h>
#include <windows.h>
#include <string.h>

int main()
{
    LPCTSTR oldstring = _T("Hello");

    LPTSTR mystring;
    mystring = new TCHAR[_tcslen(oldstring) + 1];
    _tcscpy(mystring, oldstring);

    // Stuff

    delete [] mystring;


}

それはうまくコンパイルされますcl /DUNICODE /D_UNICODE a.cpp

tcharマクロを使用しました。使用したくない場合は、

#include <windows.h>
#include <string.h>

int main()
{
    LPCWSTR oldstring = L"Hello";

    LPWSTR mystring;
    mystring = new WCHAR[wcslen(oldstring) + 1];
    wcscpy(mystring, oldstring);

    // Stuff

    delete [] mystring;


}

でうまくコンパイルしますcl a.cpp

于 2012-12-09T14:55:45.643 に答える
0

どうですか

string myNewString = std::string(myOldString);

文字列ライブラリのコピーコンストラクタを使用するだけです。

于 2012-12-09T14:33:01.323 に答える