English 中文(简体)
使用:
原标题:Use std::random_shuffle with std::array

I have code as such:

typedef intptr_t ptr_t;

const int num_elements = 100;
ptr_t *pstr = (ptr_t *)malloc(sizeof(ptr_t) * num_elements);
std::array<ptr_t,num_elements> *parray = new (pstr) std::array<ptr_t,num_elements>;

我愿把第1至2号要素uff起来,因此,我想用的是:sh。

auto s = parray->begin()++;
auto e = parray->end()--;
std::random_shuffle ( s, e );

I get a complaint that there is no overloaded function for this. I m feeling really stupid at my inability to see what I m doing wrong. How do I do this right?

EDIT:由于答复和反馈,已经改为

auto s = parray->begin();
s++;
auto e = parray->end();
std::random_shuffle ( s, e );

然而,在汽车上,我发现:汽车在间接程度上不同于 in。

最佳回答

回答你的直接问题: 我认为,你的错误是利用员额加薪操作员,在加薪之前将原值回去。 自std:array以来,探测器基本上都是点击器。

auto s = parray->begin() + 1;
auto e = parray->end() - 1;

Edit: Now, as for the rest. Why on earth are you doing it that way? Have you considered std::vector<int> arr(100) to create a dynamic array of 100 elements? It has similar capabilities, without all of the direct manipulation of pointers?

第2号:在阅读你的评论后,我认识到,问题在于,你再次试图把你作为点子 array起来。 在这种情况下,我根本不做新安排。 假设你在<代码>pstr上有点,就应当这样做。

std::random_shuffle(pstr +1, pstr + num_elements - 1);

这样做是因为一个阵列中的简单点子将作为算法图书馆的随机存取器。

问题回答

即使汇编者允许,你的法典也不会做你想要的东西。 您再尝试使用从<条码>以下现值中恢复的增减/减值:<>条码> > <条码>end(> > ,因此,您仍然将原值分配到<条码>和<条码> ,然后(如果可能) 增减/减退的温室。 你显然想要改变所分配的价值。

auto s= parray->begin();
auto e= parray->end();

++s;
--e;

std::random_shuffle(s, e);

Or, since you apparently have random access iterators anyway:

 std::random_shuffle(parray->begin()+1, parray->end()-1);

我不敢肯定,你为什么重新积极分配<代码>std:array <>/code>——这似乎在很大程度上打败了使用<代码>的点:array来开始。





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

热门标签