Differences between scanf functions and CIN, arrays of classes, C + + sort functions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.