C++stringstreamの使い方:stringstreamで任意のタイプ間の変換を実現し、競争問題を巧みに解く.————インクホワイト

3998 ワード

Csprintfとsscanf()に傷つけられた私たちは、C++のstringstreamを勉強した後、思わず「爽やかすぎる!」と叫んだ.stringstreamが創造したストリームを通じて、私たちは簡単にいろいろなタイプの間の変換を実現することができます!くだらないことは言わないで、一緒にC++stringstreamの勉強に入りましょう.
stringstreamの使い方まとめ
注意:stringstreamを説明するとき、読者の皆さんに注意してほしいポイントがあります!!!stringstreamが宣言するストリームオブジェクトは、必ずストリーム付与式の左側にしか配置できません.例:
#include
#include
#include
using namespace std;
int main()
{
    stringstream stream;
    int k=41232;
    string str1;
    //k>>stream;//  ! stream      !
    stream<>str1;//correct
    //str1<

では、次にstringstreamの使い方を正式に説明します.まず、stringstreamがistringstreamとostringstreamの機能を一体化していることを知っています.だから、ここではstringstreamだけを話します.stringstreamを使用するには、ヘッダファイルsstreamを含まなければなりません.
#include

次にstringstreamの使い方をいくつかの例で初歩的に理解します
1.intタイプ回転stringクラス
#include
#include
#include
using namespace std;
int main()
{
    int i=23412;
    string str1("hhhhhh");
    stringstream stream;
    stream<>str1;// stream   str1
    cout<

2.文字配列もstringクラスに変換できる
#include
#include
#include
using namespace std;
int main()
{
    char a[100]="hhhhhhhhh";
    double k=3.1423;
    stringstream stream;
    stream<>a;
    cout<

3.ストリームにスペースがある場合、ストリームはどのようにして他の変数に値を付与しますか?
#include
#include
#include
using namespace std;
int main()
{
    string str1("wo hao shuai a a a a ");
    string a1,a2,a3,a4;
    stringstream stream(str1);
    stream>>a1;
    stream>>a2;
    stream>>a3;
    stream>>a4;
    cout<

このように,ストリーム中のデータはスペースで分割され,他の人に伝送されると,一度にスペースの前のすべてのデータのみを通過し,順次下に下がる.
4.同じストリームを繰り返し使用する場合は、ストリームを使用することを学ばなければならない.clear()
#include
#include
using namespace std;
int main()
{
    int first(111),second(222);
    stringstream stream;
    stream<>second;
    cout<>second;
    stream>>k;
    cout<

最も恐ろしいのはclear()を使わなければ、再びストリームに値を割り当てると、何が起こると思いますか?答えは、何も起こらないということです.あなたは再びstreamで他の変数に値を割り当てます.彼は元の値を割り当てます.
#include
#include
using namespace std;
int main()
{
    int first(111),second(222);
    stringstream stream;
    stream<>second;
    cout<>second;
    cout<

例題が来たよ
ソートTime Limit:2000/1000 MS(Java/others)Memory Limit:65536/32768 K(Java/others)Total Submission(s):6476 Accepted Submission(s):1865
Problem Descriptionは1行の数字を入力し、この行の「5」をすべてスペースと見なすと、1行がスペースで分割されたいくつかの非負の整数(一部の整数は「0」で始まり、これらの頭部の「0」は無視されるべきで、この整数がいくつかの「0」で構成されていない限り、この整数は0である)が得られる.
あなたの任務は、これらの分割で得られた整数を、小さい順にソートして出力することです.
Input入力には複数のテスト例が含まれており、各入力データは1行の数字(数字の間にスペースがない)しかなく、この数字の長さは1000以下である.
入力データ保証:分割された非負の整数は1000000を超えない.入力データがすべて「5」で構成されるはずがない.
Outputは、各試験例について、分割された整数ソートの結果を出力し、隣接する2つの整数の間を1つのスペースで分割し、各グループの出力が1行を占める.
Sample Input
0051231232050775
Sample Output
0 77 12312320
#include
#include
#include
#include
using namespace std;
int a[10010];
int main()
{
	string str;
	stringstream stream;
	int x,len;
	while(cin>>str){
		for(int i=0;i>a[x])
			x++;
		len=x;
		sort(a,a+len);
		for(int i=0;i