English 中文(简体)
如何确定最低代码C++阵列的具体内容
原标题:How to set specific elements of an array with minimal code C++
  • 时间:2012-04-25 04:24:19
  •  标签:
  • c++
  • arrays

我很想知道,在一个单一的法典体中确定一个阵列的一个以上要素是否容易。 例如,而不是:

int Array[10];
Array[4] = 100;
Array[7] = 100;

是否有办法做如下一些事情?

int Array[10];
Array[4 & 7] = 100;

我知道上述法典并不奏效,但我确实可以想到以任何其他方式表明我的问题。 how 感谢任何希望表达其意见的人:

最佳回答
int array[10];
array[4] = array[7] = 100;
array[4] = 100, array[7] = 100;
4[array] = 7[array] = 100;

EDIT:

你们可能希望利用机会,以形成某种动态的因素。

int i, array[10], array_element[3] = { 3, 5, 6 };
for (i = 0; array_element[i] && array[array_element[i]]; i++) array[array_element[i]] = 100;

另一种选择是,如果用最低限度的代码来界定一种功能,则意味着抽象。

overlord::set(array, 100, "3, 5, 6");
overlord::set(array, 100, "{ 3, 5, 6 }");
overlord::set(array, "3: 200, 5: 400, 6: 500");

无论是在C++或C中,你都找到了“DYNAMIC”语言特征。 你们不得不对现有的基本功能进行抽象的解读,以便能够获得这种具有讽刺意味的打字。

问题回答

你们可能这样做。

 int Array[10];
 Array[4] = Array[7] = 100;

如果你试图确定一系列要素,你可以用来 lo

int array[10];
for(int i=0; i<10; i++) {
    array[i] = 100;
}

你们也可以通过利用这一骗局来为某些人做事。

int nums[2] = { 4,7 }; //Positions you wish to set
for(int i=0; i<2; i++) {
    array[nums[i]] = 100;  //nums[0] = 4, array[4] 
                           //nums[1] = 7, array[7]
}

您有这一完全可读的法典:

int Array[10];
Array[4] = 100;
Array[7] = 100;

你们希望“在单一编码线中形成一个阵列中的一个以上要素”。 Okay:

int Array[10];
Array[4] = 100; Array[7] = 100;

But why would you? Is there a newline shortage that I haven t heard about?





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签