简体   繁体   English

向量 <string> 或矢量 <char *> ?

[英]vector<string> or vector<char *>?

Question: 题:

  • What is the difference between: 有什么区别

    • vector<string> and vector<char *> ? vector<string>vector<char *>
  • How would I pass a value of data type: string to a function, that specifically accepts: 我如何将数据类型的值: string传递给函数,具体接受:

    • const char * ? const char *

For instance : 例如

 vector<string> args(argv, argv + argc);

 vector<string>::iterator i;

 void foo (const char *); //*i
  • I understand using vector<char *> : I'll have to copy the data, as well as the pointer 我理解使用vector<char *> :我将不得不复制数据以及指针

Edit: 编辑:

Thanks for input! 感谢您的投入!

This really has nothing to do with vectors specifically. 这实际上与矢量没有任何关系。

A char* is a pointer, which may or may not point to valid string data. char*是一个指针,它可能指向也可能不指向有效的字符串数据。

A std::string is a string class, encapsulating all the required data that makes up a string, along with allocation and deallocation functionality. std::string是一个字符串类,封装了构成字符串的所有必需数据,以及分配和释放功能。

If you store std::string 's in a vector, or anywhere else, then everything will just work . 如果将std::string存储在向量中或其他任何位置,那么一切都会正常工作

If you store char pointers, you have to do all the hard work of allocating and freeing memory, and ensuring the pointers only ever point to meaningful string data, and determine the length of the strings and so on. 如果存储char指针,则必须完成分配和释放内存的所有艰苦工作,并确保指针只指向有意义的字符串数据,并确定字符串的长度等等。

And since char* 's are expected by a lot of C API's as well as part of the C++ standard library, the string class has the c_str() function which returns a char* . 由于许多C API以及C ++标准库的一部分都需要char* ,因此string类具有返回char*c_str()函数。

char* is actually a pointer to a value of type char , which defines what can and can't be done with that value. char*实际上是一个指向char类型值的指针,它定义了使用该值可以做什么和不可以做什么。 You could make an int* number and number references the pointer to the block of memory where this value is stored, and will assume it is an int and will lock in that type for that memory block. 你可以使一个int* numbernumber引用指向存储该值的内存块的指针,并假设它是一个int并将锁定该内存块的那种类型。 But you can store a char 'C' in that memory block but it will throw a compilation error because it says you cant perform those int functions on that char . 但是你可以在该内存块中存储一个char 'C' ,但它会抛出一个编译错误,因为它说你无法在该char上执行那些int函数。

To pass a string to something expecting const char * , use the string 's c_str() member, which returns a null-terminated string: 要将string传递给期望const char *string ,请使用stringc_str()成员,该成员返回以null结尾的字符串:

string s = "foobar";

int n = strlen( s.c_str() );
foo(i->c_str());

From http://www.cplusplus.com/reference/string/string/ : 来自http://www.cplusplus.com/reference/string/string/

String objects are a special type of container, specifically designed to operate with sequences of characters. 字符串对象是一种特殊类型的容器,专门设计用于操作字符序列。

Unlike traditional c-strings, which are mere sequences of characters in a memory array, C++ string objects belong to a class with many built-in features to operate with strings in a more intuitive way and with some additional useful features common to C++ containers. 与传统的c字符串不同,传统的c字符串仅仅是存储器数组中的字符序列,C ++字符串对象属于具有许多内置功能的类,以更直观的方式操作字符串,并具有C ++容器常用的一些其他有用功能。

char* is a pointer to a character, nothing more. char*是一个指向角色的指针,仅此而已。

You can use the c_str() to pass data that is required as const char* . 您可以使用c_str()传递const char*所需的数据。

As for copying, if you copy the data you will have a new location for the string, and therefore a new pointer. 至于复制,如果你复制数据,你将有一个新的字符串位置,因此是一个新的指针。

I would use vector< string > , only because finding would be value based and not address based. 我会使用vector< string > ,只是因为查找是基于值的而不是基于地址的。 However, vector< char* > would be faster, so each has its benefits. 但是, vector< char* >会更快,所以每个都有它的好处。

vector<char *> sounds like a bad idea!! vector<char *>听起来不错! Unless your programm is running in a memory limited system. 除非您的程序在内存有限的系统中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM