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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:55:07+00:00 2026-06-17T10:55:07+00:00

i present you my problem I’ve 2 list, named them A and B. list<vector<int>

  • 0

i present you my problem

I’ve 2 list, named them A and B.

list<vector<int> > A = {{1},{2},{3}};
list<vector<int> > B = {{4},{5},{6}}; 

What i want is to have A = {{1,4},{1,5},{1,6},{2,4},{2,5},{2,6},{3,4},{3,5},{3,6}} without use any tmp list.

I use C++11 with gcc 4.6.3 on Ubuntu 12.04

So that the minimize code:

auto A_begin = A.begin();
auto A_end = A.end();
auto B_begin = B.begin();
auto B_end = B.end();

for(auto i = A_begin; i != A_end; ++i) //loop on A
{
    for (auto j = B_begin;j != B_end; ++j) //loop on B
    {
        vector<int> tmp = (*i); // A[i]
        copy((*j).begin(),(*j).end(),back_inserter(tmp)); // append B[j] to A[i]
        A.emplace_back(tmp); //add it to A
    }
}
A.erase(A_begin,A_end); // remove {1},{2},{3}

So, i think the algo is ok, but it make a infinit loop on A.
That I think is that A_end change when i make a A.emplace_back, but i save it, so i realy don’t know wat append here.

my code to identify the problem:

auto A_begin = A.begin();
auto A_end = A.end();
auto B_begin = B.begin();
auto B_end = B.end();

int ii = A.size();

for(auto i = A_begin; i != A_end; ++i) //loop on A
{
    for (auto j = B_begin;j != B_end; ++j) //loop on B
    {
        vector<int> tmp = (*i);
        A.emplace_back(tmp);
    }
    cout<<--ii<<endl; // exit when print 0 ?
}

This print negative number, and I’ve to ^C again.

EDIT : I find a solution:

auto A_begin = A.begin();
auto A_end =  A.end();
auto B_begin = B.begin();
auto B_end = B.end();

list<vector<int>> tmp_l;

for(auto i = A_begin; i != A_end; ++i) //loop on A
{
    for (auto j = B_begin;j != B_end; ++j) //loop on B
    {
        vector<int> tmp = (*i); // A[i]
        copy((*j).begin(),(*j).end(),back_inserter(tmp)); // append B[j] to A[i]
        tmp_l.emplace_back(move(tmp)); //add it to A
    }
}
 swap(tmp_l,A);
  • 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-17T10:55:09+00:00Added an answer on June 17, 2026 at 10:55 am

    These two lines:

    vector<int> tmp = (*i); // A[i]
    copy((*j).begin(),(*j).end(),tmp.end()); // append B[j] to A[i]
    

    will be invoking undefined behaviour. By copying to tmp.end() you are simply overwriting memory after the end of A[i], not extending A[i]. You need to use a back_insert iterator, something like:

    vector<int> tmp = (*i); // A[i]
    copy((*j).begin(), (*j).end(), back_inserter(tmp)); // append B[j] to A[i]
    

    You’ll also need to include the header to get the back_inserter.

    EDIT: Also, the A_end iterator points to the position “past the end” of the list so no matter how many items you add to a they are always added in front of A_end, hence the infinite loop. I’m not sure if there’s a good way to deal with this. There’s no benefit here in not creating a temporary list, you are allocating the same memory either way, just write into a new list.

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

Sidebar

Related Questions

I have a problem in loading a nib which is present in another project
I am doing my research. I have problem to make code to present this
I having problem figuring howto preserve the present state ...Let say I have selected
I am having a problem with an autoloading tree grid. At present I have
I have a problem with the site http://www.domda.se which is only present on iphone,
I have tried to use restrict qualified pointers, and I have encountered a problem.
Problem description: In my application, I have to present the contents of data packets
I want to have a portion of my TextView invisible. In detail, I present
Ever since iOS5, I have a problem where when I present and then dismiss
I'll only try to present the main part of the problem, because the whole

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.