0

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);
}

这是一个好方法吗?有没有更快的方法?

4

1 回答 1

3

尝试这个:

void set_effect_block(string paramnamestring)
{
    size_t copied = paramnamestring.copy(effe_block, 19);
    effe_block[copied] = '\0';
}

顺便说一句:您可能需要考虑使用const std::string& paramnamestringas 参数editoritems::set_effect_block(),以便不需要复制字符串即可将其传递到函数中。

于 2013-05-05T08:09:44.580 回答