#include <iostream>
#include<vector>
#include<string>
using namespace std;
class student{
public:
std::vector <pair<string,string> > stud_details;
int n;
std::vector <pair<string,string> > get_details(int n);
};
std::vector <pair<string,string> > student::get_details(int n)
{
//std::vector <pair<string,string> > stud_details1;
typedef vector <pair<string,string> > Planes;
Planes stud_details1;
pair<string,string> a;
for(int i=0;i<=n;i++)
{
cout<<"Enter the details of the student"<<endl;
cout<<"Name, subject";
cin>>stud_details1[i].first;
cin>>stud_details1[i].second;
a=make_pair(stud_details1[i].first,stud_details1[i].second);
stud_details1.push_back(a);
}
return stud_details1;
}
int main()
{
student s;
int n;
cout<<"Enter the number of people enrolled:";
cin>>n;
s.get_details(n);
return 0;
}
ランダムにテストしましたが、上記のコードを実行しようとするとセグメント化エラーが発生しました。ベクトルペアの問題をソートするにはどうすればよいですか?それが問題の解決策であれば、どのように動的メモリ割り当てを行うのですか?または私が取ったアプローチが間違っていた?セグメンテーションフォールト:コアダンプされた文字列のC++ベクトルペア
おかげ
ありがとうございました。 :)ベクトルとセグメンテーションの欠陥についてあなたが提供したリンクにも感謝します。とても役に立ちました! –