Andres Felipe
Andres Felipe

Reputation: 1

Basic c++ function using vectors

I'm doing some exercises with vectors. I created a function that fill a vector with defined size but when I tried to make it n size, the vector apparently is filled with trash memory, it shows: 0x23fe20, and my code crash when I try to use the vector.

#include <iostream> 
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

using namespace std;


int cargavn(int vec[])    // fill vector
{
    int i, t;       
    cout << "vector size: ";
    cin >> t;
    for(i = 0 ; i <= t-1; i++)
    {
        cout << "v["<< i <<"]=";
        cin >> vec[i];
    }
return (1);
}


int main()
{
    int vec[10];  // the vector, size here wont matter
    cargavn(vec); // call fill vector n positions
    cout << vec;    // to test if the vector is well filled
    system("PAUSE");
}

Upvotes: 0

Views: 87

Answers (2)

legalize
legalize

Reputation: 2251

Here's how it would look using modern C++:

#include <iostream>
#include <vector>

using namespace std;

void cargavn(vector<int> &vec)
{
    int t;       
    cout << "vector size: ";
    cin >> t;
    for(int i = 0; t > 0; ++i, --t)
    {
        cout << "v["<< i <<"]=";
        int v;
        cin >> v;
        vec.push_back(v);
    }
}

ostream &operator<<(ostream &s, vector<int> const &vec)
{
    s << '{';
    bool first = true;
    for (int v : vec)
    {
        if (!first)
        {
            s << ", ";
        }
        s << v;
        first = false;
    }
    return s << '}';
}

int main()
{
    vector<int> vec;
    cargavn(vec);
    cout << vec;
    system("PAUSE");
    return 0;
}

Things to understand about this version compared to yours:

  • Unnecessary includes are ommitted.
  • Prefer C++ containers like vector over C style arrays.
  • Use dynamically sized containers when they need to hold an arbitrary number of values provided by the user instead of fixed-size containers that break when the user enters too much data. See the rule of 0, 1, infinity.
  • When all control paths of a function return the same value, then the returned value is pointless and can be omitted. Particularly if none of the callers even bothers to check the return value. I suspect you had the intention of using the return value to indicate some kind of unexpected error. This is a C-style approach to error handling. C++ uses exceptions to deal with unexpected errors.
  • Overload operator<< to extend the stream insertion operator to decide how containers and your own custom types should be formatted. The standard library doesn't provide any such insertion operator for vector because there isn't any universally agreed upon way to decide how a vector of values should be formatted. So you decide this for yourself.
  • It is good form for main to return a value, even though this is not technically required.

Upvotes: 1

pm100
pm100

Reputation: 50220

you are exprecting cout << vec to somehow pretty print the vector. It wont, it just prints its address

You need to loop over the contents and print each element

for(int i =0 ; i < 10 ; i++)
{
   cout << vec[i];
}

Upvotes: 4

Related Questions