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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Way to print out the contents of a std::vector to the screen in c++.
Next
Answered
Margarita Cronin
  • 3
Margarita Cronin
Asked: June 23, 20222022-06-23T08:38:55+00:00 2022-06-23T08:38:55+00:00In: c

Way to print out the contents of a std::vector to the screen in c++.

  • 3

. Advertisement .

..3..

. Advertisement .

..4..

Hello experts, I’m new to programming, so my understanding is still poor. Please guide me a simple way to print out the contents of a std::vector to the screen in c++. I really appreciate your help. Thanks in advance.

  • 1 1 Answer
  • 84 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-23T09:15:22+00:00Added an answer on June 23, 2022 at 9:15 am

    Hi, I’m happy to be able to help you. Hope some of my methods below will be useful to print out the contents of a std::vector to the screen in c++.

    Solution 1: Utilizing Indices
    The simplest approach is to iterate through the vector’s elements using a basic for-loop and access them using the [] operator/at() function with the appropriate index.

    #include <iostream>
    #include <vector>
    
    void print(std::vector<int> const &input)
    {
    for (int i = 0; i < input.size(); i++) {
    std::cout << input.at(i) << ' ';
    }
    }
    
    int main()
    {
    std::vector<int> input = { 1, 2, 3, 4, 5 };
    print(input);
    
    return 0;
    }

    Output:

    1 2 3 4 5

     

    Solution 2: Utilizing the std::copy method
    With the aid of the output iterator std::ostream_iterator, we can use std::copy to copy the contents of the vector to the output stream std::cout.

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    void print(std::vector<int> const &input)
    {
    std::copy(input.begin(),
    input.end(),
    std::ostream_iterator<int>(std::cout, " "));
    }
    
    int main()
    {
    std::vector<int> input = { 1, 2, 3, 4, 5 };
    print(input);
    
    return 0;
    }

    Same output as above.

    Solution 3: Utilizing Range-based for-loop
    For printing elements of a container with C++11, the suggested method is to use a range-based for-loop:

    #include <iostream>
    #include <vector>
    
    void print(std::vector<int> const &input)
    {
    for (auto const &i: input) {
    std::cout << i << " ";
    }
    }
    
    int main()
    {
    std::vector<int> input = { 1, 2, 3, 4, 5 };
    print(input);
    
    return 0;
    }

    Same output as above.

    Solution 4: Utilizing std::for each function.
    It applies a function to each element in a range that is defined by two input iterators. The function may take the form of a lambda expression, an item of one class overloading the () operator, or a unary function.

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    void print(const int &i) {
    std::cout << i << ' ';
    }
    
    struct myclass
    {
    void operator() (int i) {
    std::cout << i << ' ';
    }
    } ob;
    
    void print_vector(std::vector<int> const &input)
    {
    // specify a lambda expression
    std::for_each(input.begin(),
    input.end(),
    [](const auto &e) {
    std::cout << e << " ";
    });
    
    // or pass an object of a class overloading the `()operator`
    // std::for_each(input.begin(), input.end(), ob);
    
    // or specify a function
    // std::for_each(input.begin(), input.end(), print);
    }
    
    int main()
    {
    std::vector<int> input = { 1, 2, 3, 4, 5 };
    print_vector(input);
    
    return 0;
    }

    The output is the same as above.

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

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.