简体   繁体   English

将char num [50]复制到std :: string

[英]Copy char num[50] to std::string

How to copy contents of char array to std::string (not conversion) 如何将char数组的内容复制到std :: string(不转换)

Ex : 例如:

void CApp::ChangeState(GameState* state)
{
std::string strImagePath;
// cleanup the current state
if ( !states.empty() ) {
    states.back()->Clean();
    states.pop_back();
    //states.back()->getImagePath( psImagepath );
}

// store and init the new state
states.push_back(state);
states.back()->Init();

//psImagepath = (char*)malloc(sizeof(char)*50);
states.back()->getImagePath( psImagepath );

printf("MenuState Init path %s\n",psImagepath);

}

i want to copy my contents into strImagePath (consider strImage path is declared in class definition) 我想将我的内容复制到strImagePath(考虑在类定义中声明strImage路径)

Just use the string's constructor that takes a char array : 只需使用带有char数组的字符串构造函数

char arr[50];
//Fill your array, do stuff, etc, etc
<...>
std::string str(arr);

Note that the char array that you are passing to the constructor should be null-terminated. 请注意,要传递给构造函数的char数组应为空终止。

您可以使用strImagePath.append(charArray)要追加的char [],或strImagePath.assign(charArray)以取代以前的内容strImagePath通过的char []的内容。

使用构造函数( std::string(const char* str) )或复制赋值运算符( std::string& operator=(const char* str) )。

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

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