定义一个字符串 [英] Defining a string

查看:114
本文介绍了定义一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曾经是一个开发人员多年前,所以我想我可以帮助我的儿子一个项目,但甚至不能定义一个字符串!使用VS Community 2017,C ++模块。试过#include string.h,locale_classes.h等没有运气。此外,当我在网上看到
上的示例时,include使用尖括号但是预先填充的#includes,当我打开一个新项目时,使用双引号(例如#include" stdafx.h")。有什么处理?

Hi, used to be a dev years ago so I thought I could help my son with a project but can't even define a string! Using VS Community 2017, C++ module. Have tried to #include string.h, locale_classes.h etc no luck. Also, when I see examples on the web the include uses angle brackets but the prepopulated #includes, when I open a new project, use double quotes (eg. #include "stdafx.h"). What's the deal with that?

推荐答案

C ++没有内置类型的字符串。 C ++库有一个字符串头(没有string.h,只是字符串),它有一个字符串类型的类。

C++ has no built in type for a string. The C++ library has a string header (no string.h, just string) that has a class for a string type.

#include <string>

int main()
{
    std::string mystring = "Hello World!";
    return 0;
}

这就是C ++支持字符串的方式。它们位于std命名空间中,因此您需要在typename中使用命名空间,或者将名称放入全局命名空间。

This is how C++ supports strings. They are in the std namespace, so you need to either to use the namespace in the typename, or bring the name into the global namespace.

#include <string>

using namespace std; //brings everything in std into the global namespace

int main()
{
    string mystring = "Hello World!"; //notice, just string, no std::string
    return 0;
}





#include <string>

using std::string; //brings only string into the global namespace

int main()
{
    string mystring = "Hello World!";
    return 0;
}

但C ++确实支持C如何处理字符串,但除非你真的需要,否则我们不想提及它们使用它们。

But C++ does support how C did strings, but we don't really like to mention them unless you really need to use them.

对于#include<>之间的区别和#include"",这在
中解释
文档
。但简而言之,<>用于包括系统标题,而"&";用于包含作为项目一部分的标题。

For the difference between #include <> and #include "", this is explained in the documentation. But in short, <> is used to include system headers while "" is used to include headers that are part of your project.


这篇关于定义一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆