stringstream用法

stringstream
作用1:分割字符串 
作用2:更安全的類型轉換

 

做一個對照:

操作 string 字符數組
定義字符串 string s; char s[100];
取得第i個字符 s[i] s[i]
字符串長度 s.length()
或 s.size()
strlen(s)
讀入一行 getline(cin, s); gets(s);
賦值 s = "you"; strcpy(s, "you");
字符串連接 s = s + "you";
s += "you";
strcat(s, "you");
字符串比較 s == "you" strcmp(s, "you");

 

size_t pos = str.find("ssdf", 3);
//可以用if(pos == string::npos) 用來判斷是否找到子串。

string str2 = str.substr(pos, 5);
//返回從pos開始的5長度子串  
//如果不存在  pos位置超過字符串總長度 返回空串
//如果從pos開始並沒有這麼長的長度  就返回有效的 abc    substr(0,100)== “abc”

stringstream

stringstream主要是用在將一個字符串分割,可以先用   .clear( )     以及      .str( )        將指定字串設定成一開始的內容,再用>>把個別的資料輸出。

舉個例子:

 

題目:輸入的第一行有一個數字 N 代表接下來有 N 行資料,每一行資料裏有不固定個數的整數(最多20個,每行最大200個字元),編程將每行的總和打印出來。

輸入:

3
1 2 3
20 17 23 54 77 60
111 222 333 444 555 666 777 888 999

輸出:

6
251
4995

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
 
int main()
{
	//char kk[100] = "123  9 1";
    string s;
    stringstream ss;
    int n;
	
    cin >> n;
    getline(cin, s);  //讀取換行
    for (int i = 0; i < n; i++)
    {
        getline(cin, s);
        ss.clear();//重置流的狀態標誌
        ss.str(s);//成員函數 將字符串 導入 ss流中  char*  string都可以
        int sum = 0;
 
        while (1)
        {
            int a;
            ss >> a;
            if(ss.fail())//比如ss流中有 1  23  156   當讀完156後 再嘗試讀 流中已經沒數字 就返回真 
                break;
            sum += a;
        }
        cout << sum << endl;
    }
 
    return 0;
}

使用stringstream簡化類型轉換

以前用c

#include <iostream>
#include <cstdio>

using namespace std;

int main() {
	char a[100];
	sprintf(a, "%d", 1000);//將右邊的內容打印在字符串a中
	printf("%s\n", a); 
	//輸出字符串1000
	int k;
	sscanf(a, "%d", &k);//從字符串a中讀取內容 寫入k  確定 要控制好類型對應 而stringstream就不需要了
	printf("%d\n", k);
	//輸出整型1000


	sprintf(a, "%lf", 0.00001);
	printf("%s\n", a); //可以

	double k2;
	sscanf(a, "%f", &k2);
	printf("%f\n", k2);//亂碼

	return 0;
}

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
	stringstream s;
	long long k;
	string a = "111111111111111111";
	//s.str(a);   儘量不用這個吧  不用下面那句 用這句 下面的100不輸出 不懂
	s << a;//這樣來給 插入到流
	s >> k;
	cout << k << endl;



	int kk = 100;
	s.clear();//對於一個流 若進行多次轉換  記得clear()
	//s.str("");  儘量不用這個
	s << kk;
	string tmp;
	s >> tmp;
	cout << tmp << endl;
	

	char f[100];//c字符串也可以
	s.clear();
	s << tmp;
	s >> f;
	cout << f <<endl;
	return 0;
}

 

下面是運用模版 實現任意類型轉換 

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
 
template<class T>
void to_string(string &result, const T &t)
{
 
	ostringstream oss;
	oss << t;
	result = oss.str();
}
 
template<class out_type, class in_value>
out_type convert(const in_value & t)
{
	stringstream stream;
 
	stream << t;
	out_type result;
	stream >> result;
 
	return result;
}
 
int main()
{
	//to_string實例
	string s1, s2, s3;
 
	to_string(s1, 10.5);  //double到string
	to_string(s2, 123);  //int到string
	to_string(s3, true);  //bool到string
	cout << s1 << endl << s2 << endl << s3 << endl << endl;
 
	//convert()例子
	double d;
	string salary;
	string s = "12.56";
 
	d = convert <double> (s);  //d等於12.56
	salary = convert <string> (9000.0); //salary等於"9000"
 
	cout << d << endl << salary << endl;
 
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章