C++ 如何包含字符串标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4103169/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:32:30  来源:igfitidea点击:

How do I include the string header?

c++stringheader-files

提问by Maxpm

I'm trying to learn about strings, but different sources tell my to include different headers.

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

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?

有人说使用<string.h>,但也有人提到"apstring.h"。我可以用 做一些基本的事情apstring,但有人告诉我另一个功能更强大。<string.h>但是,当我包含并尝试声明一些字符串变量时,出现错误。正确的用法是什么?

回答by GManNickG

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

你想包括<string>和使用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.

但是您真正需要做的是获得一本入门级书籍。您不会以任何其他方式正确学习,当然也不会在网上搜索信息。

回答by Rob Kennedy

Sources telling you to use apstring.h are materials for the Advanced Placementcourse in computer science. It describes astring 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.

告诉您使用 apstring.h 的消息来源是计算机科学高级先修课程的材料。它描述您将在整个课程中使用字符串类,一些考试题可能会参考它,并希望您对它有一定程度的熟悉。除非您注册了该课程或正在学习参加该考试,否则请忽略这些来源。

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 Cfunctions for manipulating null-terminated arrays of characters, also known as C-style strings.

告诉您使用 string.h 的消息来源要么不是真正在谈论 C++,要么已经过时了。你也应该忽略它们。该头文件用于操作以空字符结尾的字符数组的C函数,也称为 C 样式字符串。

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 stdnamespace, 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 stdat the top of the C++ source files (but generally notat the top of any header files you might write).

在 C++ 中,您应该使用字符串标头。写#include <string>在文件的顶部。当你声明一个变量时,类型是string,并且它在std命名空间中,所以它的全名是std::string。通过遵循大量介绍性文本的示例并using namespace std在 C++ 源文件的顶部(但通常不在您可能编写的任何头文件的顶部),您可以避免一直编写该名称的名称空间部分。

回答by yubaolee

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

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

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

I hope this can avail

我希望这有用

回答by James McNellis

The C++ string class is std::string. To use it you need to include the <string>header.

C++ 字符串类是std::string. 要使用它,您需要包含<string>标题。

For the fundamentals of how to use std::string, you'll want to consult a good introductory C++ book.

有关如何使用 的基础知识std::string,您需要查阅一本很好的 C++ 入门书籍

回答by paxdiablo

You shouldn't be using string.hif you're coding in C++. Strings in C++ are of the std::stringvariety which is a lot easier to use than then old C-style "strings". Use:

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

#include <string>

to get the correct information and something std::string sto declare one. The many wonderful ways you can use std::stringcan be seen here.

以获得正确的信息和std::string s声明一个。您可以在这里std::string看到许多可以使用的美妙方法。

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 :-)

如果您查看 Stack Overflow 上关于 C 字符串使用的大量问题,您就会明白为什么应该尽可能避免使用它们:-)

回答by Lie Ryan

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

"apstring"是不是标准的C ++,C ++中,你会想#include<string>标题。

回答by prolink007

Maybe this link will help you.

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

See: std::string documentation.

请参阅:std::string 文档

#include <string>is the most widely accepted.

#include <string>是最广为接受的。

回答by fatih

Use this:

用这个:

#include < string>

回答by MD ASIF ALAM

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++:

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

1)

1)

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

2)

2)

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