1

I'm in trouble and I cannot wrap my head around this myself..

// string::operator+= vs +
#include <iostream>
#include <string>
using namespace std;


int main ()
{
    unsigned char array[6]= { 'f','o','o','b','a','r' };
    string name ("works and not");
    cout << name<< endl;
    name ="";
    for(int i=0; i < 6;  i++)
        name += array[i];
    cout << "working: "<< name << endl;
    name ="";
    name = array[1] + array[0] + array[0] + array[3] + array[4] + array[5]; 
    cout <<"not working: "<<  name << endl;
    return 0;
}

Now I understand that there's some hidden conversion going on in += notation, and I get, that the plus symbol adds the integers of my characters, and just converts the final value (to 'p').

But I need to combine various chars into one string.. in one line if possible.. since I need to do that more than once (600++ times) and it's already messing the code up.

since this is the first, and most likely last time I need to convert my "array" values to a string, I'd rather NOT change my char array btw.

thank you!

3

1 Answer 1

2
name = array[1] + array[0] + array[0] + array[3] + array[4] + array[5]; 

The requirement to do this all in one line is your problem. The right-hand side of the = is evaluated before the fact that name is even considered. And, yes, since those are all unsigned chars, all you're getting is unsigned char addition.

If your array were of char, there'd be a shortcut:

name = std::string() + array[1] + array[0] + array[0] + array[3] + array[4] + array[5];

Now the RHS is being built up from a series of operator+ as applied to a (temporary) std::string, and the final concatenated result copy-assigned into name. It's pretty ugly but...

But you can't use this with your unsigned chars, because there is no operator+ for std::string and unsigned char. You've basically designed yourself into a corner so, if you can't use loops or use a std::string to start with in the first place, you're SOL.

If you don't mind a bit of refactoring, though, here's a 2015 solution:

#include <iostream>
#include <string>

int main()
{
    unsigned char array[6] = { 'f','o','o','b','a','r' };
    std::string name;

    for (auto i : {1,0,0,3,4,5})
        name += array[i];

    std::cout << name << '\n';
}

(live demo)

Sign up to request clarification or add additional context in comments.

3 Comments

Oh.. it's soo easy sometimes, isn't it's not actually too difficult to use signed chars for me, one more line of code.. no big deal, but that didn't came to my mind AT ALL! So thanks a million!!!!
@itsid: No problem. As a bonus, I've put a proper, modern solution into my answer.
Oh I like the 2015 solution! ... hmm.. time to step up I guess :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.