0

I'm learning C++ so my question might seem a bit stupid. I wanted to build a function that print every element in a vector. I come up with that but it seems to display the address of every element. I google it and find a nice solution but i wanted to do it this way so if anyone can explain me where i'm doing something wrong. My code :

void display_vector(std::vector<int>& to_display);


int main()
{
    std::vector<int> vector_to_sort = { 2,6,7,2,1,80,2,59,8,9 };


    display_vector(vector_to_sort);
}

void display_vector(std::vector<int> &to_display)
{
    for (int i = 0; i < to_display.size(); i++)
    {
        std::cout << to_display[i] << ', ';
    }

    std::cout << '\n';
}

The solution i found on internet :

#include <iterator>

void display_vector(const vector<int> &v)
{
    std::copy(v.begin(), v.end(),
        std::ostream_iterator<int>(std::cout, " "));
}

Output of my code :

21129661129671129621129611129680112962112965911296811296911296

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

4 Answers4

1

You use ", " instead of ', '.

You can use any of the following print mechanism in the display() function:

void display_vector(std::vector<int> &to_display)
{
    //by using Normal for loop
    for (auto i = to_display.begin(); i != to_display.end(); ++i) {
        cout << *i << " ";
    }

    cout << endl;

    //by using Range based for loop
    for (int & i : to_display) {
        cout << i << " ";
    }

    std::cout << '\n';
}
Arun Suryan
  • 1,615
  • 1
  • 9
  • 27
1

In this statement

std::cout << to_display[i] << ', ';
                              ^^^^^^

you are using a multybyte character literal that has an implementation defined value.

Substitute it for string literal ", ".

As for the function then for starters if the vector is not being changed in the function then the parameter should be a constant reference.

You can use the range-based for loop to outfput elements of the vector like for example

#include <iostream>
#include <vector>

std::ostream & display_vector( const std::vector<int> &to_display, std::ostream &os = std::cout );

int main()
{
    std::vector<int> vector_to_sort = { 2,6,7,2,1,80,2,59,8,9 };

    display_vector(vector_to_sort) << '\n';
}

std::ostream & display_vector( const std::vector<int> &to_display, std::ostream &os )
{
    for ( const auto &item : to_display )
    {
        os << item << ", ";
    }

    return os;
}

Using such a function you can for example output the vector in a text file.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Just replace below line

std::cout << to_display[i] << ', ';

with

std::cout << to_display[i] << ", ";

Also note that if you just want to display vector in function then declare parameter as const reference as shown below

void display_vector(const std::vector<int> &to_display);
Kiran Choudhary
  • 1,125
  • 7
  • 15
0

The debuggers make it easy to examine vectors but I include a simple template to print out vectors of standard types and often use it when debugging data that I wish to look at with other tools.

template<class T>
void print(const std::vector<T>& v){
for (auto x: v)
    std::cout << x << std::endl;
}
doug
  • 3,840
  • 1
  • 14
  • 18