mercredi 28 septembre 2016

Sort a Substring in Descending order using Vector, however getting segmentation fault

I want to sort the string from N to M where N indicates the starting index and M indicates the ending index.

However, my code is getting failed with segmentation fault. Here is my code:

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

using namespace std;

int main()
{   
    string s;
    int M,N;

    cout<<"Enter String"<<endl;
    getline(cin,s);

    vector<char> data(s.begin(), s.end());

    cout<<"Enter start_index and end_index for sorting";
    cin>>N>>M;    //Passed externally as N=start_index, M=end_index

    std::sort(data.begin()+N, data.begin()+M, std::greater<char>());

    for (std::vector<char>::const_iterator i = data.begin(); i != data.end(); ++i)
        std::cout << *i << ' ';

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire