简体   繁体   English

将std :: string转换为const tchar *

[英]convert std::string to const tchar*

I have a function that takes in a parameter called arrayOfStrings defined as below: 我有一个函数,它接受一个名为arrayOfStrings的参数,定义如下:

const TCHAR* arrayOfStrings[] = { L"Test" };

Now I would like to convert strings to the type above, but I don't know how. 现在,我想将字符串转换为上述类型,但是我不知道如何。 This link provides a solution for converting string to tchar, but not const tchar*. 该链接提供了将字符串转换为tchar而不是const tchar *的解决方案。 This other link shows how to convert string to tchar*, but not const tchar*, and the memory allocation is a problem for me with this second solution. 另一个链接显示了如何将字符串转换为tchar *,而不是const tchar *,第二种解决方案对我来说是内存分配的问题。 As you can probably tell, I am very new to c++, so any educational tips would be appreciated as well. 您可能会说,我对c ++还是很陌生,所以任何教育技巧也将不胜感激。

A simple approach, which will work with all C++ standards, would be 一种适用于所有C ++标准的简单方法是

 #include <string>

 #include <windows.h>    //   or whatever header you're using that specifies TCHAR

 int main()
 {
       std::string test("Hello");     //   string to be converted

       //   first, if you need a const TCHAR *

       std::basic_string<TCHAR> converted(test.begin(), test.end());

       const TCHAR *tchar = converted.c_str();

       //   use tchar as it is in the required form (const)

       //   second, if you need a TCHAR * (not const)

       std::vector<TCHAR> converted2(test.begin(), test.end());

       TCHAR *tchar2 = &converted2[0];

       // use tchar2 as it is of the required form (non-const).

 }

std::basic_string does not provide a means in all C++ standards to obtain a non- const pointer to its data, but std::vector does. std::basic_string并非在所有C ++标准中都提供一种获取指向其数据的非const指针的方法,但std::vector却提供了一种方法。 (Assuming you don't use an explicit conversion to introduce or remove const ness). (假设您不使用显式转换来引入或删除const )。

In C++17 and later, things are simpler: the basic_string::data() has both a const and non- const overload, which wasn't the case before the 2017 standard. 在C ++ 17和更高版本中,事情变得更简单: basic_string::data()同时具有const和非const重载,这在2017年标准之前不是这种情况。 Before C++11, the data in a basic_string was not guaranteed to be contiguous by the standard (even if implementations typically implemented things that way) but c_str() did provide the address of the first character of a contiguous array. 在C ++ 11之前,标准不保证basic_string的数据是连续的(即使实现通常以这种方式实现),但是c_str()确实提供了连续数组的第一个字符的地址。 The net effect is that, in C++17 and later, appropriate overloads of basic_string::data() or basic_string::c_str() can be used, without a need for cast to change const ness, and without resorting to a vector (which has been guaranteed to have contiguous elements in all C++ standards). 最终的结果是,在C ++ 17和更高版本中,可以使用basic_string::data()basic_string::c_str()适当重载,而无需强制转换来更改const ,也无需使用vector (已保证在所有C ++标准中它们都具有连续的元素)。

Points to note in both cases 两种情况下的注意事项

  1. The pointers ( tchar and tchar2 ) are invalidated if their respective containers ( converted or converted2 ) are resized or if they cease to exist. 如果指针( tchartchar2 )各自的容器( convertedconverted2 )被调整大小或不再存在,则它们将无效。 For example, don't use the data pointed to by tchar if converted has passed out of scope, since data tchar points at will no longer exist. 例如,不使用数据指向tchar如果converted已经传递出的范围,因为数据tchar在将不复存在点。
  2. It is simply undefined behaviour to use the pointers to run past the end (no magical resizing when using the pointers). 使用指针运行到最后是完全未定义的行为(使用指针时没有神奇的大小调整)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM