I
I
igoodmood2016-09-07 15:11:18
C++ / C#
igoodmood, 2016-09-07 15:11:18

How to combine three one-dimensional arrays into one?

How to combine three one-dimensional arrays into one?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vitaly, 2016-09-07
@vt4a2h

Create an array with enough elements and copy the three arrays to be combined into it :)
PS I advise you to use std::vector.

F
Fat Lorrie, 2016-09-07
@Free_ze

std::vector<int> dest = {1,2,3};
std::vector<int> src = {4,5,6};
std::vector<int> src2 = {6,7,8};

dest.insert( dest.end(), src.begin(), src.end() );
dest.insert( dest.end(), src2.begin(), src2.end() );

M
Mercury13, 2016-09-07
@Mercury13

I would dump them one by one into one array or vector
www.cplusplus.com/reference/algorithm/copy
and then call the function
twice www.cplusplus.com/reference/algorithm/inplace_merge
If the arrays are length 10, 15 and 20 then …
• First time - std::inplace_merge(v.begin(), v.begin() + 10, v.begin() + 10 + 15);
• Second time - std::inplace_merge(v.begin(), v.begin() + 10 + 15, v.begin() + 10 + 15 + 20);

A
AtomKrieg, 2016-09-07
@AtomKrieg

www.cplusplus.com/reference/algorithm/copy

D
Daniil Demidko, 2016-09-09
@Daniro_San

STL will help you

const std::vector<int> 
first{ 1, 2, 3 }, second{ 4, 5, 6 }, third{ 7, 8, 9 };

std::vector<int> result=first;
std::copy(second.cbegin(), second.cend(), std::back_inserter(result));
std::copy(third.cbegin(), third.cend(), std::back_inserter(result));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question