简体   繁体   English

c ++在值参数中返回数据?

[英]c++ return data in a value parameter?

I'm trying to understand the difference between 我试图了解它们之间的区别

void getval(const int record, int32* val, bool allowExc) const;

and

void getval(const int record, vector<int32>* value, bool allowExc) const;

I need to return a list of int32s. 我需要返回一个int32s列表。 Can the 1st example return a list of ints32s or only one? 第一个例子可以返回一个ints32s列表还是只返回一个? Any help is appreciated 任何帮助表示赞赏

Both functions take 3 arguments, return no value, and do not modify the object which they are associated with. 这两个函数都有3个参数,不返回任何值,也不修改与之关联的对象。 The difference is in the second parameter. 区别在于第二个参数。

The first function takes a pointer to an int32 (which is presumably, but not necessarily, a 32-bit signed integer). 第一个函数采用指向int32的指针(可能是,但不一定是32位有符号整数)。 This might be a pointer to the first element of an array of int32 values, or a pointer to an individual value. 这可能是指向int32值数组的第一个元素的指针,也可能是指向单个值的指针。 The value(s) can be modified by the function. 可以通过函数修改值。

The second function takes a pointer to a vector of int32 . 第二个函数接受一个指向int32向量的指针。 This is an array, and because it is a non-const parameter, the vector can be modified (with care) by the function. 这是一个数组,因为它是一个非const参数,所以可以通过函数修改向量(小心)。 One of the many advantages of a vector over a plain pointer is that you can find out how many elements are in the vector, but there is no way for the function to tell how many elements are in the array associated with the pointer unless one of the other two parameters indicates the size. 向量相对于普通指针的众多优点之一是你可以找出向量中有多少元素,但是函数无法告诉与指针关联的数组中有多少元素,除非其中一个另外两个参数表示大小。


Update to question: 更新问题:

I need to return a list of int32s. 我需要返回一个int32s列表。 Can the 1st example return a list of ints32s or only one? 第一个例子可以返回一个ints32s列表还是只返回一个?

The first function can assign values to memory allocated by its caller, but cannot usefully allocate memory and pass that back to the caller. 第一个函数可以将值赋给其调用者分配的内存,但无法有效地分配内存并将其传递回调用者。 If the caller passes in a pointer to a list (array) of int32 values, then the function can overwrite that list; 如果调用者传入一个指向int32值列表(数组)的指针,那么该函数可以覆盖该列表; it does not (self-evidently) know how big that list is. 它(不言而喻)不知道该列表有多大。

The second function gets a pointer to a vector of int32 values. 第二个函数获取一个指向int32值向量的指针。 The function could assign a new vector to the pointer, leaving open the question of how the previous value was released (or leaked). 该函数可以为指针分配一个新的向量,留下如何释放(或泄漏)前一个值的问题。 It could also simply modify the vector, but in that case, a reference would be easier to understand. 它也可以简单地修改向量,但在这种情况下,引用将更容易理解。 The vector would take care of the memory management. 向量将负责内存管理。

The difference is obviously between int32* and vector<int32>* . 显然, int32*vector<int32>*之间存在差异。 While the two are doing basically the same thing you should know that int32* is a primitive data type ( a pointer to an array of int32s) while vector is a STL (Standard Template Library) class holding in this case an array of int32s while offering more options like: 虽然两者基本上做同样的事情你应该知道int32*是一个原始数据类型(指向int32s数组的指针)而vector是一个STL(标准模板库)类,在这种情况下保存int32s数组,同时提供更多选项如:

  • out of bounds checking ([] operator) 越界检查([]运算符)
  • size of the vector (size function ) 矢量大小(大小函数)
  • maximum capacity of the vector (capacity function) 矢量的最大容量(容量函数)
  • methods for reserving more bytes if needed (reserve function) 如果需要,保留更多字节的方法(保留功能)

You don't have any of these with the primitive data type. 您没有任何这些原始数据类型。

All about vector template class you can find on its help page . 所有关于矢量模板类,您可以在其帮助页面上找到。

The first function has the val parameter as a single pointer to a int32, while the second has the value parameter as a pointer to a vector of int32. 第一个函数将val参数作为指向int32的单个指针,而第二个函数将value参数作为指向int32向量的指针。 (a vector can store multiple variables of the same type) (向量可以存储多个相同类型的变量)

vector is an STL container class. vector是一个STL容器类。 In your case, it is the difference between an int32 pointer and a pointer to a vector of int32 s. 在您的情况下,它是int32指针和指向int32的向量的指针之间的区别。

For more information, I suggest you read up on http://www.cplusplus.com/reference/stl/vector/ . 有关详细信息,建议您阅读http://www.cplusplus.com/reference/stl/vector/

Sometimes, parameters are used to return values. 有时,参数用于返回值。 In this case, val is a pointer to an int32 , so we could write into *val and leave a value there. 在这种情况下, val是一个指向int32的指针,所以我们可以写入*val并在那里留一个值。 For value , we could do something like value->push_back(an int32 value) or (*value)[0] = an int32 value . 对于value ,我们可以执行类似value->push_back(an int32 value)(*value)[0] = an int32 value

只是想知道,为什么不使用向量作为参考参数(即vector<int32>& value )?

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

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