简体   繁体   中英

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] is an int * , not a std::vector<int> * . You can't call a function expecting a std::vector<int> * with a 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> *

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. 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.

Pay attention to that in the first program provided in your question you are passing the vector vect by value.

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 . You can pass a pointer to the first element of an object of the type std::string as you are doing with arrays. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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