简体   繁体   English

获取指向向量C ++的指针

[英]getting pointer to a vector C++

this is my second basic question on pointers. 这是我关于指针的第二个基本问题。 I am calling a function exposed in DLL.. 我正在调用DLL中公开的函数。

A vector is being declared and populated with values inside that function being called. 正在声明一个向量,并使用该函数内部的值填充该向量。

I need to loop through the vector and access its values from the calling function. 我需要遍历向量并从调用函数访问其值。

int calling_function()
{
int* vectorSize;
string input = "someValue";
vector<customObjects> *v;// do i need a pointer to a vector here?

void function_being_called(input,v,&vectorSize);

//need to access the vector here...

}

void function_being_called(string input, void *returnValue, int* vectorSize)
{
vector<customObjects> v;
v.push_back(myObj);

*vectorSize= v.size();

*returnValue = ? // how to pass vector to the calling function through this parameter pointer variable

return;
}

It should be like this: 应该是这样的:

int calling_function()
{
  string input = "someValue";
  vector<customObjects> v;

  function_being_called(input,&v);

  // access the vector here...

}

void function_being_called(string input, vector<customObjects>* v)
{
  v->push_back(myObj);
}

You've got two options. 您有两个选择。 First, pass the vector as a reference: 首先,将向量作为参考传递:

string input = "someValue";
vector<customObjects> v;
function_being_called(input, v);

void function_being_called(string input, vector<customObjects> &v)
{
 // Whatever
}

Or, if you're using C++11 just return a vector and let the move constructor take care of it: 或者,如果您使用的是C ++ 11,则只需返回一个vector然后让move构造函数来处理它:

string input = "someValue";
vector<customObjects> v =  function_being_called(input);

vector<customObjects> function_being_called(string input)
{
  vector<customObjects> v;

 // Whatever

  return v;
}

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

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