#### Archived

This topic is now archived and is closed to further replies.

# Question on making one array equal another?

This topic is 5440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ive got: int a[4]={0,1,2,3}; int b[4]={3,2,1,0}; how would i make a equal b in one line? or is that possible eg something like a=b(tried this, doesn''t work) thanks,

memcpy

##### Share on other sites
The reason why it didn''t work is because you have not overloaded (defined your own implementation for) the = operator.

Could you be more specific by what you mean when they are equal? Are you treating them as sets?

##### Share on other sites
by sets u mean just replace a''s array with b''s?
well thats what im trying to do.

memcpy worked fine anyway,

thanks

##### Share on other sites
No, I meant as in set theory where they would be equal because they contain the same values.

##### Share on other sites
memcpy or a for loop

for (int i = 0; i<4; i++){a[i] = b[i]}

it may loop one extra time (for loops have always confused me a bit) but im pretty confident this doesnt

oh.... one line....

[edited by - Ademan555 on January 16, 2004 11:42:21 AM]

##### Share on other sites
Yours is one line Ademan:

for (int i = 0; i<4; i++){a[i] = b[i]}// And correct me if I''m wrong, but if you change it to this, it// shouldn''t run the extra loopfor(int i = -1; i<4; ++i){a[i] = b[i]}

##### Share on other sites
for(int i=0;i < 4;i++)   a[ i ]=b[ i ];

Is the correct way to do it. However, I recommend using memcpy (for C) or std::copy (for C++).

[edited by - brassfish89 on January 17, 2004 12:42:08 PM]

##### Share on other sites
Use boost::array class its handy indeed.

boost::array<int,4> a = {{0,1,2,3}};boost::array<int,4> b = {{2,4,6,8}};a = b;orstd::copy (a.begin(), a.end(), b.begin());

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633650
• Total Posts
3013119
×