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

查看:37
本文介绍了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}

似乎不起作用...

有没有办法做到这一点?

is there a way to do so?

推荐答案

初始化赋值是有区别的.你要做的不是初始化,而是赋值.但是这样的数组赋值在 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::vector 而不是原始数组.

Of course, if you choose to use std::vector instead of raw array.

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

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