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

    vector<int> newvector() {
        vector<int> v;
        v.push_back(5);
        return v;
    }
int main(){
cout << newvector();

}


When I run this code I got an error in cout how to run this properly to return a out as vector

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
VISHNU
  • 11
  • 2

1 Answers1

0

You must iterate through each element of the vector and std::cout it individually.

int main(){
   vector<int> myVector = newvector();
   for(int i = 0; i < myVector.size(); i++)
      cout << myVector[i] << '\n';

   /* Alternatively
   for(int element : myVector)
      cout << element << '\n';
   */

   return 0;
}
Warm Red
  • 287
  • 2
  • 8
  • Ya ok but in java we can use toString() to print the value so is any way here to do that ? – VISHNU Nov 21 '22 at 13:40
  • I am afraid there isn't such a thing here. You may need to write your own function or overload this stream insertion operator to achieve such a thing. – persona Nov 21 '22 at 13:45