Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Merging Arrays

Merging Arrays

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pix_programmer
    wrote on last edited by
    #1

    How to merge two arrays? Consider the following two arrays: int a[3] = {2,3,5}; int b[3] = {4,6,8}; These two arrays has to be merged and stored in a third array c. My expected result would be : c[6] = {2,3,5,4,6,8}; How to do this?

    A CPalliniC S L 5 Replies Last reply
    0
    • P pix_programmer

      How to merge two arrays? Consider the following two arrays: int a[3] = {2,3,5}; int b[3] = {4,6,8}; These two arrays has to be merged and stored in a third array c. My expected result would be : c[6] = {2,3,5,4,6,8}; How to do this?

      A Offline
      A Offline
      Andy411
      wrote on last edited by
      #2

      I know two solutions:

      // Use Array.CopyTo
      int[] a = { 2, 4, 6 };
      int[] b = { 3, 5, 7 };

      int[] x = new int[a.Length + b.Length];
      a.CopyTo(x, 0);
      b.CopyTo(x, b.Length);

      // Use a List
      List c = new List();
      c.AddRange(a);
      c.AddRange(b);

      int[] d = c.ToArray();

      I would prefer using List<> because it is more powerfull, you can easily search, sort etc.

      1 Reply Last reply
      0
      • P pix_programmer

        How to merge two arrays? Consider the following two arrays: int a[3] = {2,3,5}; int b[3] = {4,6,8}; These two arrays has to be merged and stored in a third array c. My expected result would be : c[6] = {2,3,5,4,6,8}; How to do this?

        A Offline
        A Offline
        Andy411
        wrote on last edited by
        #3

        OK, I had to delete my first answer, because it was a C# solution. Sorry. Now my brain is switsched in C-mode :) You can use memcpy. But allways be aware of the size of the target array! It has to be large enough to take all of the elements.

        int a[] = { 2, 4, 6};
        int b[] = { 3, 5, 7};

        int c[6];

        memcpy(c, a, sizeof(a));
        memcpy(c + 3, b, sizeof(b)); // + 3 because the first 3 elements are
        // allready in use

        Hope it helps.

        1 Reply Last reply
        0
        • P pix_programmer

          How to merge two arrays? Consider the following two arrays: int a[3] = {2,3,5}; int b[3] = {4,6,8}; These two arrays has to be merged and stored in a third array c. My expected result would be : c[6] = {2,3,5,4,6,8}; How to do this?

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          pix_programmer wrote:

          How to do this?

          Coding! Another option would be using std::vector, and using the insert[^] method.

          Veni, vidi, vici.

          In testa che avete, signor di Ceprano?

          L D 2 Replies Last reply
          0
          • CPalliniC CPallini

            pix_programmer wrote:

            How to do this?

            Coding! Another option would be using std::vector, and using the insert[^] method.

            Veni, vidi, vici.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            CPallini wrote:

            Coding!

            Now that's a brilliant idea; who'd a thunk it?

            One of these days I'm going to think of a really clever signature.

            CPalliniC 1 Reply Last reply
            0
            • L Lost User

              CPallini wrote:

              Coding!

              Now that's a brilliant idea; who'd a thunk it?

              One of these days I'm going to think of a really clever signature.

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              I know I'm an innovator. :-)

              Veni, vidi, vici.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • P pix_programmer

                How to merge two arrays? Consider the following two arrays: int a[3] = {2,3,5}; int b[3] = {4,6,8}; These two arrays has to be merged and stored in a third array c. My expected result would be : c[6] = {2,3,5,4,6,8}; How to do this?

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                1. determine the required size of the resulting container 2. Allocate a container of that size on the heap. 3. copy the elements to the new container. If you have trouble following those steps, you better learn basic C/C++ by reading a good book or tutorial. A good one can be found at http://www.cplusplus.com/doc/tutorial/[^]

                1 Reply Last reply
                0
                • CPalliniC CPallini

                  pix_programmer wrote:

                  How to do this?

                  Coding! Another option would be using std::vector, and using the insert[^] method.

                  Veni, vidi, vici.

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  CPallini wrote:

                  Another option would be using std::vector, and using the insert[^] method.

                  I agree.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                  1 Reply Last reply
                  0
                  • P pix_programmer

                    How to merge two arrays? Consider the following two arrays: int a[3] = {2,3,5}; int b[3] = {4,6,8}; These two arrays has to be merged and stored in a third array c. My expected result would be : c[6] = {2,3,5,4,6,8}; How to do this?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    This is a simple program. I think in my opinion programming skill will develop through thinking, practice etc. here your problem is simple. so try to implement self such a problem. i will give the logic. 1. allocate required array size for destination array. 2.copy the content from source array to destination. I also agree with Richard and pallini :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups