C ++:是“我的文本”一个std :: string,一个* char或一个c字符串? [英] C++: Is "my text" a std::string, a *char or a c-string?
问题描述
我刚刚做了似乎是一个常见的新手错误:
首先,我们阅读很多教程之一,如下所示:
#include< fstream> ;
int main(){
using namespace std;
ifstream inf(file.txt);
//(...)
}
其次,我们尝试在我们的代码中使用类似的东西,像这样:
#include< fstream&
int main(){
using namespace std;
std :: string file =file.txt; //或从返回std :: string的函数中获取文件名
//。
ifstream inf(file);
//(...)
}
被一些神秘的编译器错误消息困惑。
问题是 ifstream 以 const * char
作为构造函数参数。
解决方案是将std :: string转换为const * char 。 / p>
现在,真正的问题是,对于一个新手来说,file.txt或类似的例子在几乎所有的教程都很像一个std :: string。
那么,my text是一个std :: string,c-string还是* char,还是依赖于上下文?
你能提供例子说明我的文本如何根据上下文不同的解释?
谢谢。
- C和C ++都不具有内置字符串数据类型,因此任何双引号字符串你的代码实质上是
const char *
(或const char []
)。 - 在C ++中,
std :: string
是一个字符数组,一个将原始字符串包装到对象中的方便类。通过使用这个,你可以避免自己做(杂乱的)指针算术和内存重分配。 - 大多数标准库函数仍然只需要
char * $
(或
const char *
)参数。 - 可以隐式转换
char * / code>到
std :: string
,因为后者有一个构造函数。 -
c_str()$>将
std :: string
转换为const char *
感谢Clark Gaebel指出const
方法。 ness,jalf和GMan提到它实际上是一个数组。I have just done what appears to be a common newbie mistake:
First we read one of many tutorials that goes like this:
#include <fstream> int main() { using namespace std; ifstream inf("file.txt"); // (...) }
Secondly, we try to use something similar in our code, which goes something like this:
#include <fstream> int main() { using namespace std; std::string file = "file.txt"; // Or get the name of the file // from a function that returns std::string. ifstream inf(file); // (...) }
Thirdly, the newbie developer is perplexed by some cryptic compiler error message.
The problem is that ifstream takes
const * char
as a constructor argument.The solution is to convert std::string to const * char.
Now, the real problem is that, for a newbie, "file.txt" or similar examples given in almost all the tutorials very much looks like a std::string.
So, is "my text" a std::string, a c-string or a *char, or does it depend on the context?
Can you provide examples on how "my text" would be interpreted differently according to context?
[Edit: I thought the example above would have made it obvious, but I should have been more explicit nonetheless: what I mean is the type of any string enclosed within double quotes, i.e. "myfilename.txt", not the meaning of the word 'string'.]
Thanks.
解决方案So, is "string" a std::string, a c-string or a *char, or does it depend on the context?
- Neither C nor C++ have a built-in string data type, so any double-quoted strings in your code are essentially
const char *
(orconst char []
to be exact). "C string" usually refers to this, specifically a character array with a null terminator. - In C++,
std::string
is a convenience class that wraps a raw string into an object. By using this, you can avoid having to do (messy) pointer arithmetic and memory reallocations by yourself. - Most standard library functions still take only
char *
(orconst char *
) parameters. - You can implicitly convert a
char *
intostd::string
because the latter has a constructor to do that. - You must explicitly convert a
std::string
into aconst char *
by using thec_str()
method.
Thanks to Clark Gaebel for pointing out
const
ness, and jalf and GMan for mentioning that it is actually an array.这篇关于C ++:是“我的文本”一个std :: string,一个* char或一个c字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Neither C nor C++ have a built-in string data type, so any double-quoted strings in your code are essentially