为什么要在C ++中使用std :: string超过c风格的字符串? [英] Why should one use std::string over c-style strings in C++?

查看:158
本文介绍了为什么要在C ++中使用std :: string超过c风格的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应始终使用超过c风格的 std :: string 字符串( char * )是几乎每个在这里发布的源代码的建议。虽然建议毫无疑问是好的,但正在解决的实际问题不允许详细阐述建议的为什么?方面。这个问题是作为一个占位符。

"One should always use std::string over c-style strings(char *)" is advice that comes up for almost every source code posted here. While the advice is no doubt good, the actual questions being addressed do not permit to elaborate on the why? aspect of the advice in detail. This question is to serve as a placeholder for the same.

一个好的答案应该涵盖以下方面(详细):

A good answer should cover the following aspects(in detail):


  1. 为什么应该在C ++中对c风格字符串使用 std :: string

  2. #1 中提及的做法有什么缺点(如果有的话)?

  3. #1 中提到的建议的相反方案是什么情况是一个好的做法?

  1. Why should one use std::string over c-style strings in C++?
  2. What are the disadvantages (if any) of the practice mentioned in #1?
  3. What are the scenarios where the opposite of the advice mentioned in #1 is a good practice?


推荐答案


  1. std :: string管理自己的内存,

  2. 您不能将自己的缓冲区用作std :: string。

  3. 您需要将ac字符串/缓冲区传递给希望获取缓冲区所有权的内容(例如第三方C库)。

这篇关于为什么要在C ++中使用std :: string超过c风格的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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