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

  • SEARCH
  • Home
  • 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 4064414
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:51:09+00:00 2026-05-20T15:51:09+00:00

I am working on C++.. am in a need to swap two blocks of

  • 0

I am working on C++.. am in a need to swap two blocks of elements in an array..

Say, {1,2,3,4,5,6} is my input array.. block {4,5} should be moved to beginning and the output array should be like {4,5,1,2,3,6}.. all i have is the start index and end index of the block {4,5}.. for doing this i am using a temp array, copying the blocks individually to temp array and moving it back to the original array, which is tedious

but i am sure there will be better methods to do this using memcpy or memmove.. any ideas?

  • 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-05-20T15:51:10+00:00Added an answer on May 20, 2026 at 3:51 pm

    There is a standard algorithm designed specifically for this task called std::rotate():

    #include <algorithm>
    #include <cstdio>
    
    int main()
    {
        int inputArray[] = {1, 2, 3, 4, 5, 6};
        ::printf("Before: ");
        for(int i = 0; i < 6; ++i)
        {
            ::printf("%d ", inputArray[i]);
        }
        ::printf("\n");
    
        int startIndex = 3; // refers to the number 4 in inputArray
        int endIndex = 5;   // refers one-past the number 5 in inputArray
        std::rotate(inputArray, inputArray+startIndex, inputArray+endIndex);
    
        ::printf("After: ");
        for(int i = 0; i < 6; ++i)
        {
            ::printf("%d ", inputArray[i]);
        }
        ::printf("\n");
    }
    

    Expected output:

    Before: 1 2 3 4 5 6

    After: 4 5 1 2 3 6

    std::rotate() performs the rotation in-place via std::swap(), so there’s no temporary array involved.

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

Sidebar

Related Questions

I think I have this working but I need advice. I'd like to know
There is a legacy site that I'm working on, and I need to swap
I need help working with very big numbers. According to Windows calc, the exponent
I have this regex working but now need to allow numbers without the decimal
I have this JQuery code working however I need to know whether how to
I have a MVC 4 project I am currently working on and need some
I have a jQuery code, but need it working by using Mootools: if (
My app, Datastore, webapp2, and form-specific responses are all working :) but I need
I need to begin working with milliseconds in .Net 3.0. The data will be
I need help getting CommonJS working on Java 7 and Rhino 1.7R3. Rhino 1.7R3

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.