C ++中string和char []类型之间的区别 [英] Difference between string and char[] types in C++

查看:188
本文介绍了C ++中string和char []类型之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一点C,现在我来看看C ++。
我用于处理C字符串的字符数组,但是我看看C ++代码,我看到有使用字符串类型和字符数组的例子:

I know a little C and now I'm taking a look at C++. I'm used to char arrays for dealing with C strings, but while I look at C++ code I see there are examples using both string type and char arrays:

#include <iostream>
#include <string>
using namespace std;

int main () {
  string mystr;
  cout << "What's your name? ";
  getline (cin, mystr);
  cout << "Hello " << mystr << ".\n";
  cout << "What is your favorite team? ";
  getline (cin, mystr);
  cout << "I like " << mystr << " too!\n";
  return 0;
}

#include <iostream>
using namespace std;

int main () {
  char name[256], title[256];

  cout << "Enter your name: ";
  cin.getline (name,256);

  cout << "Enter your favourite movie: ";
  cin.getline (title,256);

  cout << name << "'s favourite movie is " << title;

  return 0;
}

(两个示例均来自 http://www.cplusplus.com

我想这是一个被广泛提出和回答(明显?)的问题,但它如果有人能告诉我在C ++中处理字符串的两种方式(性能,API集成,每种方式更好,...)之间的区别。

I suppose this is a widely asked and answered (obvious?) question, but it would be nice if someone could tell me what's exactly the difference between that two ways for dealing with strings in C++ (performance, API integration, the way each one is better, ...).

谢谢。

推荐答案

一个字符数组就是一个字符数组:

A char array is just that - an array of characters:


  • 如果在堆栈上分配(如在你的例子中),它总是占用eg。

  • 如果在堆上分配(使用malloc()或new char []),您将负责释放内存,然后您将总是有一个堆分配的开销。

  • 如果您将超过256个字符的文本复制到数组中,则可能会崩溃,生成丑陋的声明消息或在程序中的其他位置导致无法解释的(误)行为。 >
  • 要确定文本的长度,必须逐个字符扫描一个\0字符的数组。

字符串是一个包含char数组的类,但会自动为您管理它。大多数字符串实现都有一个16个字符的内置数组(因此,短字符串不会对堆进行分段),并使用堆来获取更长的字符串。

A string is a class that contains a char array, but automatically manages it for you. Most string implementations have a built-in array of 16 characters (so short strings don't fragment the heap) and use the heap for longer strings.

string's char array like this:

You can access a string's char array like this:

std::string myString = "Hello World";
const char *myStringChars = myString.c_str();

C ++字符串可以包含嵌入的\0字符,知道它们的长度不计数,为短文本分配字符数组,并保护您免受缓冲区溢出的影响。

C++ strings can contain embedded \0 characters, know their length without counting, are faster than heap-allocated char arrays for short texts and protect you from buffer overruns. Plus they're more readable and easier to use.

-

但是,C ++字符串不是)适合跨DLL边界使用,因为这将需要这样的DLL函数的任何用户,以确保他使用完全相同的编译器和C ++运行时实现,以免他的字符串类的行为不同。

However, C++ strings are not (very) suitable for usage across DLL boundaries, because this would require any user of such a DLL function to make sure he's using the exact same compiler and C++ runtime implementation, lest he risk his string class behaving differently.

通常,字符串类也会在调用堆上释放其堆内存,因此只有在使用共享(.dll或.so)版本的运行时。

Normally, a string class would also release its heap memory on the calling heap, so it will only be able to free memory again if you're using a shared (.dll or .so) version of the runtime.

简而言之:在所有内部函数和方法中使用C ++字符串。如果你写一个.dll或.so,在你的public(dll / so-exposed)函数中使用C字符串。

In short: use C++ strings in all your internal functions and methods. If you ever write a .dll or .so, use C strings in your public (dll/so-exposed) functions.

这篇关于C ++中string和char []类型之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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