[英]Convert any string to fixed size string
我在以下代碼中使用set_effect_block
將字符串轉換為20字節的固定大小的字符串。
class editoritems{
public:
editoritems(string= "");
void set_effect_block(string paramnamestring) //set effect block
{
const char *effectnamevalue=paramnamestring.data();
int length=strlen(effectnamevalue);
length=(length<20?length:19);
strncpy_s(effe_block,effectnamevalue,length);
effe_block[length]='\0';
}
string get_effect_block()const{return effe_block;}
private:
char effe_block[20];
};
editoritems::editoritems(string h)
{
set_effect_block(h);
}
這是這樣做的好方法嗎? 有沒有更快的方法?
嘗試這個:
void set_effect_block(string paramnamestring)
{
size_t copied = paramnamestring.copy(effe_block, 19);
effe_block[copied] = '\0';
}
順便說一句:您可能要考慮使用const std::string& paramnamestring
作為editoritems::set_effect_block()
參數,這樣就無需復制字符串即可將其傳遞給函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.