简体   繁体   English

在C ++中通过指针和引用传递向量

[英]Pass vectors by pointer and reference in C++

A quick question about how to safely pass and use vectors in c++. 关于如何在c ++中安全地传递和使用向量的快速问题。

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

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;

To compile requires 编译要求

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

But

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

is all a different beast. 是完全不同的野兽。

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". 同样,采用引用和采用值的表达式具有相同的语法,但是采用指针的表达式需要引用指针以提供相同的语义,但这导致表达式语法不同( *a + *b反对a+b ),从而导致“较少的通用代码”。

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. 在相反的情况下,如果您正在编写一个具有运行时多态性(并考虑了lyskov替换)的类,则很可能会处理动态分配的对象,因此,通过指针操作它们可能更自然。

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. 有“灰色区域”,两物相互啮合,但是-通常,指针获取函数在基于运行时的OOP框架中更为频繁,而引用获取函数在“基于值的通用算法”中更为频繁,在这种情况下,静态类型推断是可以预期的,并且很可能需要基于堆栈的分配。

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

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