Question on making one array equal another?

Started by
8 comments, last by johnnyBravo 20 years, 3 months ago
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,
Advertisement
memcpy
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?
by sets u mean just replace a''s array with b''s?
well thats what im trying to do.

memcpy worked fine anyway,

thanks
No, I meant as in set theory where they would be equal because they contain the same values.
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]
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
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]}
std::copy(a, a + 4, b);

[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
for(int i=0;i < 4;i++)   a=b;<br>  </pre>  <br>Is the correct way to do it. However, I recommend using memcpy (for C) or std::copy (for C++).      <br><br><SPAN CLASS=editedby>[edited by - brassfish89 on January 17, 2004 12:42:08 PM]</SPAN>
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());



"I turned into a driveway I thought was mine and crashed into a tree I don't have."- a real insurance claim

This topic is closed to new replies.

Advertisement