Adding an int to all elements of a vector

Viewed 3176

I have a vector with integers lets say {1, 2, 3, 4}.

How can I add a constant value 10 to each element to modify the vector to be {11, 12, 13, 14}.

And the same thing with divides if I wanted to divide each element by an int and modify the vector. I haven't been able to find solutions.

5 Answers

Lets say {1, 2, 3, 4}, how can I add a constant value 10 to each element to modify the vector to be {11, 12, 13, 14}. and the same thing with divides if [...]

How about using std::valarray!

std::valarray is the class for representing and manipulating arrays of values. It supports element-wise mathematical operations and various forms of generalized subscript operators, slicing and indirect access.

If you can use them, that is just one line of operation. (See a live demo online)

#include <iostream>
#include <valarray> // std::valarray

int main()
{
   std::valarray<int> valArray{ 1, 2, 3, 4 };
   valArray += 10;  // add each element with 10
   for (const int ele : valArray) std::cout << ele << " ";
   std::cout << "\n";

   valArray /= 2;   // divide each element by 2
   for (const int ele : valArray) std::cout << ele << " ";
   return 0;
}

Output:

11 12 13 14 
5 6 6 7 

There is many ways you can achieve what your aiming for, but I will post the 2 most used of them.

Solution 1 :
You have to use iterators to solve your underlying problem.
In the code just below I'm iterating through all the references of the elements of my vector and modifying their values.

Code:

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

int main()
{
  vector<int> vec{1, 2, 3, 4};

  for(int& x : vec) // if you want to add 10 to each element
    x += 10;

  for(int& x : vec) // if you want to divide each element
    x /= 2;

  for (int x : vec) // print results
    printf("%d\n", x);
}

Solution 2:
Use the std::for_each function provided by the standard library (STL) in the header algorithm. Which also boils down to using iterators but in this solution you only have to write one line.

Code:

#include <iostream>
#include <vector>
#include <algorithm> // you need to include this to use std::for_each() !

using namespace std;

int main()
{
  vector<int> vec{1, 2, 3, 4};
  std::for_each(vec.begin(), vec.end(), [](int &n){ n+=10; }); // add 10 to each element
  std::for_each(vec.begin(), vec.end(), [](int &n){ n/=3; }); // divide each element by 3
  for (int x : vec) // print results
    printf("%d\n", x);
}

Consider using transform:

  std::vector<int> v = { 1, 2, 3, 4 };
  std::transform(
          v.begin(), v.end(),             // The input source
          v.begin(),                      // The output destination
          [](int x) { return x + 10; }    // The transforming function
  );

… or, if you don't want to write the loop yourself:

std::for_each(std::begin(vec), std::end(vec), [](int& x) { x += 10; });

But the loop is clearer.

Or simply with just a for loop, like:

for (auto& item : vec)
{
    item += 10;
}

But I admit that for_each looks better...

Related