pass <vector> to function
I'm making tests about <vector> and I want to pass it to function to insert some values, e.g.
Code:
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
struct volo {
string codice;
int num;
double costo;
volo(string cd, int n, double cs): codice(cd), num(n), costo(cs) {};
};
void insert(volo* pointer, string cd_, int n_, double cs_) {
volo app(cd_, n_, cs_);
pointer.push_back(app);
}
int main() {
vector <volo> flight;
string cod;
int n;
double c;
cin >> cod >> n >> c;
insert(flight, cod, n ,c );
}
How can I pass <vector> to function to insert values into it?
I thought <vector> was only dynamic array created by template class, so it was necessary to pass only vector name
Re: pass <vector> to function
Quote:
Originally Posted by
zio_mangrovia
How can I pass <vector> to function to insert values into it?
I thought <vector> was only dynamic array created by template class, so it was necessary to pass only vector name
Code:
void someFunction(vector& <volo> flightToFillIn)
{
flightToFillIn.push_back(volo(....));
...
}
...
vector& <volo> flight; // empty vector
someFunction(flight); // now it contains something
Re: pass <vector> to function
Quote:
Originally Posted by
VictorN
[code]
vector& <volo> flight;
I don t understand what means here & symbol
Re: pass <vector> to function
Quote:
Originally Posted by
zio_mangrovia
I don t understand what means here & symbol
It was just mistakenly copied/pasted from function definition! I am awfully sorry! :cry:
It should be
Code:
void someFunction(vector& <volo> flightToFillIn)
{
flightToFillIn.push_back(volo(....));
...
}
...
vector <volo> flight; // empty vector
someFunction(flight); // now it contains something
Re: pass <vector> to function
I noted It's possible to manage <vector> even in this way:
Code:
void printcode(volo* pp, int nn) {
for (int i=0; i<nn; ++i) {
cout << pp[i].codice << endl;
cout << pp[i].num << endl;
cout << pp[i].costo << endl;
}
}
in main() {
vector <volo> flight;
string cod;
int N;
double c;
cin >> N;
for (int i=0; i<N; ++i) {
cin >> cod >> n >> c;
volo *app = new volo(cod, n, c);
flight.push_back(*app);
delete app;
}
printcode(&flight[0], N);
...
}
Where N is number of elements.
Compiler works fine, my doubt is for &flight[0], It's the address of first element of <vector> as for array... but why this function call printcode(flight, N); gives error? flight is <vector> object while pp is is volo pointer !?
Re: pass <vector> to function
This is unnecessarily convoluted:
Code:
volo *app = new volo(cod, n, c);
flight.push_back(*app);
delete app;
It would suffice to write:
Code:
flight.emplace_back(cod, n, c);
Even if you had to compile with respect to pre-C++11, it would still be simpler to write:
Code:
flight.push_back(volo(cod, n, c));
As for your question: yes, you get the error because -- unlike an array -- you cannot implicitly convert a vector into a pointer to its first element, hence the use of &flight[0].
Re: pass <vector> to function
printcode() takes a pointer to volo. flight is a vector of volo. The two types are not compatible. flight[0] gives the first element of flight and hence &flight[0] gives the address of the first element of flight - a pointer to volo.
Note that it would be more usual to code this as flight.data() rather than &flight[0] - both give the same.
printcode() really should be changed to
Code:
printcode(const vector<volo>& pp)
{
for (const auto& v : pp) {
cout << v.codice << endl;
cout << v.num << endl;
cout << v.costo << endl;
}
}
However, a more c++ way of doing this would be:
Code:
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
struct volo {
string codice;
int num = 0;
double costo = 0.0;
volo() = default;
volo(string cd, int n, double cs) : codice(cd), num(n), costo(cs) {};
};
ostream& operator<<(ostream& os, const volo& vo)
{
return os << vo.codice << ' ' << vo.num << ' ' << vo.costo;
}
ostream& operator<<(ostream& os, const vector<volo>& vo)
{
for (const auto& v : vo)
os << v << endl;
return os;
}
istream& operator>>(istream& is, volo& vo)
{
return is >> vo.codice >> vo.num >> vo.costo;
}
int main() {
vector <volo> flight;
int N;
cin >> N;
flight.resize(N);
for (auto& f : flight) {
volo vo;
cin >> vo;
f = vo;
}
cout << flight;
// ...
}
PS Note that this code (and the original) does not check/deal with stream errors. Also note that for codice you can't input a string using >> that contains a 'white-space' char (eg a space). Try entering the text "white space" (without the ") and see what the output is!.
Re: pass <vector> to function
Quote:
Originally Posted by
zio_mangrovia
my doubt is for &flight[0]
Mine too :)
I think you should avoid &flight[0] and flight.data().
There is no reason to use it just because you can. Save it for situations where you have no choice. Normally, if you have decided to go for an std::vector there is no reason to treat it as a mere C array unless you must.