dimanche 1 mars 2015

char array string confusion

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!


Aucun commentaire:

Enregistrer un commentaire