wvm2008
wvm2008

Reputation: 3034

How to concatenate two string *arr[] in C++

Im a bit stumped here. Im working on building a binary heap class, modeled by an array. I'm trying to take two arrays of strings, and concatenate them together (for a merge function) to create a new array, which I'll then perform sorting operations on.

Both of the arrays are initialized like so in my interface:

string *heapArray;

Both arrays are constructed as so in my implementation:

heapArray = new string[10];

And the code that I tried, which won't compile, is this:

merged->heapArray = one->heapArray + two->heapArray;

So obviously I've done something wrong here, but I haven't done any concatenation of raw C++ arrays in the past, mostly play it safe with vectors. Alas, working on this optimization assignment requires it. Any guidance would be appreciated.

I realize I haven't posted much code, but I get frustrated by people who post mass blocks of code on here. I've posted what I think is relevant, but if there is something specific I'm missing which would be helpful, let me know and I'll add it.

Upvotes: 0

Views: 3716

Answers (5)

user1065734
user1065734

Reputation:

the compilation error is due to the fact that you are adding two irrelevant pointers if you want to merge two strings then you can add them now what you should do is

vector<string> v1,v2,v3;
.
.
.
for(int i = 0; i < v1.size(); i++){
    v3.push_back(v1[i] + v[i]);
}

v3 will give the vector with concatenated strings

Upvotes: 0

Jacob
Jacob

Reputation: 34601

You can't do that ; you're adding two memory addresses (pointers) together to result in a meaningless pointer. I would recommend you replace heapArray with a vector of strings

heapArray = vector<string>(10);

and then concatenate the two vectors.

merged.heapArray = one.heapArray;
one.heapArray.insert(one.heapArray.end(),two.heapArray.begin(),two.heapArray.end() );

Also, the data is on the heap so your "heapArray" identifier is still valid. If you don't want to use vector (for some academic reason), you can merge one and two by allocating enough space in merged->heapArray and copying over each element in your arrays.

Upvotes: 1

Sam
Sam

Reputation: 2552

I think what you are looking for looks like this:

string heapArray;
heapArray = new string[10]; // create an array of strings
...
string mergedString = heapArray[0] + heapArray[1];

Upvotes: 0

Ed Swangren
Ed Swangren

Reputation: 124652

merged->heapArray = one->heapArray + two->heapArray;

You are attempting to add two pointers here, not concatenating strings. You cannot just concatenate arrays like that, you will need to allocate a new region of memory and copy them in (using memcpy with complex types is not always safe. You could always allocate an array sufficient for both and copy them in).

Also, why use an array of string (assuming std::string here) for a heap? That is very... strange.

Upvotes: 0

Dan F
Dan F

Reputation: 17732

When dealing with straight C++ arrays, the only way to add two of them together like that is to allocate a new array, and insert them one by one from the two source arrays into the new one

EDIT: Or a memcpy, but that isn't as easy with a non-basic data type

Upvotes: 2

Related Questions