C++ 通过指针在函数之间传递向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5348089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:58:24  来源:igfitidea点击:

passing a vector between functions via pointers

c++pointersvector

提问by shavera

It was suggested to me to use pointers to add a vector that I wanted to pass from some existing function to another function. I am really stuck on how to get the information back outof that pointer though. I've tried a number of things I've read here and there so let me demonstrate what I'm talking about.

有人建议我使用指针添加一个我想从某个现有函数传递到另一个函数的向量。不过,我真的很困惑如何该指针中取回信息。我已经尝试了很多我在这里和那里读过的东西,所以让我来演示一下我在说什么。

primary program:

主要程序:

std::vector<float> * dvertex=NULL;

track.calculate(irrelevant stuff, dvertex)

secondary program (track, calculate)

辅助程序(跟踪、计算)

track::caclulate(irrelevant stuff, vector<float> * dvertex)
{
...
vector<float> pos;
... pos filled after some calculations
if(! (dvertex==NULL))
{
  dvertex = &pos1;
}

back to primary, unless I messed up something above, here's some things I've tried

回到主要,除非我把上面的东西搞砸了,这是我尝试过的一些东西

1

1

(*dvertex).at(0)
float z = (*dvertex).at(0)

2

2

(*dvertex)[0]

and a bunch of stuff that just plain didn't compile. I'm quite stuck as I'm not sure how to get the specific values out of that vector in the main program. I even thought it might be the if(! (dvertex==NULL)) bit, so I changed it to if(dvertex==NULL) but still no joy. Any help would be greatly appreciated.

和一堆简单的东西没有编译。我很困惑,因为我不确定如何从主程序中的向量中获取特定值。我什至认为它可能是 if(! (dvertex==NULL)) 位,所以我将其更改为 if(dvertex==NULL) 但仍然没有乐趣。任何帮助将不胜感激。

*Edit/Update*Thanks so much everyone for the help, but I fear I'm still doing it wrong.

*编辑/更新*非常感谢大家的帮助,但我担心我仍然做错了。

So following the suggestions that I just pass a reference: I did this:

所以按照我只是通过参考的建议:我这样做了:

primary

基本的

std::vector<float> dvertex;
track.calculate( foo, &dvertex);

secondary stayed the same (with !Null check)

次要保持不变(使用 !Null 检查)

primary

基本的

std::cout<<dvertex[0]<<std:endl; 

(among other attempts to actually use the data)

(以及其他实际使用数据的尝试)

Thanks a lot for any thoughts on what I'm still doing improperly. Everything compiles, the program just freezes when it gets to a point that the data from dvertex is used.

非常感谢您对我仍然不正确的事情的任何想法。一切都会编译,当程序达到使用来自 dvertex 的数据的程度时,它就会冻结。

Edit:Final fix

编辑:最终修复

in the secondary program I needed

在我需要的辅助程序中

*dvertex = pos1;

instead of

代替

dvertex = &pos1;

回答by wilhelmtell

I'm not sure why these didn't compile for you, because they're valid as long as the pointer is valid and not null:

我不确定为什么这些没有为您编译,因为只要指针有效且不为空,它们就是有效的:

void f(std::vector<int>* v)
{
    if( v != 0 ) {
        int n = (*v)[0];     // ok
        int m = (*v).at(0);  // ok
        int o = v->at(0);    // ok
    }
}

But never mind that. Use a reference if you must change the vector, and a const reference if you must not. There's rarely if ever a need to take a container by pointer.

但别介意。如果必须更改向量,请使用引用,如果不需要,请使用常量引用。很少需要通过指针获取容器。

Also, I suggest you check pointers against 0, not NULL, because sometimes NULLis defined as (void*)0as per C compilers. But some people may argue otherwise here.

另外,我建议您检查指针0,而不是NULL,因为有时根据 C 编译器NULL定义(void*)0。但有些人可能在这里争论不休。

回答by Jerry Coffin

If you're going to modify the vector, you probably just want to pass it by reference. If you do use a pointer, however, you need to define a vector in main, and then pass the address of that vector:

如果您要修改向量,您可能只想通过引用传递它。但是,如果确实使用指针,则需要在 main 中定义一个向量,然后传递该向量的地址:

void calculate(std::vector<float> *vertex_array) { 
    vertex_array->pushback(1.0f);
    vertex_array->pushback(2.0f);
}

int main() {    
    std::vector<float> vertexes;
    calculate(&vertexes);

    std::copy(vertexes.begin(), vertexes.end(), 
        std::ostream_iterator<float>(std::cout, "\n"));
    return 0;
}

回答by jonsca

See my note above, but for your scenario to work, you need std::vector<float> * dvertex=NULL;to be std::vector<float> * dvertex = new std::vector<float>();

见我注意上面,但是对于你的情况下工作,你需要std::vector<float> * dvertex=NULL;std::vector<float> * dvertex = new std::vector<float>();