在C ++中通过指针和引用传递向量 [英] Pass vectors by pointer and reference in C++

查看:123
本文介绍了在C ++中通过指针和引用传递向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何在c ++中安全地传递和使用向量的快速问题.

A quick question about how to safely pass and use vectors in c++.

我知道,使用向量时,必须特别注意它们的地址及其元素,因为当您动态更改其大小时,它们可能会更改其地址(除非您使用reserve等.但是我想我不会知道我需要多少空间.

I know that when using vectors you have to be very careful with addresses to them and their elements because when you dynamically change their size they may change their address (unless you use reserve etc. but I'm imagining I will not know how much space I will need).

现在,我想将现有矢量(在其他地方创建的矢量)传递给可对其进行调整并更改其大小等的函数,但是我对安全性尚不清楚,因为我通常可以通过以下方式实现所有这些功能:指针.最重要的是,使用了对向量的引用,这对我来说只是一团糟.

Now I want to pass an existing vector (created elsewhere) to a function which adapts it and changes it size etc. but I'm a little unclear as to what is safe to do because I would normally achieve all of this with pointers. On top of this there is using references to the vector and this just muddies the water for me.

例如,采用以下两个功能并在其中添加注释

For instance take the two following functions and comments in them

void function1(std::vector<int>* vec){

    std::cout<<"the size of the vector is: "<<vec->size()<<std::endl; //presumably valid here  

    for (int i=0;i<10;i++){

        (*vec).pushback(i);  //Is this safe? Or will this fail?
        // Or: vec->pushback(i); Any difference?
    }

    std::cout<<"the size of the vector is: "<<vec->size()<<std::endl; //Is this line valid here??

}

AND

void function2(std::vector<int>& vec){

    std::cout<<"the size of the vector is: "<<vec.size()<<std::endl; //presumably valid here  

    for (int i=0;i<10;i++){

        vec.pushback(i);  //Is this safe? Or will this fail?

    }

    std::cout<<"the size of the vector is: "<<vec.size()<<std::endl; //Is this line valid here??

}

这两个功能在功能和安全性方面是否有区别?

Is there any difference between the two functions, both in terms of functionality and in terms of safety?

或者换句话说,如果我只有一个指向矢量的指针/引用并且需要调整它的大小,那么在我之后如何确定矢量在内存中的实际位置或指向矢量的真正指针是什么?对其进行操作.谢谢.

Or in other words, if I only have a pointer/reference to a vector and need to resize it how can I be sure where the vector will actually be in memory, or what the pointer to the vector really is, after I operate on it. Thanks.

推荐答案

在功能方面,在您给我们提供的非常有限的上下文中,它们本质上是相同的.

In term of functionality, in the very limited context you gave us, they are essentially the same.

在更一般的视图中,如果要编写通用代码,请考虑将操作和运算符直接绑定到引用,而不绑定到指针

In more general view, if you want to write generic code, consider that operation and operators bind directly to reference, but not to pointers

a = b + c;

编译需要

A operator+(const B&, const C&);

但是

A* operator+(const B*, const C*);

都是不同的野兽.

另外,一个采用引用和采用值的表达式具有相同的语法,但是采用指针的表达式需要引用指针以提供相同的语义,但这导致了不同的表达式语法(*a + *b相对于a+b),因此导致改为少用通用代码".

Also, an expression taking reference and taking value have the same syntax, but an expression taking pointers require pointers to be deference to provide equal semantics, but this leads to different expression syntax ( *a + *b against a+b ) thus leading to "less general code".

相反,如果您正在编写一个具有运行时多态性(并考虑了lyskov替换)的类,则很可能会处理动态分配的对象,因此,通过指针进行操作可能更为自然.

On the counterpart, if you are writing a class that have runtime polymorphism (and lyskov substitution in mind), you will most likely treat dynamically allocated object, and hence, manipulating them through pointers may be more natural.

在两部分相互啮合的灰色区域"中,但是-一般而言,指针获取函数在基于运行时的OOP框架中更为常见,而引用获取函数在基于值的泛型算法"中则更为频繁,其中静态类型可以预料到,并且很可能需要基于堆栈的分配.

There are "grey areas" where the two things mesh, but -in general- pointer taking function are more frequent in runtime based OOP frameworks, while reference taking functions are more frequent in "value based generic algorithms", where static type deduction is expected, and on-stack based allocation is most likely wanted.

这篇关于在C ++中通过指针和引用传递向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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