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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:17:12+00:00 2026-05-31T18:17:12+00:00

The following is a C++ program using STL vector container. Just wanted to know

  • 0

The following is a C++ program using STL vector container. Just wanted to know why the display() function is not printing the vector contents to the screen. If displaying the size() line is commented out, display() function works fine.

#include <iostream>
#include <vector>

using namespace std;

void display(vector<int> &v)
{
    for(int i; i<v.size(); i++)
    {
        cout << v[i] << " ";
    }
    cout << "\n" << endl;
}

int main()
{
    vector<int> v;
    cout << "Size of Vector=" << v.size() << endl;

    //Putting values into the vector
    int x;
    cout << "Enter five integer values" << endl;
    for(int i; i<5; i++)
    {
        cin >> x;
        v.push_back(x);
    }
    //Size after adding values
    cout << "Size of Vector=" << v.size() << endl;

    //Display the contents of vector
    display(v);

    v.push_back(6);

    //Size after adding values
    cout << "Size of Vector=" << v.size() << endl;

    //Display the contents of vector
    display(v);

}

Output:

Size of Vector=0

Enter five integer values

1

2

3

4

5

Size of Vector=5


Size of Vector=6
  • 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-31T18:17:13+00:00Added an answer on May 31, 2026 at 6:17 pm

    There is an idiomatic way for printing a vector out.

    #include <algorithm>
    #include <iterator>
    
    //note the const
    void display_vector(const vector<int> &v)
    {
        std::copy(v.begin(), v.end(),
            std::ostream_iterator<int>(std::cout, " "));
    }
    

    This way is safe and doesn’t require you to keep track of the vectors size or anything like that. It is also easily recognisable to other C++ developers.

    This method works on other container types too that do not allow random access.

    std::list<int> l;
    //use l
    
    std::copy(l.begin(), l.end(),
              std::ostream_iterator<int>(std::cout, " "));
    

    This works both ways with input too consider the following:

    #include <vector>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    int main()
    {
        std::cout << "Enter int end with q" << std::endl;
        std::vector<int> v; //a deque is probably better TBH
        std::copy(std::istream_iterator<int>(std::cin),
                  std::istream_iterator<int>(),
                  std::back_inserter<int>(v));
    
        std::copy(v.begin(), v.end(),
                  std::ostream_iterator<int>(std::cout, " "));
    }
    

    This version doesn’t require any hard coding of size or manual management of the actual elements.

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

Sidebar

Related Questions

In my following program I'm currently using unordered_map just because I wanted O(1) search/insert
following program should print error but its printing success.why? #include<iostream> using namespace std; int
Whenever I compile a program using boost::signal I get following errors: /tmp/ccQFaJsy.o: In function
Consider the following program: #include <iostream> #include <iterator> #include <vector> #include <utility> using namespace
Im using a STL vector in my SDL program. and it looks like this:
I launch the following command line (process) from a Windows VC++ 6 program using
I'm having problems compiling the following program. I'm using gcc -framework Foundation inherit8.1m and
I'm trying to start Microsoft word using QProcess as following: QString program = WINWORD.EXE;
I have the following Python 2.6 program and YAML definition (using PyYAML ): import
Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void

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.