C++ vector of char array

后端 未结 7 901
梦谈多话
梦谈多话 2020-12-09 16:06

I am trying to write a program that has a vector of char arrays and am have some problems.

char test [] = { \'a\', \'b\', \'c\', \'d\', \'e\' };

vector

        
7条回答
  •  爱一瞬间的悲伤
    2020-12-09 16:55

    You cannot store arrays in vectors (or in any other standard library container). The things that standard library containers store must be copyable and assignable, and arrays are neither of these.

    If you really need to put an array in a vector (and you probably don't - using a vector of vectors or a vector of strings is more likely what you need), then you can wrap the array in a struct:

    struct S {
      char a[10];
    };
    

    and then create a vector of structs:

    vector  v;
    S s;
    s.a[0] = 'x';
    v.push_back( s );
    

提交回复
热议问题