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));
}
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.
How about doing it recursively:
template<typename Itr>
void inc(Itr itr, Itr end)
{
if (itr == end) return;
++(*itr);
inc(++itr,end);
}
int main()
{
int a[4]={4,4,4,4};
inc(a,a + 4);
return 0;
}
This is tail-recusive, so a decent compiler should convert this into a loop.
SSE2 solution (using intrinsics):
__m128i v1 = _mm_set_epi32(4, 4, 4, 4);
__m128i v2 = _mm_set_epi32(1, 1, 1, 1);
__m128i v3 = _mm_add_epi32(v1, v2);
#include <iostream>
using namespace std;
int main()
{
int a[4]={4,4,4,4};
a[0]++,a[1]++,a[2]++,a[3]++;
return 0;
}