C + + Set container

Source: Internet
Author: User

Set is a mathematical set, where each element appears at most once, and a custom class can construct a set, but the less - than operator must be defined

Problem:

Enter a text to find all the different words (sequential alphabetical sequence), in dictionary order from small to large output
Words are case insensitive

Ideas:

Using StringStream to separate each word, the set element is already sorted out.

Code:

1#include <iostream>2#include <string>3#include <sstream>4#include <Set>//set mathematical set, where each element appears at most once set elements have been ordered from small to large .5 using namespacestd;6 Set<string>de;//String Collection7 intMain ()8 {9     stringS,buf;Ten      while(cin>>1) { One         //processing a string to turn a non-letter into a space letter all lowercase A          for(intI=0; I<s.length (); i++) -         { -             if(Isalpha (S[i])) s[i]=ToLower (S[i]); the             Elses[i]=' ';  -         } - StringStream SS (s); -          while(SS&GT;&GT;BUF) De.insert (BUF);//separating text with whitespace +     } -          +         //Iterating through the set set output with an iterator A         Set<string>::iterator it;//to define a forward iterator at         //The middle sequence iterates through all the elements in the collection -          for(It=de.begin (); It!=de.end (); it++)  -cout<<*it<<Endl;  -         //iterators are similar to pointers -      -      in}

Note the point:

The <sstream> library defines three kinds: Istringstream, Ostringstream, and StringStream, respectively, for the input, output, and input and output operations of the stream.

stringstream>>result;//writing values to result

stringstream<<t;//the value in the stream

Simple to use:

1     string test="123 345 Yuna hhh qwe"; 2     stringstream SS (test); 3     string s; 4     " To read a string by space: " << Endl; 5      while
View Code

C + + Set container

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.