How to print array Or vector inside [ ] these brackets. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to print array Or vector inside [ ] these brackets.

30th Jan 2021, 5:45 AM
Amisha Jha
Amisha Jha - avatar
4 Answers
+ 2
Amisha Jha No way to do it in the standard library. You need to implement a function yourself. Loop through the vector and print it manually. EDIT: Actually there is a way to do it from the standard library. That is, to copy the contents of the vector into the output stream. See here how https://stackoverflow.com/questions/10750057/how-do-i-print-out-the-contents-of-a-vector/11335634#11335634 Also, I forgot to mention, you can manually implement an overload for the << operator to cout, so that when you pass in a vector to cout, it prints it. See this on how to overload the stream insertion opertor for cout and vector https://docs.microsoft.com/en-us/cpp/standard-library/overloading-the-output-operator-for-your-own-classes?view=msvc-160
30th Jan 2021, 6:04 AM
XXX
XXX - avatar
+ 1
Are u talking about the formatting of Output the elements should be inside bracket . #include <iostream> #include <vector> #include<algorithm> #include <iterator> using std::cout; using std::cin; using std::endl; using std::vector; using std::copy; using std::ostream_iterator; int main() { vector<int> int_vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; cout<<" \[ "; copy(int_vec.begin(), int_vec.end(), ostream_iterator<int>(cout, " ")); cout<<" \] "; cout << endl; return EXIT_SUCCESS; }
30th Jan 2021, 7:08 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
30th Jan 2021, 6:26 AM
SERG Starkov
SERG Starkov - avatar
0
https://code.sololearn.com/cSKnPcB124DI/?ref=app
30th Jan 2021, 8:38 AM
SERG Starkov
SERG Starkov - avatar