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>>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