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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:50:48+00:00 2026-05-28T14:50:48+00:00

I have a container of large objects that are expensive to copy. I must

  • 0

I have a container of large objects that are expensive to copy. I must sometimes iterate over the whole container normally, and sometimes in reverse. Once I determine the iteration direction, I don’t need to change mid-flight, i.e. no random access needed.

I’m hoping to do something like this pattern:

#include <iostream>
#include <vector>
using namespace std;
int main( int argc, char** )
{
    // pretend this is a vector of expensive objects
    vector<int> foo = {1,2,3,4,5};

    // calculate forward or backward iteration direction
    bool backwards = (argc > 1);

    if( backwards )
        // prepare backward iteration, but don't copy objects
    else
        // prepare forward iteration, but don't copy objects

    for( auto& i : /* either forward or backward */ )
    {
        // my loop body
        cout << i;
    }

    return 0;
}

This is a C++11 program, but I don’t think that really helps me here. I’m just not seeing the best way to do this. Thanks for any help.

  • 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-28T14:50:49+00:00Added an answer on May 28, 2026 at 2:50 pm

    The C++ standard containers come with these things called “reverse iterators”. Use std::vector::rbegin() and std::vector::rend() to get an iterator that iterates backwards through the vector. C++03 can do this easily:

    #include <iostream> 
    #include <vector>  
    
    // Use const reference to pass expensive-to-copy types
    void loop_body(const int& i)
    {
        std::cout << i;
    }
    
    int main( int argc, char** ) 
    { 
        // pretend this is a vector of expensive objects 
        std::vector<int> foo = {1,2,3,4,5}; 
    
        // calculate forward or backward iteration direction 
        bool backwards = (argc > 1); 
    
        if( backwards ) { 
            std::for_each(foo.rbegin(), foo.rend(), &loop_body);
        } else { 
            std::for_each(foo.begin(), foo.end(), &loop_body);
        } 
        return 0; 
    } 
    

    You may be able to do this, using lambdas in C++11:

    #include <iostream> 
    #include <vector> 
    
    int main( int argc, char** ) 
    { 
        // pretend this is a vector of expensive objects 
        std::vector<int> foo = {1,2,3,4,5}; 
    
        // calculate forward or backward iteration direction 
        bool backwards = (argc > 1); 
    
        // Use const reference to pass expensive-to-copy types
        auto loop_body = [](const int& i)
        {
            std::cout << i;
        };
    
        if( backwards ) { 
            std::for_each(foo.rbegin(), foo.rend(), loop_body);
        } else { 
            std::for_each(foo.begin(), foo.end(), loop_body);
        } 
        return 0; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have three divs in a container that I want to float over a large
I have large (~75MB) pickled objects that are made available on mapped network drives
I have a data field that contains large numbers in two formats: 553000.468...705.46.0000000 <-
I have a form with a textarea that can contain large amounts of content
I have a PHP file that contains a large array (about 90KB). I'm considering
I have a large JSON file that contains A LOT of similar code. It's
I have a large-ish project that contains one class that reads from a file
I have a large pool of objects with starting number and ending number. For
I am looking for a design pattern that handles large data sets over the
I have a very large point cloud (> 100000 points) that I'd like to

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.