私はベクトルを使って構造体に対してn個のオブジェクトを作成するプログラムを用意しています。私はこのベクトルを関数get_stud_dataに渡して、そこでstudentの名前を入力したいと思います。すべてのベクターオブジェクトをメイン関数に戻したい場合はベクトルオブジェクトを関数に渡して返す
#include <iostream>
#include <string>
#include <vector>
#include "stdio.h"
using namespace std;
struct students
{
string name; // I am not sure
int roll_no;
};
vector<p>& get_stud_data(vector<students> & p, int n)
{
for(int i=0;i<n;i++)
{
cout<<"Enter Name of " <<i+1<<"th student\n";
cin>> p[i].name;
}
return p;
}
int main()
{
int n;
cout<<" Enter the number of students: ";
cin>> n;
vector<students> p(n); // creating n objects for struct students
// Want to retrieve the objects here.
p=get_stud_data()
return 0;
}
。ベクトルを返す必要はありません。ちょうど参照として渡される更新されたベクトルを使用してください。 –