Given n strings, the n strings are arranged in dictionary order, where the permutation function is the C + + library function sort, resulting in the following two questions, look big guy answer
#include <iostream>#include<algorithm>#include<string>#include<vector>using namespacestd;/***********************************************q1: Why do you define an array of classes that cannot be sorted with the sort function? The array is out of bounds, the solution *********************************************int Main () {string str[1000]; int n; scanf ("%d", &n); for (int i = 0;i < n;i++) {scanf ("%s", str+i); Str[i] = Str[i] + ' + '; } sort (str,str+n-1); for (int i =0;i < n;i++) printf ("%s\n", str+i); return 0;} ***********************************************//*The following code works correctly*/intmain () {vector<string>str; stringNewstr; intN; scanf ("%d",&N); for(inti =0; I < n;i++) {cin>>Newstr; //scanf ("%s", NEWSTR); //Q2: Here to read the input with scanf, why is it wrong? Str.push_back (NEWSTR); } sort (Str.begin (), Str.end ()); for(inti =0; I < n;i++) cout<<str[i]<<Endl; return 0;}
Differences between scanf functions and CIN, arrays of classes, C + + sort functions