你什么时候会使用数组而不是向量/字符串? [英] When would you use an array rather than a vector/string?

查看:29
本文介绍了你什么时候会使用数组而不是向量/字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名初学 C++ 程序员,所以我学会了使用数组而不是向量(这似乎是做事的一般方法,稍后再转向向量).

I'm a beginner C++ programmer and so I've learnt using arrays rather than vectors (this seems to be the general way to do things, then move on to vectors later on).

我注意到很多关于 SO 的答案都建议在数组上使用向量,在字符数组上使用字符串.这似乎是在 C++ 中编码的正确"方式.

I've noticed that a lot of answers on SO suggest using vectors over arrays, and strings over char arrays. It seems that this is the "proper" way to code in C++.

说了这么多,什么时候还值得使用经典的数组/字符*(如果有的话)?

That all being said, when is it still worth using a classic array/char* (if ever)?

推荐答案

在编写应该在其他项目中使用的代码时,尤其是当您面向可能不存在 STL 的特殊平台(嵌入式、游戏机等)时.

When writing code that should used in other projects, in particular if you target special platforms (embedded, game consoles, etc.) where STL might not be present.

旧项目或有特殊要求的项目可能不想引入对 STL 库的依赖.依赖于数组、char* 或任何东西的接口将与任何东西兼容,因为它是语言的一部分.但是,不能保证所有构建环境中都存在 STL.

Old projects or projects with special requirements might not want to introduce dependencies on STL libraries. An interface depending on arrays, char* or whatever will be compatible with anything since it's part of the language. STL however is not guaranteed to be present in all build environments.

这篇关于你什么时候会使用数组而不是向量/字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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