C++ 传递一个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4475634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Pass A String
提问by Josh
Quick probably obvious question.
快速可能是显而易见的问题。
If I have:
如果我有:
void print(string input)
{
cout << input << endl;
}
How do I call it like so:
我该如何称呼它:
print("Yo!");
It complains that I'm passing in char *, instead of std::string. Is there a way to typecast it, in the call? Instead of:
它抱怨我传递的是 char *,而不是 std::string。有没有办法在通话中对其进行类型转换?代替:
string send = "Yo!";
print(send);
Thanks.
谢谢。
回答by In silico
You can write your function to take a const std::string&
:
您可以编写函数以获取const std::string&
:
void print(const std::string& input)
{
cout << input << endl;
}
or a const char*
:
或const char*
:
void print(const char* input)
{
cout << input << endl;
}
Both ways allow you to call it like this:
这两种方式都允许您像这样调用它:
print("Hello World!\n"); // A temporary is made
std::string someString = //...
print(someString); // No temporary is made
The second version does require c_str()
to be called for std::string
s:
第二个版本确实需要c_str()
为std::string
s调用:
print("Hello World!\n"); // No temporary is made
std::string someString = //...
print(someString.c_str()); // No temporary is made
回答by Davis King
You should be able to call print("yo!") since there is a constructor for std::string which takes a const char*. These single argument constructors define implicit conversions from their aguments to their class type (unless the constructor is declared explicit which is not the case for std::string). Have you actually tried to compile this code?
您应该能够调用 print("yo!") 因为 std::string 有一个构造函数,它采用 const char*。这些单参数构造函数定义了从它们的参数到它们的类类型的隐式转换(除非构造函数被声明为显式,而 std::string 不是这种情况)。您是否真的尝试过编译此代码?
void print(std::string input)
{
cout << input << endl;
}
int main()
{
print("yo");
}
It compiles fine for me in GCC. However, if you declared print like this void print(std::string& input)
then it would fail to compile since you can't bind a non-const reference to a temporary (the string would be a temporary constructed from "yo")
它在 GCC 中对我来说编译得很好。但是,如果您像这样声明 printvoid print(std::string& input)
则它将无法编译,因为您无法将非常量引用绑定到临时引用(该字符串将是从“yo”构造的临时引用)
回答by EboMike
Well, std::string
is a class, const char *
is a pointer. Those are two different things. It's easy to get from string
to a pointer (since it typically contains one that it can just return), but for the other way, you need to create an object of type std::string
.
嗯,std::string
是一个类,const char *
是一个指针。这是两个不同的东西。很容易得到string
一个指针(因为它通常包含一个可以直接返回的指针),但对于另一种方式,您需要创建一个类型为 的对象std::string
。
My recommendation: Functions that take constant strings and don't modify them should always take const char *
as an argument. That way, they will always work - with string literals as well as with std::string
(via an implicit c_str()
).
我的建议:采用常量字符串且不修改它们const char *
的函数应始终作为参数。这样,它们将始终有效 - 使用字符串文字以及使用std::string
(通过隐式c_str()
)。
回答by svens
print(string ("Yo!"));
You need to make a (temporary) std::string
object out of it.
您需要从中制作一个(临时)std::string
对象。
回答by Fiktik
The obvious way would be to call the function like this
显而易见的方法是像这样调用函数
print(string("Yo!"));
回答by Lightness Races in Orbit
Make it so that your function accepts a const std::string&
instead of by-value. Not only does this avoid the copy and is therefore alwayspreferable when accepting strings into functions, but it also enables the compiler to construct a temporary std::string
from the char[]
that you're giving it. :)
使您的函数接受 aconst std::string&
而不是 by-value。这不仅避免了复制,因此在将字符串接受到函数中时总是更可取的,而且它还使编译器能够std::string
根据char[]
您提供的构造一个临时的。:)
回答by Ron Tackaberry
Just cast it as a const char *. print((const char *)"Yo!") will work fine.
只需将其转换为 const char * 即可。print((const char *)"Yo!") 可以正常工作。