i want to add an integer in the all indexes of an array .without using loop .in single ststement.
int a[4]={4,4,4,4}; i want to add 1 in all indexes.. so the out put is cout<
output:_ 5 5 5 5
i relly thnks to all..
i want to add an integer in the all indexes of an array .without using loop .in single ststement.
int a[4]={4,4,4,4}; i want to add 1 in all indexes.. so the out put is cout<
output:_ 5 5 5 5
i relly thnks to all..
#include <functional>
#include <algorithm>
int main()
{
int a[] = {1, 2, 3, 4};
std::transform(a, a + sizeof(a) / sizeof(a[0]), a, std::bind1st(std::plus<int>(), 1));
}
a |> Array.map ((+) 1)).std::transform( a, a+4, a, []( int x ) { return x+1; } );Yet another possibility would be to use an std::valarray:
int aa[] = {4, 4, 4, 4};
std::valarray<int> a(aa, 4);
a += 1;
I can't say I'd really recommend this, but it does work.
#include <iostream>
using namespace std;
int main()
{
int a[4]={4,4,4,4};
a[0]++,a[1]++,a[2]++,a[3]++;
return 0;
}
, are not considered two separate operations.the , operator chains this together. Whilst this may not be the nicest looking solution, and definitely not scalable it dam well fits the questionIf your array can only have 4 elements, you might be able to use the PADDD instruction of the SSE2 instruction-set extension. I have never used this, so i cannot provide more details.
This question might give you more hints on how to do it.