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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:38:55+00:00 2026-06-09T15:38:55+00:00

int main() { vector <int> multiples(1); cout << multiples[0]; } This returns 0 when

  • 0
int main() {

    vector <int> multiples(1);

    cout << multiples[0];

}

This returns 0 when I want it to be 1. This happens when I initialize the vector with one element, I can access the second element, however:

int main() {

    vector <int> multiples(1, 4);

    cout << multiples[1]; // 4

}

Moreover, when I try to access elements in the vector that do not exist, I get the value of the rightmost element (in this case, 4). But I cannot seem to get the first element however. Can anyone explain why?

  • 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-09T15:38:57+00:00Added an answer on June 9, 2026 at 3:38 pm

    This

    vector <int> multiples(1);
    

    creates a vector of int with size 1. The single element is value initialized, which for ìnt means zero initialized. So you get a vector with one entry, with value 0. And this one

    vector <int> multiples(1, 4);
    

    creates a vector of size 1, this time with value4. If you try to access multiplies[1] you are going beyond the bounds of your size-1 vector, thereby invoking undefined behaviour. It you want to initialize a vector with two elements of values 1 and 4, in C++11 you can do this:

    vector <int> multiples{1, 4};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One friend of mine showed very simple example: #include <vector> int main() { vector<int>
I have this code: int main() { vector<int> res; res.push_back(1); vector<int>::iterator it = res.begin();
Is this example code valid? #include<vector> using namespace std; int main() { vector<int> vec(10);
Consider this void f(vector<const T*>& p) { } int main() { vector<T*> nonConstVec; f(nonConstVec);
Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7,
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
#include <iostream> #include <algorithm> #include <vector> #include <boost/array.hpp> #include <boost/bind.hpp> int main() { boost::array<int,
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
Consider the following code: #include <iostream> #include <vector> #include <boost/numeric/ublas/vector.hpp> #include <boost/numeric/ublas/io.hpp> int main()
int main() { int b[2][3]={{1,2},{3,4,5}}; cout << b[0][2] << endl; } And the result

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.