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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:30:32+00:00 2026-06-05T12:30:32+00:00

Below in c++ program, include<iostream> #include<vector> using namespace std; int main() { vector<int> numbers;

  • 0

Below in c++ program,

include<iostream>
#include<vector>
using namespace std;

int main()
{
     vector<int> numbers;

    numbers.push_back(2);
    numbers.push_back(10);
    numbers.push_back(5);
    numbers.push_back(3);
    numbers.push_back(7);

    numbers[3] = 8;
    numbers[5] = 11;

    for(int i=0; i<numbers.size(); ++i)
    {
            cout<<" "<<numbers[i];
    }
}    

see it on ideone.

here, numbers[3] is working but numbers[5].
It looks like, vector::operator[] doesn’t increase the size of vector like vector::push_back.
so, is this the only difference between these two or something else is there?

  • 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-05T12:30:34+00:00Added an answer on June 5, 2026 at 12:30 pm

    push_back creates a new element on the back with the value specified. operator[] requires the element to be there; it just accesses it. The reason [5] doesn’t work is because you have 5 elements, so your indices range from 0 to 4.

    Generally, when adding new elements, push_back is preferred over resize, followed by operator[]. Only one can be used for reading, though, and operator[] is also needed to maintain normal array syntax.

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

Sidebar

Related Questions

i have written a sample program.below: #include<iostream> #include<string> #include<set> using namespace std; int main()
I have the below program written in C++: #include <iostream> using namespace std; int
On Visual Studio 2010 the following program #include <iostream> using std::cout; int main() {
Consider the sample program below: #include <iostream> using namespace std; class test { public:
In the below program,i am not getting values from printf . #include<stdio.h> int main()
Could you tell mw what is the problem with the below boost::thread program #include<iostream>
The below program calculates 2 raised to the power n without using any loop,runtime
I made a file hi.cpp and I wrote the command given below: #include <iostream>
I want to eliminate the junk that I'm getting on the vector<int> synsAux below.
What am I doing wrong in the program below? I want to use std::find()

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.