C ++多个值的数组赋值 [英] c++ array assignment of multiple values

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

问题描述

所以,当你初始化数组,你可以在一个地方为其分配多个值:

so when you initialize an array, you can assign multiple values to it in one spot:

int array [] = {1,3,34,5,6}

但如果阵列已经初始化,我想完全取代一行数组中元素的值

but what if the array is already initialized and I want to completely replace the values of the elements in that array in one line

所以

int array [] = {1,3,34,5,6}
array [] = {34,2,4,5,6}

似乎不工作...

doesn't seem to work...

是有办法做到这一点?

推荐答案

初始化分配之间的差异。你想要做的不是初始化,但分配。但是这样的分配数组是不可能在C ++中。

There is a difference between initialization and assignment. What you want to do is not initialization, but assignment. But such assignment to array is not possible in C++.

下面是你可以做什么:

#include <algorithm>

int array [] = {1,3,34,5,6};
int newarr [] = {34,2,4,5,6};
std::copy(newarr, newarr + 5, array);


不过,C ++ 0x中,你可以这样做:


However, in C++0x, you can do this:

std::vector<int> array = {1,3,34,5,6};
array = {34,2,4,5,6};

当然,如果您选择使用的std ::矢量而不是原始的数组。

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

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