C++stl set container and Stream class

Source: Internet
Author: User

First the stream stream class

Header file # include <sstream>

Its function is the conversion between different types, and the C language fscanf and fprintf role is the same, but more than the C language more convenient and better use.

Very easy to use method

#include <string> #include <sstream> #include <iostream> int main () {    std::stringstream stream;    std::string result;    int i = n;    Stream << i; input int Stream stream    >> result;//extract the previously inserted int value from stream    
The above code converts the string type to int.

!! Note: It has a buffering function during the conversion process, for example, there is a string "Dada fffff"

Entering a string by buffering once through the while loop

Example:

        StringStream SS (s);        while (SS>>TMP)//Can press a space one output        Val.insert (TMP);


The following sets the set container. is a tree. To be able to carry out a heavy sentence, to filter out repeatedly.

Look at this topic: uva1085 Andy's first diction

Test instructions is to give you a bunch of English. Let you read the words inside and not repeat them.

With a set container, very easy

#include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include < Ctype.h> #include <set>using namespace std;set<string> val;int main () {    string s,tmp;    while (Cin>>s)    {        if (s== ' break ') break            ;        for (int i=0;i<s.size (); i++)        {            if (Isalpha (S[i]))                S[i]=tolower (S[i]);            else                s[i]= ';        }        StringStream SS (s);        while (SS>>TMP)//Can press a space one output        Val.insert (TMP);    }    For (Set<string>::iterator It=val.begin (); It!=val.end (); it++)        cout<<*it<<endl;    return 0;}


C++stl set container and Stream class

Related Article

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.