简体   繁体   English

字符数组字符串混淆

[英]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'). 现在我了解到,在+=表示法中进行了一些隐藏的转换,我知道加号将字符的整数相加,并将最终值转换为“ 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. 但是我需要将各种字符组合到一个字符串中..如果可能的话,在一行中..因为我需要执行多次(600次以上),并且已经弄乱了代码。

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. 因为这是第一次,也是最有可能的最后一次,我需要将“数组”值转换为字符串,所以我宁愿不更改我的char数组。

thank you! 谢谢!

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. 在甚至考虑name之前,先对=的右侧进行评估。 And, yes, since those are all unsigned char s, all you're getting is unsigned char addition. 而且,是的,由于这些都是unsigned char ,因此您获得的只是unsigned char加法。

If your array were of char , there'd be a shortcut: 如果您的arraychar ,那么会有一个捷径:

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 . 现在,RHS是由应用于(临时) std::string的一系列operator+构建而成的,最后的级联结果被copy-assigned分配给name It's pretty ugly but... 这很丑,但是...

But you can't use this with your unsigned char s, because there is no operator+ for std::string and unsigned char . 但是您不能将其与unsigned char一起使用,因为std::stringunsigned char没有operator+ 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. 您基本上已经将自己设计成一个角落,因此,如果您不能使用循环或首先使用std::string开始,那么您就是SOL。

If you don't mind a bit of refactoring, though, here's a 2015 solution: 但是,如果您不介意重构,可以使用以下2015年解决方案:

#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 ) 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM