如何包含字符串标头?

发布于 2024-10-01 05:40:54 字数 258 浏览 6 评论 0原文

我正在尝试了解 string,但不同的来源告诉我要包含不同的标头。

有些人说使用 ,但其他人提到 "apstring.h"。我能够使用 apstring 做一些基本的事情,但我被告知另一个更强大。然而,当我包含 并尝试声明一些字符串变量时,我收到错误。正确的用法是什么?

I'm trying to learn about strings, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

余厌 2024-10-08 05:40:54

您想要包含 并使用 std::string

#include <string>
#include <iostream>

int main()
{
    std::string s = "a string";
    std::cout << s << std::endl;
}

但您真正需要做的是获取 入门级书籍。任何其他方式你都无法正确学习,更不用说在网上搜寻信息了。

You want to include <string> and use std::string:

#include <string>
#include <iostream>

int main()
{
    std::string s = "a string";
    std::cout << s << std::endl;
}

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.

别理我 2024-10-08 05:40:54

告诉您使用 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 is string, and it's in the std namespace, so its full name is std::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 saying using namespace std at the top of the C++ source files (but generally not at the top of any header files you might write).

云之铃。 2024-10-08 05:40:54

我没有听说过“apstring”。如果你想在 c++ 中使用字符串,你可以这样做:

#include<string>
using namespace std;
int main()
{
   string str;
   cin>>str;
   cout<<str;
   ...
   return 0;
}

我希望这有用

I don't hear about "apstring".If you want to use string with c++ ,you can do like this:

#include<string>
using namespace std;
int main()
{
   string str;
   cin>>str;
   cout<<str;
   ...
   return 0;
}

I hope this can avail

找回味觉 2024-10-08 05:40:54

如果您使用 C++ 进行编码,则不应使用 string.h。 C++ 中的字符串属于 std::string 类型,它比旧的 C 风格“字符串”更容易使用。使用:

#include <string>

获取正确的信息,并使用 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 the std::string variety which is a lot easier to use than then old C-style "strings". Use:

#include <string>

to get the correct information and something std::string s to declare one. The many wonderful ways you can use std::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 :-)

美人如玉 2024-10-08 05:40:54

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.

久伴你 2024-10-08 05:40:54

也许这个链接会对您有所帮助。

请参阅:std::string 文档

#include 是最广泛接受的。

Maybe this link will help you.

See: std::string documentation.

#include <string> is the most widely accepted.

一个人的夜不怕黑 2024-10-08 05:40:54

"apstring" 不是标准 C++,在 C++ 中,您需要 #include 标头。

"apstring" is not standard C++, in C++, you'd want to #include the <string> header.

一口甜 2024-10-08 05:40:54

使用这个:

#include <string>

Use this:

#include <string>
享受孤独 2024-10-08 05:40:54

为了首先使用字符串头,我们必须将字符串头文件包含为 #include,然后我们可以在 C++ 中通过以下方式包含字符串头:

1)

string header = "--- Demonstrates Unformatted Input ---";

2)

string header("**** Counts words****\n"), prompt("Enter a text and terminate"
" with a period and return:"), line( 60, '-'), text;

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)

string header = "--- Demonstrates Unformatted Input ---";

2)

string header("**** Counts words****\n"), prompt("Enter a text and terminate"
" with a period and return:"), line( 60, '-'), text;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文