【发布时间】:2017-06-24 00:02:48
【问题描述】:
#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++ vector segmentation-fault std-pair