Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C++;?_C++_String_Char_Boolean_Concatenation - Fatal编程技术网

C++ 在C++;?

C++ 在C++;?,c++,string,char,boolean,concatenation,C++,String,Char,Boolean,Concatenation,我想做如下事情: bool b = ... string s = "Value of bool is: " + b ? "f" : "d"; 我看到的所有示例都使用cout,但我不想打印字符串;把它储存起来 我该怎么做?如果可能的话,我想要一个分配给char*的示例,一个分配给std::string的示例,我将使用std::stringstream: std::stringstream ss; ss << s << (b ? "f" : "d"); std::strin

我想做如下事情:

bool b = ...
string s = "Value of bool is: " + b ? "f" : "d";
我看到的所有示例都使用
cout
,但我不想打印字符串;把它储存起来


我该怎么做?如果可能的话,我想要一个分配给
char*
的示例,一个分配给
std::string
的示例,我将使用
std::stringstream

std::stringstream ss;
ss << s << (b ? "f" : "d");
std::string resulting_string = ss.str();
std::stringstream-ss;

ss我会使用
std::stringstream

std::stringstream ss;
ss << s << (b ? "f" : "d");
std::string resulting_string = ss.str();
std::stringstream-ss;
ss使用:


注意:发布的代码几乎是正确的(不可能
+
two
char[]
):


要分配给
char[]
,可以使用
snprintf()

或者只是
std::string::c_str()

使用:


注意:发布的代码几乎是正确的(不可能
+
two
char[]
):


要分配给
char[]
,可以使用
snprintf()

或者只要
std::string::c_str()

如果您的编译器足够新,它应该具有:

这当然会将
“1”
(对于
true
)或
“0”
(对于
false
)附加到字符串中,而不是您想要的
“f”
“d”
。原因是没有采用
bool
类型的
std::to_string
重载,因此编译器将其转换为整数值

当然,您可以分两步完成,首先声明字符串,然后附加值:

string s = "Value of bool is: ";
s += b ? "f" : "d";
或者像现在这样做,但将第二个显式创建为
std::string

string s = "Value of bool is: " + std::string(b ? "f" : "d");
编辑:如何从
std::string

这是通过该方法完成的。但是正如Pete Becker所指出的,您必须小心如何使用这个指针,因为它指向string对象内部的数据。如果对象被销毁,数据也被销毁,指针如果保存,现在将无效。

如果您的编译器足够新,它应该具有:

这当然会将
“1”
(对于
true
)或
“0”
(对于
false
)附加到字符串中,而不是您想要的
“f”
“d”
。原因是没有采用
bool
类型的
std::to_string
重载,因此编译器将其转换为整数值

当然,您可以分两步完成,首先声明字符串,然后附加值:

string s = "Value of bool is: ";
s += b ? "f" : "d";
或者像现在这样做,但将第二个显式创建为
std::string

string s = "Value of bool is: " + std::string(b ? "f" : "d");
编辑:如何从
std::string


这是通过该方法完成的。但是正如Pete Becker所指出的,您必须小心如何使用这个指针,因为它指向string对象内部的数据。如果对象被销毁,数据也被销毁,指针如果保存,现在将无效。

分两步执行操作:

bool b = ...
string s = "Value of bool is: ";
s+= b ? "f" : "d";

这是必要的,因为否则您将尝试对两个
常量char*
求和,这是不允许的;相反,通过这种方式,您依赖于
std::string
和C字符串的
+=
运算符的重载。

分两步执行该操作:

bool b = ...
string s = "Value of bool is: ";
s+= b ? "f" : "d";
这是必要的,因为否则您将尝试对两个
常量char*
求和,这是不允许的;相反,通过这种方式,
std::string
和C字符串依赖于
+=
运算符的重载。

简单点:

bool b = ...
string s = "Value of bool is: ";
if (b)
  s += "f";
else
  s += "d";
简单一点:

bool b = ...
string s = "Value of bool is: ";
if (b)
  s += "f";
else
  s += "d";
您也可以使用strcat()

您也可以使用strcat()

很简单:

std::string s = std::string("Value of bool is: ") + (b ? "f" : "d");
很简单:

std::string s = std::string("Value of bool is: ") + (b ? "f" : "d");
封装:

std::string toString(const bool value)
{
    return value ? "true" : "false";
}
然后:

封装:

std::string toString(const bool value)
{
    return value ? "true" : "false";
}
然后:


对于这个简单的用例,只需将一个字符串附加到另一个字符串:

std::string text = std::string("Value of bool is: ").append( value? "true" : "false" ); 
现在,对于更通用的解决方案,您可以创建字符串生成器类:

class make_string {
   std::ostringstream st;
   template <typename T>
   make_string& operator()( T const & v ) {
       st << v;
   }
   operator std::string() {
       return st.str();
   }
};
类生成字符串{
std::奥斯汀河街;
模板
生成字符串和运算符()(T常量和v){

对于这个简单的用例,只需将一个字符串附加到另一个字符串:

std::string text = std::string("Value of bool is: ").append( value? "true" : "false" ); 
现在,对于更通用的解决方案,您可以创建字符串生成器类:

class make_string {
   std::ostringstream st;
   template <typename T>
   make_string& operator()( T const & v ) {
       st << v;
   }
   operator std::string() {
       return st.str();
   }
};
类生成字符串{
std::奥斯汀河街;
模板
生成字符串和运算符()(T常量和v){

如何分配给
字符*
?我试图调用一个方法签名中需要它的函数。@asdf4lyfe call
c_str()
在字符串上,它会给你一个
常量字符*
谢谢!@Joachim Pileborg,如果你能把它编辑成一个好的答案的话。@Peter-是的,但是要注意生命周期问题。返回的字符*指向字符串对象内的内存,如果字符串对象被修改或破坏,它就会消失。分配如何到
char*
?我试图调用一个方法签名中需要它的函数。@asdf4lyfe call
c_str()
在字符串上,它会给你一个
常量字符*
谢谢!@Joachim Pileborg,如果你能把它编辑成一个好的答案的话。@Peter-是的,但是要注意生命周期问题。返回的字符*指向字符串对象内的内存,如果字符串对象被修改或销毁,它就会消失。