H4p2[sorting Words] Reflections

Source: Internet
Author: User

Problem 2:sorting Wordsdescription

Implement a program This reads from a user-specified input file, and stores the words to a vector , then sort the vector . A Function object should is passed to sort (), which accepting, strings as parameters. If the length of the first string is shorter than that of the second, the function object returns true . Output the sorted words to a file 3_2out.txt . All words should is small letters.

Input Format

You ' re supposed to get the path of the article from the standard input device. The path is either relative or complete. Each of the path is guaranteed existence.

The article file contains several lines. Those words in it could be captial letters or small letters. Captial words like love and love should is regard as the same word.

Output Format

You ought to accomplish the above requirements.

Sample Input

Standard Input

article.txt

article.txt

The Maximum-Subarray ProblemDescriptionSuppose that you been offered the opportunity to invest in the Volatile Chemical Corporation. Like the chemicals the company produces, the stock price of the Volatile Chemical Corporation is rather volatile. You are allowed to buy one unit of stock only one time and then sell it at a later date, buying and selling after the close of trading for the day. To compensate for this restriction, you are allowed to learn what the price of the stock will be in the future. Your goal is to maximize your profit.A brute-force solutionTry every possible pair of buy and sell dates in which the buy date precedes the sell date.
Sample Output

3_2out.txt

abeatitisofintothetrydayforandonebuyareyousellthenbeenthiswhatthatwilllikepairyourdateunitonlytimegoalwhichstockeverypricecloselaterdatesafterlearnbuyingratherfutureprofitinvestofferedtradingallowedsellingcompanyproblemsupposeproducespossibleprecedesmaximizevolatilechemicalsolutionchemicalscompensatecorporationrestrictionbrute-forcedescriptionopportunitymaximum-subarray

MyCode:

2016.04.01.0:24

1 classCompare2 {3  Public:4     BOOL operator() (Const string&AMP;S1,Const string&S2)5     {6         if(s1.length () = =s2.length ())7             returnS1 <S2;8         Else9             returnS1.length () < S2.length ()?true:false;Ten     } One }; A  - classcispunct - { the  Public: -     BOOL operator() (Const Charc) -     { -         returnIspunct (c) = =0?false:true; +     } -};

1#include <iostream>2#include <string>3#include <vector>4#include <algorithm>5#include <fstream>6#include <cctype>7#include"2.h"8 using namespacestd;9 Ten intMain () One { Avector<string>Vstr; -     stringfilename; -Ifstreaminch(filename); theOfstream out("3_2out.txt"); -CIN >>filename; -  -     if(!inch) +     { -Cerr <<"Error"; +         return 1; A     } at  -     stringtemp; -      while(inch>>temp) -     { -Temp.erase (Remove_if (Temp.begin (), Temp.end (), cispunct ()), Temp.end ());//Find punctuation and delete -Transform (Temp.begin (), Temp.end (), Temp.begin (),:: ToLower);//Turn each word into lowercase in Vstr.push_back (temp); -     } to  +Sort (Vstr.begin (), Vstr.end (), Compare ());//sort all words (based on word length, default < operator) -Auto iter = unique (Vstr.begin (), Vstr.end ());//remove adjacent repeating words theVstr.erase (ITER, Vstr.end ());//Remove the extra words completely *  $      for(ConstAuto &c:vstr)Panax Notoginseng          out<< C <<Endl; -  theSystem"Pause"); +     return 0; A}

A few pits and new usages that take a long time to memorize:

Used: function object, file input and output, several generic algorithms (Sort,transform, remove_if,)

    • int ispunct (int c);
    • Template <class forwarditerator> forwarditerator unique (forwarditerator first, ForwardIterator last);

Template <class ForwardIterator, class binarypredicate> ForwardIterator unique (forwarditerator first, ForwardIterator last, Binarypredicate pred);

    • Template <class Inputiterator, class Outputiterator, class unaryoperation> Outputiterator transform ( Inputiterator First1, Inputiterator last1, outputiterator result, unaryoperation op);

Template <class InputIterator1, class InputIterator2, Class Outputiterator, class binaryoperation> Outputiterator Transform (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, outputiterator result, binaryoperation BINARY_OP);

H4p2[sorting Words] Reflections

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.