日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

istringstream字符串流,实现类似字符串截取的功能,字符串流中的put,str()将流转换成为字符串string

發布時間:2024/9/27 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 istringstream字符串流,实现类似字符串截取的功能,字符串流中的put,str()将流转换成为字符串string 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


1. istringstream字符串流

#include <iostream>

#include <sstream>

#include <string>

?

using namespace std;

?

struct MyStruct

{

??? string str1, str2, str3;

??? double db;

??? int num;

??? char ch;

};

?

void main()

{

??? string? mystring("china? google?microsoft 12.9 123 A");

??? MyStruct struct1;

???

??? istringstream input(mystring);//創建一個字符串掃描流

??? input >> struct1.str1 >> struct1.str2 >> struct1.str3 >> struct1.db >> struct1.num >> struct1.ch;

??? cout << struct1.str1 << endl;

??? cout << struct1.str2 << endl;

??? cout << struct1.str3 << endl;

??? cout << struct1.db << endl;

??? cout << struct1.num << endl;

??? cout << struct1.ch << endl;

?

??? cin.get();

}

2.實現類似字符串截取的功能

#include <iostream>

#include <sstream>

#include <string>

?

using namespace std;

//實現類似字符串截取的功能

void main()

{

??? char mystring[50] = "china#123#A";

??? for (char *p = mystring; *p != '\0'; p++)

??? {

??????? if (*p == '#')

??????? {

??????????? *p = ' ';

??????? }

??? }

??? istringstream input(mystring);//創建一個字符串掃描流

??? string str;

??? int num;

??? char ch;

??? input >> str >> num >> ch;

?

??? cout << str << endl;

??? cout << num << endl;

??? cout << ch << endl;

?

??? cin.get();

}

運行結果:

3.實現類似字符串截取的功能

#include <iostream>

#include <sstream>

#include <string>

?

using namespace std;

//實現類似字符串截取的功能

void main()

{

??? ostringstream? MYOUT;

??? char str[100] = { 0 };

??? //ostringstream MYOUT(str,sizeof(str));

?

??? char str1[50] = "a1234567b";

?

??? MYOUT << "a1234b" << " " << 123<< ""<< 234.89 << " " << 'h' << " " << str1 << endl;

??? cout << MYOUT.str();

??? //cout <<str;

?

??? cin.get();

}

運行結果如下:

4.字符串流中的put

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

#include <sstream>

#include <string>

#include <stdlib.h>

?

using namespace std;

void main()

{

??? stringstream mystr;//字符串進行輸入

??? mystr.put('X').put('Y');//連個字符輸入

??? mystr << "ZXCV";//字符串輸入

??? cout << mystr.str();

?

??? string str = mystr.str();//定義字符串接受值

?

??? char ch;??? //從字符串內部讀取一個字符

??? mystr >> ch;

??? cout << "\n";

??? cout.put(ch);

?

??? cout << "\n";

??? cout << mystr.str();

??? std::cin.get();

??? system("pause");

}

運行結果

5.str()將流轉換成為字符串string

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

#include <sstream>

#include <string>

#include <stdlib.h>

?

using namespace std;

void main()

{

??? stringstream mystr;//sprintf功能

??? char cmd1[30] = { 0 };

??? char cmd2[30] = { 0 };

??? cin.getline(cmd1, 30).getline(cmd2, 30);//輸入兩個字符串

??? mystr << cmd1 << "&" << cmd2;//字符打印

??? string str = mystr.str();//定義字符串接受值

??? system(str.c_str());

?

??? char cstr[50] = { 0 };//默認的字符串

??? strcpy(cstr, str.c_str());

??? cout << cstr << endl;

??? for (char *p = cstr; *p != '\0'; p++)

??? {

??????? if (*p == '&')

??????? {

??????????? *p = ' ';

??????? }

??? }

??? char newcmd1[30] = { 0 };

??? char newcmd2[30] = { 0 };

??? stringstream? newstr(cstr);//sscanf的功能

??? newstr >> newcmd1 >> newcmd2;

??? cout << newcmd1 << "\n" << newcmd2 << endl;

?

??? system("pause");

}

?

總結

以上是生活随笔為你收集整理的istringstream字符串流,实现类似字符串截取的功能,字符串流中的put,str()将流转换成为字符串string的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。