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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:45:06+00:00 2026-05-18T04:45:06+00:00

iterator insert ( iterator position, const T& x ); Is the function declaration of

  • 0
iterator insert ( iterator position, const T& x );

Is the function declaration of the insert operator of the std::Vector class.

This function’s return type is an iterator pointing to the inserted element. My question is, given this return type, what is the most efficient way (this is part of a larger program I am running where speed is of the essence, so I am looking for the most computationally efficient way) of inserting at the beginning. Is it the following?

//Code 1
vector<int> intvector;
vector<int>::iterator it;
it = myvector.begin();
for(int i = 1; i <= 100000; i++){
    it = intvector.insert(it,i);
}

Or,

//Code 2
vector<int> intvector;
for(int i = 1; i <= 100000; i++){
    intvector.insert(intvector.begin(),i);
}

Essentially, in Code 2, is the parameter,

intvector.begin() 

“Costly” to evaluate computationally as compared to using the returned iterator in Code 1 or should both be equally cheap/costly?

  • 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-18T04:45:07+00:00Added an answer on May 18, 2026 at 4:45 am

    The efficiency of obtaining the insertion point won’t matter in the least – it will be dwarfed by the inefficiency of constantly shuffling the existing data up every time you do an insertion.

    Use std::deque for this, that’s what it was designed for.

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

Sidebar

Related Questions

void insert_string( std::vector<std::string> & strings, const std::string &s ) { std::vector<std::string>::iterator it=lower_bound(strings.begin(),strings.end(),s); if(strings.size()>0) std::cout<<*it<<
Efficiency of the map::insert(iterator position, const value& k) can be dramatically improved by providing
bool operator()(Iterator it1, Iterator it2) const { return (*it1 < *it2); } Can someone
I'd like to use std::copy to insert elements into a queue like this: vector<int>
The syntax of STL list::insert is as below - iterator insert(iterator __position, const value_type&
While I use iterator like this , //include header files using namespace std; int
I want to insert an int value in a vector object at position -1,
I just found std::map.insert could accept an iterator as its first parameter as a
I know I can do this: std::vector<double> vec; std::back_insert_iterator<std::vector<double> > it( back_inserter(vec) ); it
Is there a std iterator I could use to insert elements into std::map using

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.