将任何字符串转换为固定大小的字符串
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]=' ';
}
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] = ' ';
}
BTW:你可能想要考虑使用const std::string& paramnamestring
作为editoritems::set_effect_block()
的参数,这样字符串就不需要被复制到函数中。
相关文章:
- 删除字符串中具有相同值的任何相邻字母"pair"
- 在处理任何字符大小的模板中使用字符串文本
- 如何创建一个模板化函数,可以在任何具有字符串键的 std::map 上运行?
- 为什么字符串没有更新并且最终没有打印任何内容
- 是否可以对任何语句字符串使用预准备语句?
- 如何在不进行任何其他修改的情况下"pretty print" C++的XML字符串?
- 如何将csv中的数据放入c++中的字符串向量中,而绝对没有任何作用
- 当我在将文件读取到C字符串的2D数组后尝试对任何内容进行定制时,C++分段错误
- 是否可以用类似C/C++(或任何语言)的语言,从作为用户输入的字符串或文件中创建用户定义的数据类型
- 当通过常量和不是字符串的最佳选择时,是否有任何情况?
- 构造字符串向量时是否有任何副本?
- C++ 复制字符串的指定索引之间的任何子字符串
- Cout 一个字符串可以打印任何内容到屏幕上
- 查找字符串是否包含字符串向量的任何一个元素的最佳方法
- 有人知道可以使用 c++ 以字节为单位编码 asm 字符串的任何东西吗?
- 我想知道如何将所有数值与没有任何空格的字符串分开,就像 123we45rt75 一样
- 字符串流在清除后不接受任何数据
- 任何C 字符串库,例如QString(或Java String)
- 除了字符串或宏调用之外,是否存在任何情况,其中"(<"是有效的字符流?
- 带有 std::的任何值unordered_map不能any_cast字符串