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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:13:02+00:00 2026-06-13T00:13:02+00:00

I am trying to write a data structure that I can cycle round, sort

  • 0

I am trying to write a data structure that I can cycle round, sort of a circular list, using a vector. I resize which I am thinking should initialise the underlying array with ten elements. I don’t understand why I cannot advance the iterator. Can someone please help.

I cannot use push_back() because that will always append to the end which is not what I want.

// re-use start of vector when get to end
#include <vector>
#include <iostream>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;

class printme {
public:
   void operator() (int val) {cout << val << endl; }
};


//get a debug assertion - message says: vector iterators incompatible
//I assume this means that it is invalid after previous it++
int main(int argc, char* argv[])
{
   vector<int> myvec;
   myvec.resize(10);  //underlying array now has size=10 elements
   vector<int>::iterator it = myvec.begin();  //point to start of array
   for(int i = 0; i < 100; ++i) {
      if(it == myvec.end())           //on 2nd iteration crashes here - invalid iterator
         it = myvec.begin();

      myvec.insert(it++, i);
   }

   //print contents of vector - check 90-99 printed
   for_each(myvec.begin(), myvec.end(), printme());

    return 0;
}

EDIT
Changed loop to this:

for(int i = 0; i < 100; ++i) {
   if(it == myvec.end())
      it = myvec.begin();

   *it++ = i;
}

I didn’t properly understand insert.

  • 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-13T00:13:02+00:00Added an answer on June 13, 2026 at 12:13 am

    From what you expect in output – I believe you misunderstood what insert is doing.
    Implement your loop in this way (without insering – just replacing). std::vector<>::insert increments the size of your vector by one – I believe it is not what you expect.

    Do not do this:

    myvec.insert(it++, i);
    

    But this:

     *it++ = i;
    

    Then you’ll get your desired ouput:

       //print contents of vector - check 90-99 printed
       for_each(myvec.begin(), myvec.end(), printme());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my data structure class, I am trying to write a program that simulates
I am trying to write a program that would use a data structure/class that
I am trying to write data stored in a binary file using turbo C++.
I am trying write a function that generates simulated data but if the simulated
I am trying to write an application that adds data to an exsiting spreadsheet
I am trying to write a simple program in C# that will read data
I am trying to write a python script that takes record data like this
I am trying to write a macro that defines a special class of data
I'm trying to call IOCTL_BTH_GET_LOCAL_INFO using DeviceIoControl, which I believe it can be done
I'm trying to write classes which handle different number types. I know that C#

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.