简体   繁体   中英

Segmentation fault: Core dumped C++ vector pairs of string:

#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;
}

I was randomly testing out something, but when I tried to run the above code I got an segmentation error. What should I do to sort the vector pair problem? How do I do dynamic memory allocation, if it is the solution for the problem? or the approach I took was wrong?

Your problem was that you are doing a cin on a vector that was not initialized.

cin>>stud_details1[i].first;
cin>>stud_details1[i].second;

These two lines were causing the What is a segmentation fault?

Vectors grow on demand, they are not like array where you pre-initialize size and access the array based on a indexes. Please read more about vectors .


Solution:

string name,subject;
cin >> name;
cin >> subject;
stud_details1.push_back(std::make_pair(name,subject));

Just read the name and subject as two string variables then make a pair with both, and finally push that pair to the vector.


Full code:

#include <iostream>
#include<vector>
#include<string>
#include <algorithm>


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";
    string name,subject;
    cin >> name;
    cin >> subject;
    stud_details1.push_back(std::make_pair(name,subject));
    }
return stud_details1;
}

int main()
{

    student s;
    int n;
    cout<<"Enter the number of people enrolled:";
    cin>>n;
    s.get_details(n);
    return 0;
}

Note: You had also a logic flaw, for(int i=0;i<=n;i++) this reads two inputs if 1 is entered, I have fixed it for you in the code above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM