CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2009
    Posts
    24

    Question Can you make arrays equal to each other

    Is there a way to make the elements of an array equal to the elements of another array?
    I know that you can do it in a loop but I'm looking for something like a shortcut.

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Can you make arrays equal to each other

    Well...depending on the type of the array. In case you are using STL containers then take a look at the following algorithms:
    • copy (for unsorted ranges)
    • merge (for sorted ranges)
    • set_union (for sorted ranges - elements in both source ranges end up only once in destination range)
    • set_intersection (for sorted ranges - only elements that are in both source ranges)


    Edit: Okay....I guess, I actually misunderstood the question...if you want to make one array equal to the other, then the 'copy' algorithm is actually the one you are looking for (which can even be used with POD types). The other three are actually used for merging. Sorry for not paying attention in the first place and thanks to Lindley for giving me the wake up.
    Last edited by Andreas Masur; January 11th, 2009 at 12:45 AM. Reason: Brain cells were already sleeping....

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Can you make arrays equal to each other

    In the event the array is of a plain-old-data type, you can always use memcpy(). std::copy() should be preferred, though, since it (probably) reduces to memcpy() in the POD case, while also correctly handling non-POD types.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Can you make arrays equal to each other

    Quote Originally Posted by E-man96 View Post
    Is there a way to make the elements of an array equal to the elements of another array?
    I know that you can do it in a loop but I'm looking for something like a shortcut.
    Code:
    #include <algorithm>
    
    SomeType array1[10];
    SomeType array2[10];
    
    //...
    std::copy(array1, array1 + 10, array2);  // set array2 equal to array1
    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Can you make arrays equal to each other

    Quote Originally Posted by E-man96 View Post
    Is there a way to make the elements of an array equal to the elements of another array?
    I know that you can do it in a loop but I'm looking for something like a shortcut.
    There's no built-in C++ language feature available (like field operations or so), so you must copy explicitly in one way or another.

    Note that if you're doing extensive copying of large amounts of data, then you could consider using a fine-grain parallel library, like Intel TBB, to make it faster on multi-core processors.
    Last edited by _uj; January 11th, 2009 at 02:25 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured