附加到一个数组的开始 [英] Append to the beginning of an Array

查看:107
本文介绍了附加到一个数组的开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想追加一个字符串到现有数组的开头。

I am trying to append a string to the beginning of an existing array.

char array[50] = {5,A,0,1,0,0,1,1};
char firstElement[] = {"F"};
char secondElement[] = {"s", "S"};

据我所知,我们可以使用的memcpy或strcat的追加到一个数组。但我要附加到现有的数组的开头。你能告诉我怎么样?谢谢你。

I understand that we can use memcpy or strcat to append to an array. But I want to append to the beginning of my existing array. Can you please tell me how? Thank you.

推荐答案

不是没有复制的元素 - ?任何理由不使用C ++的std ::矢量

Not without copying the elements - any reason not use a c++ std::vector?

std::vector<char> array;
array.push_back(5); // adds to end
array.push_back(A);

// insert a new array at beginning
char myarray [] = { 1,2,3 };
array.insert (array.begin(), myarray, myarray+3);

如果您需要在前面一个std :: deque的是更有效地插入的东西,但否则等同于一个vector

If you need to insert things at the front a std::deque is more efficient but otherwise is identical to a vector

std::deque<char> array;
array.push_back(5); // adds to end
array.push_back(A);
array.push_front(4); // inserts at front

这篇关于附加到一个数组的开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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