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 212807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:13:14+00:00 2026-05-11T18:13:14+00:00

The problem is pretty basic. (I am puzzled why the search didn’t find anything)

  • 0

The problem is pretty basic.
(I am puzzled why the search didn’t find anything)

I have a rectangular “picture” that stores it’s pixel color line
after line in a std::vector

I want to copy a rectangular region out of that picture.

How would I elegantly code this in c++?

My first try:

 template <class  T>    std::vector<T> copyRectFromVector(const std::vector<T>& vec, std::size_t startx,  std::size_t starty, std::size_t endx, std::size_t endy, std::size_t fieldWidth, std::size_t fieldHeight)
    {
     using namespace std;
    vector<T> ret((endx-startx)*(endy-starty)+10);  // 10: chickenfactor

    // checks if the given parameters make sense:
    if (vec.size() < fieldWidth*endy)
    {
        cerr << "Error: CopyRectFromVector: vector to small to contain rectangular region!" << std::endl;
        return ret;
    }

    // do the copying line by line:
    vector<T>::const_iterator vecIt = vec.begin();
    vector<T>::forward_iterator retIt = ret.end();

    vecIt += startx + (starty*fieldWidth);
     for(int i=starty; i < endy; ++i)
    {
            std::copy(vecIt, vecIt + endx - startx, retIt);
        }
        return ret;
}

does not even compile…..

Addit: Clarification:
I know how to do this “by hand”. It is not a problem as such. But I would love some c++ stl iterator magic that does the same, but faster and… more c++ stylish.

Addition: I give the algorithm the pictureDataVector, the width and height of the picture and a rectangle denoting the region that I want to copy out of the picture.
The return value should be a new vector with the contents of the rectangle.

Think of it as opening your favorite image editor, and copy a rectangular region out of that.
The Picture is stored as a long 1D array(vector) of pixelcolors.

  • 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-11T18:13:14+00:00Added an answer on May 11, 2026 at 6:13 pm

    Your question asks for a C++ way of copying a rectangular field of elements in some container. You have a fairly close example of doing so and will get more in the answers. Let’s generalize, though:

    You want an iterator that travels a rectangular range of elements over some range of elements. So, how about write a sort of adapter that sits on any container and provides this special iterator.

    Gonna go broad strokes with the code here:

    vector<pixels> my_picture;
    point selTopLeft(10,10), selBotRight(40, 50);
    int picWidth(640), picHeight(480);
    rectangular_selection<vector<pixels> > selection1(my_picture.begin(),
      my_picture.end(), picWidth, picHeight, selTopLeft, selBotRight);
    
    // Now you can use stl algorithms on your rectangular range
    vector<pixels> rect_copy = std::copy(selection1.begin(), selection1.end());
    // or maybe you don't want to copy, you want 
    // to modify the selection in place
    std::for_each (selection1.begin(), selection1.end(), invert_color);
    

    I’m sure this is totally do-able, but I’m not comfortable coding stl-style template stuff off-the-cuff. If I have some time and you’re interested, I may re-edit a rough-draft later, since this is an interesting concept.

    See this SO question’s answer for inspiration.

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

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First remove the colon after Class-Path: to match <attribute name="Class-Path"… May 11, 2026 at 8:22 pm
  • Editorial Team
    Editorial Team added an answer How about building a Queue with the elements you want… May 11, 2026 at 8:22 pm
  • Editorial Team
    Editorial Team added an answer None. Level 1 includes Level 2. Maybe I misunderstood you,… May 11, 2026 at 8:22 pm

Related Questions

I am trying to help a small business that has an application that could
I am testing a site build with a slow connection and I noticed the
I have been considering taking the plunge and installing Linux on my Playstation 3.
I am writing a website in my spare time for an educational facility and
I have a pretty standard table set-up in a current application using the .NET

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.