C++ 字符数组向量 [英] C++ vector of char array

查看:40
本文介绍了C++ 字符数组向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个具有字符数组向量的程序,但遇到了一些问题.

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<char[]> v;

v.push_back(test);

对不起,这必须是一个字符数组,因为我需要能够生成字符列表,因为我正在尝试获得类似的输出.

Sorry this has to be a char array because I need to be able to generate lists of chars as I am trying to get an output something like.

一个乙一个 c广告一个乙bc

a a a b a c a d a e b a b c

谁能指出我正确的方向?

Can anyone point me in the right direction?

谢谢

推荐答案

您不能将数组存储在向量中(或任何其他标准库容器中).标准库容器存储的东西必须是可复制和可赋值的,而数组两者都不是.

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 <S> v;
S s;
s.a[0] = 'x';
v.push_back( s );

这篇关于C++ 字符数组向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆