如何包含字符串标头?
我正在尝试了解 string
,但不同的来源告诉我要包含不同的标头。
有些人说使用
,但其他人提到 "apstring.h"
。我能够使用 apstring
做一些基本的事情,但我被告知另一个更强大。然而,当我包含
并尝试声明一些字符串变量时,我收到错误。正确的用法是什么?
I'm trying to learn about string
s, but different sources tell my to include different headers.
Some say to use <string.h>
, but others mention "apstring.h"
. I was able to do some basic stuff with apstring
, but I've been told the other one is more powerful. When I include <string.h>
and try to declare some string variables, however, I get errors. What is the proper usage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您想要包含
并使用std::string
:但您真正需要做的是获取 入门级书籍。任何其他方式你都无法正确学习,更不用说在网上搜寻信息了。
You want to include
<string>
and usestd::string
:But what you really need to do is get an introductory level book. You aren't going to learn properly any other way, certainly not scrapping for information online.
告诉您使用 apstring.h 的资料是高级安置课程的材料在计算机科学中。它描述了您将在整个课程中使用的一个字符串类,一些考试问题可能会引用它并希望您对它有一定的熟悉度。除非您已注册该课程或正在准备参加该考试,否则请忽略这些来源。
告诉您使用 string.h 的消息来源要么没有真正谈论 C++,要么已经严重过时了。你或许也应该忽略它们。该标头用于操作以 null 结尾的字符数组(也称为 C 样式字符串)的 C 函数。
在 C++ 中,您应该使用字符串标头。在文件顶部写入
#include
。当你声明一个变量时,类型是string
,并且它位于std
命名空间中,所以它的全名是std::string
。您可以按照大量介绍性文本的示例并在 C++ 源文件顶部使用using namespace std
来避免始终编写该名称的命名空间部分(但通常 不在您可能编写的任何头文件的顶部)。Sources telling you to use apstring.h are materials for the Advanced Placement course in computer science. It describes a string class that you'll use through the course, and some of the exam questions may refer to it and expect you to be moderately familiar with it. Unless you're enrolled in that class or studying to take that exam, ignore those sources.
Sources telling you to use string.h are either not really talking about C++, or are severely outdated. You should probably ignore them, too. That header is for the C functions for manipulating null-terminated arrays of characters, also known as C-style strings.
In C++, you should use the string header. Write
#include <string>
at the top of your file. When you declare a variable, the type isstring
, and it's in thestd
namespace, so its full name isstd::string
. You can avoid having to write the namespace portion of that name all the time by following the example of lots of introductory texts and sayingusing namespace std
at the top of the C++ source files (but generally not at the top of any header files you might write).我没有听说过“apstring”。如果你想在 c++ 中使用字符串,你可以这样做:
我希望这有用
I don't hear about "apstring".If you want to use string with c++ ,you can do like this:
I hope this can avail
如果您使用 C++ 进行编码,则不应使用
string.h
。 C++ 中的字符串属于std::string
类型,它比旧的 C 风格“字符串”更容易使用。使用:获取正确的信息,并使用
std::string s
来声明一个。您可以在此处查看使用std::string
的许多精彩方法。如果您查看了 Stack Overflow 上有关 C 字符串使用的大量问题,您就会明白为什么应该尽可能避免使用它们:-)
You shouldn't be using
string.h
if you're coding in C++. Strings in C++ are of thestd::string
variety which is a lot easier to use than then old C-style "strings". Use:to get the correct information and something
std::string s
to declare one. The many wonderful ways you can usestd::string
can be seen here.If you have a look at the large number of questions on Stack Overflow regarding the use of C strings, you'll see why you should avoid them where possible :-)
C++ 字符串类是
std::string
。要使用它,您需要包含
标头。有关如何使用 std::string 的基础知识,您需要查阅 一本很好的 C++ 入门书。
The C++ string class is
std::string
. To use it you need to include the<string>
header.For the fundamentals of how to use
std::string
, you'll want to consult a good introductory C++ book.也许这个链接会对您有所帮助。
请参阅:std::string 文档。
#include
是最广泛接受的。Maybe this link will help you.
See: std::string documentation.
#include <string>
is the most widely accepted."apstring"
不是标准 C++,在 C++ 中,您需要#include
标头。"apstring"
is not standard C++, in C++, you'd want to#include
the<string>
header.使用这个:
Use this:
为了首先使用字符串头,我们必须将字符串头文件包含为
#include
,然后我们可以在 C++ 中通过以下方式包含字符串头:1)
2)
For using the string header first we must have include string header file as
#include <string>
and then we can include string header in the following ways in C++:1)
2)