How to copy an one dimensional array to another array

Hi,

I'm looking to copy one dimensional array to another array.

But, I don't want to use loops. Can anyone please explain me with example.

Thanks in advance.

 
  a[i]=b[i]; // without loops 
#include <algorithm>
#include <iterator>

std::copy( std::begin( b ), std::end( b ), std::begin( a ) );
Last edited on
Another way is to use std::array. For example

std::array<int, 10> a;
std::array<int, 10> b = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

a = b;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    double dt;
    int j=41;
    double y[45],uy[45];

    cout<<"Enter time step interval of your choice : ";
    cin>>dt;

        for(int i=1;i<=j;i++)
        {
            if (i==1)
                    {
                             y[i]=40;
                    }
                else
                    {
                             y[i]=0;
                    }
        }
       
getch();
}



Here, I want to copy y[i] to uy[i]. I tried using std::array. But it gives error as

`array' is not a member of `std' .

Please Help.
Last edited on
I do not see where uy is defined and how you are goint to copy y to uy,
I have edited the code. Now its having uy array.

I tried your two of your methods but my compiler is giving me either

`array' is not a member of `std' .

or

`begin' is not a member of `std' .
For example you can copy the array the following way.

1
2
3
4
5
6
7
8
9
10
#include <algorithm>

int main()
{
   const size_t N = 45;
   int y[N] = {};
   int  uy[N];

   std::copy( y, y + N, uy ); 
}


That to use std::array you should include header <array> and your compiler should support new features of the C++ 2011 Standard.
Thank you a Lot. It worked Finally.

Topic archived. No new replies allowed.