在C ++中为char *添加一个char前缀的最好方法是什么? [英] Best way to add a char prefix to a char* in C++?
本文介绍了在C ++中为char *添加一个char前缀的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要为char *(is cool)添加一个前缀('X')。
I need to add a prefix ('X') to the char* (" is cool").
最好的方法是什么?
最简单的方法是什么?
What is the easiest way?
char a = 'X';
char* b= " is cool";
我需要:
char* c = "X is cool";
到目前为止,我尝试了strcpy-strcat,memcpy;
So far I tried strcpy-strcat, memcpy;
我知道这听起来像一个愚蠢的,未经研究的问题。
我想知道是否有一种方法来添加字符到数组,而不将char转换为字符串。
I'm aware this sounds as a stupid, unresearched question. What I was wondering is whether there is a way to add the char to the array without turning the char into a string.
推荐答案
如何使用C ++标准库而不是C库函数?
How about using C++ standard library instead of C library functions?
auto a = 'X';
auto b = std::string{" is cool"};
b = a+b;
或短:
auto a ='X';
auto b = a+std::string{" is cool"};
请注意,显式转换为字符串是必需的。
note that the explicit cast to string is mandatory.
这篇关于在C ++中为char *添加一个char前缀的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文