Home > Enterprise >  Increase values from vector for each call
Increase values from vector for each call

Time:01-05

I want to use a function for a vector multiple times and for each call to increment the vector's value. How could I do it ?

#include <iostream>
#include <vector>
using namespace std;

struct A
{   
    void pri(std::vector<int>& a)
    {
        std::cout<<"0: " << a[0] << std::endl;
        std::cout<<"1: " << a[1] << std::endl;
    }
    
    void priUse(std::vector<int> a)
    {
        pri(a);
        int x = 12;
        **pri(a   12);** // to work this
    }
};

int main()
{
   std::vector<int> a = {1, 2};
    A aa;
    aa.priUse(a);

    return 0;
}

CodePudding user response:

The thing to note here is that

  1. you need a loop to iterate through each element of the vector.
  2. We have to pass the vector by reference.

Solution

If you want to write a function that increase all elements of a vector by 1 that you can use the following approach:

#include <vector>
#include <iostream>

struct A 
{
    void pri(std::vector<int> &vec)
    {
        //iterate through the vector and increment each element's value 
        for(int &element: vec)
        {
              element;
        }
    }
    void priUse(std::vector<int> &vec)
    {
        pri(vec);
    }
    
};
int main()
{
    std::vector<int> a  = {1,4,6};
    std::cout<<"before incrementing: "<<std::endl;
    for(const int& element: a)
    {
        std::cout<<element<<" ";
    }
    std::cout<<std::endl;
    
    //create object 
    A aa;
    
    //call the member function 
    aa.priUse(a);
    
    std::cout<<"after incrementing: "<<std::endl;
    for(const int& element: a)
    {
        std::cout<<element<<" ";
    }
    
}

Output

The output of the program is:

before incrementing: 
1 4 6
after incrementing: 
2 5 7

CodePudding user response:

  1. Pass the vector by reference.
  2. Subscript operator returns a reference to the element of the vector that is just modifiable (if you are not working on a const vector). E.g. a[0] will increment the value of the vector's first element.

[Demo]

#include <iostream>  // cout
#include <vector>

struct A
{   
    void pri(std::vector<int>& a)
    {
        std::cout << "0: " << a[0]   << "\n";
        std::cout << "1: " << a[1]   << "\n";
    }
    
    void priUse(std::vector<int>& a)
    {
        pri(a);
        std::cout << "After first call to pri(a); ...\n";
        pri(a);
        std::cout << "After second call to pri(a); ...\n";
    }
};

int main()
{
    std::vector<int> a = {1, 2};
    A aa;
    aa.priUse(a);
}

// Outputs:
//
//   0: 1
//   1: 2
//   After first call to pri(a); ...
//   0: 2
//   1: 3
//   After second call to pri(a); ...

If you want to update the elements of the vector by adding a variable amount to them, pass that amount as a parameter, then walk the vector getting a reference to each element (e.g. with a range based for loop), auto& i : a, and update that reference:

[Demo]

    void pri(std::vector<int>& a, int val)
    {
        std::cout << "0: " << a[0] << "\n";
        std::cout << "1: " << a[1] << "\n";
        
        for (auto& i : a) { i  = val; }
    }
    
    void priUse(std::vector<int>& a)
    {
        int x{12};

        pri(a, x);
        std::cout << "After first call to pri(a); ...\n";
        pri(a, x);
        std::cout << "After second call to pri(a); ...\n";
    }

// Outputs
//
//   0: 1
//   1: 2
//   After first call to pri(a); ...
//   0: 13
//   1: 14
//   After second call to pri(a); ...
  •  Tags:  
  • Related