English 中文(简体)
是否使用建筑商在C++中将2DVctor作为一线推器?
原标题:Is it okay to use constructors to initialize a 2D Vector as a one-liner in C++?

是否象这种2D病媒(在5x4 2D vectro中的所有数值均已初步确定为3)?

std::vector<std::vector<int> > foo(5, std::vector<int>(4, 3));

这似乎有oka,但是,在任何地方,我看着网络的人似乎都建议把这种媒介引入 lo和推回。 起初,我担心,这里的所有各行都将指同一种病媒,但情况似乎并非如此。 我失踪了吗?

最佳回答

这完全是有效的——你获得2D矢量([5,4]元素,每个元素都初始到3。

在其他大多数情况下(例如,在你想要不同要素的不同价值观的地方),你不能使用任何一行——因此需要假体。

问题回答

守则是有效的,确实是你想要做到的(假定我正确理解你的意图)。

然而,这样做一般效率不高(至少在目前语言/图书馆版本中)。 以上初始化形成一种临时病媒,然后将单个子主推为copying。 这可能相当低的效率。 由于这一原因,在许多情况下,最好自行建造顶级病媒。

std::vector<std::vector<int> > foo(5);

之后,通过做像样的事情,在本地建造自己的小子。

foo[i].resize(4, 3);




相关问题
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?

热门标签