array-initialization

Zero-Initialize array member in initialization list

偶尔善良 提交于 2019-12-09 04:33:57
问题 I have a class with an array member that I would like to initialize to all zeros. class X { private: int m_array[10]; }; For a local variable, there is a straightforward way to zero-initialize (see here): int myArray[10] = {}; Also, the class member m_array clearly needs to be initialized, as default-initializing ints will just leave random garbage, as explained here. However, I can see two ways of doing this for a member array: With parentheses: public: X() : m_array() {} With braces: public

How to initialize an array of 2D-arrays?

╄→гoц情女王★ 提交于 2019-12-08 15:00:25
问题 I have an array of 2D-arrays. For example, it is like: {{{0, 0, 1}, {1, 0, 0}} {{0, 0, 3}, {2, 1, 2}, {2, 2, 1}, {3, 0, 0}} {{0, 0, 7}, {3, 2, 6}, {6, 2, 3}, {6, 3, 2}, {7, 0, 0}}} But If I write int [,][] arrays={{{0, 0, 1}, {1, 0, 0}} {{0, 0, 3}, {2, 1, 2}, {2, 2, 1}, {3, 0, 0}} {{0, 0, 7}, {3, 2, 6}, {6, 2, 3}, {6, 3, 2}, {7, 0, 0}}}; the compiler will complain "; expected". If I write int [,][] arrays={new int[,] {{0, 0, 1}, {1, 0, 0}} new int[,] {{0, 0, 3}, {2, 1, 2}, {2, 2, 1}, {3, 0, 0

Is it good practice to initialize array in C/C++?

笑着哭i 提交于 2019-12-06 21:41:08
问题 I recently encountered a case where I need to compare two files (golden and expected) for verification of test results and even though the data written to both the files were same, the files does not match. On further investigation, I found that there is a structure which contains some integers and a char array of 64 bytes, and not all the bytes of char array were getting used in most of the cases and unused fields from the array contain random data and that was causing the mismatch. This

C# - Can I use an array initializer to build one byte array out of another?

蓝咒 提交于 2019-12-05 18:30:34
I'd like to use an array initializer to build one byte array out of another byte array as well as some other bytes that form a header/trailer. Basically, I'd like to do something like this: byte[] DecorateByteArray(byte[] payload) { return new byte[] { 0, 1, 2, payload.GetBytes(), 3, 4, 5}; } GetBytes() above is fictional, unfortunately. Is there any nice/elegant way to do this? I solved this by using a BinaryWriter to write everything to a MemoryStream , and then converting this into a byte array with MemoryStream.ToArray() , but it feels kind of clunky. The closest you could get would be:

Why can't you use shorthand array initialization of fields in Java constructors?

梦想的初衷 提交于 2019-12-04 22:30:01
Take the following example: private int[] list; public Listing() { // Why can't I do this? list = {4, 5, 6, 7, 8}; // I have to do this: int[] contents = {4, 5, 6, 7, 8}; list = contents; } Why can't I use shorthand initialization? The only way I can think of getting around this is making another array and setting list to that array. When you define the array on the definition line, it assumes it know what the type will be so the new int[] is redundant. However when you use assignment it doesn't assume it know the type of the array so you have specify it. Certainly other languages don't have a

Zero-Initialize array member in initialization list

微笑、不失礼 提交于 2019-12-03 01:17:35
I have a class with an array member that I would like to initialize to all zeros. class X { private: int m_array[10]; }; For a local variable, there is a straightforward way to zero-initialize (see here ): int myArray[10] = {}; Also, the class member m_array clearly needs to be initialized, as default-initializing ints will just leave random garbage, as explained here . However, I can see two ways of doing this for a member array: With parentheses: public: X() : m_array() {} With braces: public: X() : m_array{} {} Are both correct? Is there any difference between the two in C++11? Initialising

evaluation order initialization array in c++

做~自己de王妃 提交于 2019-11-30 03:42:35
问题 I like c++11 variadic templates, so I often write some little codes with it. See this example: #include <cstdio> #include <type_traits> #include <vector> template< typename ... T > auto make_vector(T ... t ) -> std::vector< typename std::common_type<T...>::type > { std::vector< typename std::common_type<T...>::type > v; v.reserve( sizeof...(T) ); using list = int[]; (void)list{ 0, ( (void)v.push_back(std::move(t)) ,0)... }; // |/ / / / // -------- // \-- How are evaluated v.push_back()s,

F#: Why is Array.createZero so fast?

ε祈祈猫儿з 提交于 2019-11-29 15:10:35
I have this code: let timer = new System.Diagnostics.Stopwatch() timer.Start() Array.zeroCreate<int> 100000000 timer.Stop() printfn "%ims" timer.ElapsedMilliseconds timer.Reset() timer.Start() Array.create 100000000 0 timer.Stop() printfn "%ims" timer.ElapsedMilliseconds I tested it and had these results: 0ms 200ms How does Array.zeroCreate create array so fast and it's guaranteed that all it's elements have default value? In other languages I know there are no such possibilities (as far as I know). In other languages I know only about fast initialization of array which elements are not

Array concatenation in C#

流过昼夜 提交于 2019-11-28 13:49:15
How do I smartly initialize an Array with two (or more) other arrays in C#? double[] d1 = new double[5]; double[] d2 = new double[3]; double[] dTotal = new double[8]; // I need this to be {d1 then d2} Another question: How do I concatenate C# arrays efficiently? Julien Hoarau You could use CopyTo : double[] d1 = new double[5]; double[] d2 = new double[3]; double[] dTotal = new double[d1.Length + d2.Length]; d1.CopyTo(dTotal, 0); d2.CopyTo(dTotal, d1.Length); var dTotal = d1.Concat(d2).ToArray(); You could probably make it 'better' by creating dTotal first, and then just copying both inputs

How to initialize an array of objects?

感情迁移 提交于 2019-11-28 11:24:24
I just looked at this SO Post: However, the Columbia professor's notes does it the way below. See page 9. Foo foos = new Foo[12] ; Which way is correct? They seem to say different things. Particularly, in the notes version there isn't [] . This simply won't compile in Java (because you're assigning a value of an array type to a variable of a the non-array type Foo ): Foo foos = new Foo[12]; it's rejected by javac with the following error (See also: http://ideone.com/0jh9YE ): test.java:5: error: incompatible types Foo foos = new Foo[12]; To have it compile, declare foo to be of type Foo[] and