将 CString 转换为浮点数数组

Convert a CString to an array of floats

我目前正在使用 MFC 构建某个程序,该程序要求用户输入下面突出显示的一系列数字 CString(为简单起见,我们称之为 aCString)。

我可以使用 'strtok' 将字符串或字符数组转换为浮点数组 没问题。

但我正在努力将 CString 转换为字符串或字符数组,以便我可以进行 pre-mentioned 转换!

-我试过了strcpy

strcpy(my_string, (LPCTSTR)aCString);

但是得到那个错误

char *strcpy(char *,const char *)': cannot convert argument 2 from 'LPCTSTR' to 'const char *'

感谢您的帮助!

CString class template provides the Tokenize member, that can be used to split an input string into individual tokens. The tokens can then be converted to floating point values using the std::stof函数:

std::vector<float> ToFloats( const CString& numbers ) {
    std::vector<float> buffer;
    int start{ 0 };
    CString token = numbers.Tokenize( _T( "," ), start );
    while ( start != -1 ) {
        buffer.push_back( std::stof( { token.GetString(),
                                       static_cast<size_t>( token.GetLength() ) } ) );
        token = numbers.Tokenize( _T( "," ), start );
    }
    return buffer;
}