std :: string vs c ++中的字符串 [英] std::string vs string in c++

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

问题描述

可能的重复项:
为什么使用命名空间标准;"认为在C ++中是不好的做法?
使用标准命名空间

我一直在很多不同的论坛上徘徊,我似乎一次又一次地看到这个问题.这是一个非常初学者的问题.

Ive been hovering around a bunch of different forums and i seem to see this pop up every time and again. Its a very much beginner question.

我通常使用以下方式定义程序

I usually define a program with

#include<string>
using namespace std;

string x;

我看到了很多将字符串定义为

I see a bunch of code samples out there who define a string as

std::string.

这是什么目的?是好的做法还是有一些功能?

What is the purpose of this? is it good practice or have some functionality?

推荐答案

正如已经回答的其他答案一样,除非您导入整个 std std :: >名称空间或 std :: string (请参见下文).

As the other answer already stated, using std:: is necessary unless you import either the whole std namespace or std::string (see below).

在我看来,最好使用 std :: string 而不是 string ,因为它明确表明它是 std :: string 和没有其他字符串实现.

In my opinion it's nicer to use std::string instead of string as it explicitly shows that it's a std::string and not some other string implementation.

但是,如果您只想编写 string ,我建议您使用std :: string; 使用,而不要使用命名空间std; .代码>,将其仅导入到您实际需要的全局名称空间中.

If you prefer to write just string though, I'd suggest you to use using std::string; instead of using namespace std; to only import the things into the global namespace that you actually require.

这篇关于std :: string vs c ++中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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