简体   繁体   English

如何将向量传递给函数参数(我需要指针吗?)

[英]How to pass a vector into a function parameter (do I need pointers?)

When we are passing a vector into a function why can't we do it like an array?当我们将一个向量传递给一个函数时,为什么我们不能像数组那样做呢?

For example:例如:

#include<bits/stdc++.h> 
using namespace std;


void func(vector<int> vect)
{
    vect.push_back(30);
}

int main()
{
    vector<int> vect;
    vect.push_back(10);
    vect.push_back(20);

    func(vect);

    for (int i = 0; i<vect.size(); i++)
        cout << vect[i] << " ";

    return 0;
}

In this example, it works fine.在这个例子中,它工作正常。 But if I do it like this why doesn't it work like an array?但是如果我这样做,为什么它不像数组那样工作?

#include<bits/stdc++.h> 
using namespace std;

void func(vector<int> *vect)
{
    vect.push_back(30);
}

int main()
{
    vector<int> vect;
    vect.push_back(10);
    vect.push_back(20);

    func(&vect[0]);

    for (int i = 0; i<vect.size(); i++)
        cout << vect[i] << " ";

    return 0;
}

Just like an array, why is this not possible?就像数组一样,为什么这不可能?

vect[0] names an int , not a std::vector<int> . vect[0]命名为int ,而不是std::vector<int>

&vect[0] is an int * , not a std::vector<int> * . &vect[0]是一个int * ,而不是一个std::vector<int> * You can't call a function expecting a std::vector<int> * with a int *你不能调用一个需要std::vector<int> *int *的函数

You need to learn references.你需要学习参考。

void func(vector<int> & vect) // use an existing std::vector<int>
{
    vect.push_back(30);
}

In this function call在这个函数调用中

func(&vect[0]);

the argument expression has the type int * while the function parameter has the type std::vector<int> *参数表达式的类型为int *而函数参数的类型为std::vector<int> *

void func(vector<int> *vect)

and there is no implicit conversion from one type to another.并且没有从一种类型到另一种类型的隐式转换。

In essence an object of the type std::vector is already a pointer wrapped in a class.本质上,std::vector 类型的对象已经是一个包含在类中的指针。 So passing an object of the type std::vector by reference you are in fact simultaneously passing a pointer to the allocated array internally pointed to by the vector.因此,通过引用传递 std::vector 类型的对象实际上是同时传递了一个指向向量内部指向的已分配数组的指针。

Pay attention to that in the first program provided in your question you are passing the vector vect by value.请注意在您的问题中提供的第一个程序中,您正在按值传递向量 vect。

void func(vector<int> vect)
{
    vect.push_back(30);
}

So the function does not change the original object passed to the function.所以函数不会改变传递给函数的原始对象。 If you want to change the original object then the function parameter should have a referenced type as for example如果要更改原始对象,则函数参数应具有引用类型,例如

void func(vector<int> &vect)
{
    vect.push_back(30);
}

Something similar what you mean is done for the standard class std::string in C++ 17. It is the class std::string_view .在 C++ 17 中为标准类std::string做了类似的事情。它是类std::string_view You can pass a pointer to the first element of an object of the type std::string as you are doing with arrays.您可以像处理数组一样传递指向 std::string 类型对象的第一个元素的指针。 But also you need to pass the length of the character array pointed to by the pointer.但是您还需要传递指针指向的字符数组的长度。

Here is a demonstrative program.这是一个演示程序。

#include <iostream>
#include <string>
#include <string_view>

void func( std::string_view s )
{
    std::string reversed_string( s.rbegin(), s .rend() );

    std::cout << reversed_string << '\n';
}

int main()
{
    std::string s( "Hello" );

    func( { &s[0], s.size() } );
}

Its output is它的输出是

olleH

It's quite possible, but you are passing an element of the vector, not the vector itself:很有可能,但是您传递的是向量的元素,而不是向量本身:

void func(vector<int> *vect) {
    vect->push_back(30);   // <-- pointer type expression
}

int main() {

    vector<int> vect;
    vect.push_back(10);
    vect.push_back(20);

    func(&vect); // <-- pass the vector

    for (size_t i = 0; i < vect.size(); i++) // <-- iterator should be size_t type
        cout << vect[i] << " ";

    return 0;
}

If you would like to pass a reference to an element of a vector you can do it too, beware, that element must already exist:如果您想传递对向量元素的引用,您也可以这样做,请注意,该元素必须已经存在:

void func(int *vect_elem) {  //<-- pointer to int parameter
    *vect_elem = 30; // <-- assign 30 to the vector element
}

int main() {

    vector<int> vect;
    vect.push_back(10);
    vect.push_back(20);

    func(&vect[0]); //<-- reference an existing element of the vector

    for (size_t i = 0; i<vect.size(); i++)
        cout << vect[i] << " ";

    return 0;
}

In this particular case you don't need pointers, a better option is to use references only, like it was already mentioned, since you wouldn't need any dereferencing:在这种特殊情况下,您不需要指针,更好的选择是仅使用引用,就像已经提到的那样,因为您不需要任何取消引用:

void func(vector<int> &vect) { //<-- parameter reference
    vect.push_back(30); 
}

int main() {

    vector<int> vect;
    vect.push_back(10);
    vect.push_back(20);

    func(vect); // <-- pass the vector

    for (size_t i = 0; i < vect.size(); i++)
        cout << vect[i] << " ";

    return 0;
}
void func(int &vect_elem) { //<-- parameter reference
    vect_elem = 30;
}

int main() {

    vector<int> vect;
    vect.push_back(10);
    vect.push_back(20);

    func(vect[0]); //<-- pass the element

    for (size_t i = 0; i<vect.size(); i++)
        cout << vect[i] << " ";

    return 0;
}

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

相关问题 我是否需要在向量中取消分配对象指针? - Do I need to deallocate object pointers in a vector? 如何将子类作为期望基类的函数的参数传递,然后将该对象传递到指向这些抽象类对象的指针向量中? - how to pass subclass as parameter for function expecting base class then pass that object into vector of pointers to those abstract class objects? C ++如何将指针向量(双指针)作为常量传递给函数 - C++ How to pass a vector of pointers (double pointers) as a constant to a function 如何将指针数组作为参数传递给函数? - How do I pass a pointer to an array of pointers as an argument to a function? 如何将函数作为参数传递? - How do I pass a function as parameter? 为什么我需要手动删除vector中的指针? - Why do I need to delete pointers from vector manually? 如何将向量的向量传递到期望指向指针的(原始)指针的函数中 - how to pass a vector of vectors into a function expecting a (raw) pointer to pointers 如何传递指针向量以便在函数中进行更改? - How to pass a vector of pointers so as to change them in the function? 如何将指针传递给指针向量? - How to pass a pointer to vector of pointers? 如何正确将向量的大小传递给需要int的函数? - How do I properly pass the size of a vector to a function requiring an int?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM