Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7901841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:20:15+00:00 2026-06-03T09:20:15+00:00

I have code as such: typedef intptr_t ptr_t; const int num_elements = 100; ptr_t

  • 0

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>;

I’d like to be able to shuffle elements 1 to num_elements-2 , so I thought to use std::shuffle.

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: due to answers and feedback, it has changed to

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

However, on the ‘auto e’ I get: ‘auto’ differs in levels of indirection from ‘int *’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:20:17+00:00Added an answer on June 3, 2026 at 9:20 am

    In answer to your direct question: I believe your error is using the post increment operators, which return their original value before incrementing. Since std::array iterators are basically pointers, try

    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?

    Edit 2: After reading your comments, I realize the issue is that you’re trying to shuffle an array you were given as a pointer. In that case, I wouldn’t do the placement new at all. Assuming you have the pointer in pstr, this should work.

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

    This works because simple pointers in an array will work as random access iterators for the purposes of the algorithm library.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have code such as class CString { int GetLength(); }; bool smaller(CString
I have such situation in boost and C++: // Somewhere: typedef boost::signals2::signal<void (int KeyCode)>
I have such code with 2 structures: #include <list> using namespace std; struct Connection;
I have code like this: class MapIndex { private: typedef std::map<std::string, MapIndex*> Container; Container
Possible Duplicate: Array of zero length I have seen such type of code :-
I have something like this: #include <iostream> namespace N { typedef std::pair<int, double> MyPair;
At the moment I have the following code: typedef struct _hexagon { int *vertice[6];
For reference, the code is for the motorola 68008. Say I have code such
I was wondering, if I have some code such as: $result = $db->query($sql); //
I have such code: <h:inputText id=input value=#{bean.input}> <f:convertNumber /> <rich:ajaxValidator event=onblur /> </h:inputText> I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.