C++ stringstream格式化輸出輸入詳情
最近在筆試時(shí)經(jīng)常遇見各種輸入問題,于是細(xì)心總結(jié)一波;首先string str; cin>>str;遇到空格結(jié)束;于是乎產(chǎn)生了getline(),可與得到一行字符串;空格自動(dòng)去掉,只要不講cin和getline混用即可
cin.getline(s,k);
接收一行中k個(gè)字符,可以接收空格
cin.getline()實(shí)際有三個(gè)參數(shù),cin.getline(字符串,接收個(gè)數(shù),結(jié)束字符);
當(dāng)?shù)谌齻€(gè)參數(shù)省略時(shí),系統(tǒng)默認(rèn)為 '\0' ;
getline(cin,s);
和cin.getline()類似,讀入一行字符串,值得注意的是cin.getline()屬于istream流,而getline()屬于string流,二者并不相同。
StringStream
這個(gè)東西單獨(dú)講,比較重要,包含在sstream庫(kù)中。
istringstream類用于執(zhí)行C++風(fēng)格的串流的輸入操作。ostringstream類用于執(zhí)行C風(fēng)格的串流的輸出操作。stringstream類同時(shí)可以支持C風(fēng)格的串流的輸入輸出操作。
然后stringstream的作用就是從string對(duì)象讀取字符或字符串。
string s = "ABCD";
stringstream ss(s);
char ch;
while(ss>>ch){
cout << ch << " ";
}
//運(yùn)行結(jié)果
//A B C D
又例如:
string s = "hello world";
stringstream ss(s);
string str;
while(ss>>str){
cout << str << " ";
}
//運(yùn)行結(jié)果
//hello world
在某些題目需要處理字符串時(shí),這些題目往往是輸入的一行中包含多個(gè)字符以及空格,這個(gè)時(shí)候就可以利用 stringstream進(jìn)行單個(gè)字符或者單個(gè)字符串分析處理了
例子程序:
int main()
{
string line;
int k = 1;
cout << "===============case1================" << endl;;
while (getline(cin, line)) //可與讀到包含空格, ;等字符;但是在ss>>x時(shí)被截?cái)?
{
int sum = 0, x;
stringstream ss(line);
while (ss >> x)
{
sum += x;
}
cout << "the sum is :" << sum << endl;
++k;
cout << endl;
cout << "===============case" << k << "================" << endl;;
}
return 0;
}
-輸出:
===============case1================
1 2 3 4 5
the sum is :15===============case2================
1,2,3,4,5
the sum is :1===============case3================
1 a 2 b 3
the sum is :1===============case4================
a 1 1 1 1
the sum is :0===============case5================
另外一組:
int main()
{
string line;
int k = 1;
cout << "===============case1================" << endl;;
while (getline(cin, line))
{
string out, x;
stringstream ss(line);
while (ss >> x)
{
cout << x << ";";
}
++k;
cout << endl;
cout << "===============case" << k << "================" << endl;;
}
return 0;
}
輸出:
===============case1================
this is very good!
this;is;very;good!;
===============case2================
this,is,very,good!
this,is,very,good!;
===============case3================
實(shí)驗(yàn)矩陣類型的輸入:
3
0 1 2
2 3 4
5 6 7
int main()
{
string line;
int k = 1;
//測(cè)試矩陣形式的輸入:
string input;
int n;
//cin >> n; //輸入n行數(shù)據(jù),如果后面用getline()后面的換行符不能處理
getline(cin, input);
stringstream ss(input);
ss >> n;
vector<vector<int> > vec;
for (int i = 0; i < n;i++)
{
getline(cin,input); //會(huì)將換行符當(dāng)做一行
stringstream ss(input);
int x;
vector<int> temp;
while (ss>>x) //只能以空格處理分離
{
temp.push_back(x);
}
vec.push_back(temp);
}
return 0;
}
使用方法:
- input1是沒有空格的,帶有,;將矩陣的行列分出來;
- 但是使用cin>>k后,沒有使用getline(input1), 應(yīng)為input1本身是沒有空格的字符串

經(jīng)上面啟發(fā):練習(xí)這樣的輸入:
3 3
0,1 0,2;0,0 1,0;0,1 1,1;0,2 1,2;1,0 1,1;1,1 1,2;1,1 2,1;1,2 2,2;2,0 2,1
測(cè)試代碼:
int main()
{
string line;
int k = 1;
int row, col;
getline(cin, line);
stringstream ss(line);
ss >> row >> col;
getline(cin, line); //第二行
stringstream s(line);
string temp1, temp2, temp3;;
vector<pair<pair<int,int>,pair<int,int>>> vec;
while (getline(s, temp1, ';'))
{
vector<pair<int, int>> pair1;
stringstream s3(temp1);
while (getline(s3, temp2,' ')) //以換行符結(jié)束,中間為空格
{
pair<int, int> pair2;
stringstream s4(temp2);
vector<int> res;
while (getline(s4, temp3, ','))
{
res.push_back(stoi(temp3));
}
pair2.first = res[0];
pair2.second = res[1];
pair1.push_back(pair2);
}
vec.push_back(make_pair(pair1[0],pair1[1]));
}
return 0;
}
輸出:

另外參考輸入遇到過問題題解:
5 0
1 2 3
0 4
0 4
0 4
1 2 3
int main()
{
int N, id;
string str;
getline(cin, str);
stringstream ss(str);
ss >> N >> id;
vector<vector<int>> vec;
//for (int i = 0; i < N; i++)
//{
// vector<int> temp;
// int user;
// getline(cin, str);
// stringstream s(str);
// while (s>>user) //以空格進(jìn)行劃分
// {
// temp.push_back(user);
// }
// vec.push_back(temp);
//}
while (getline(cin, str)) //其實(shí)可都可以不知道行數(shù)
{
vector<int> temp;
int user;
stringstream s(str);
while (s >> user)
{
temp.push_back(user);
}
vec.push_back(temp);
}
cout << recommendFriends(vec, id) << endl;
return 0;
}
int test()
{
//int N,id;
//cin >> N >> id; //直接輸入用戶數(shù)和需要查找的用戶id ; 這樣就會(huì)產(chǎn)生換行符
int N;
vector<int> in;
char c;
while ((c = cin.get()) != '\n')
{
cin.unget();
cin >> N;
in.push_back(N);
}
vector<vector<int>> vec;
for (int i = 0; i < in[0]; i++)
{
vector<int> temp;
int user;
while ((c=cin.get())!= '\n') //文件結(jié)果沒有換行符了,所以陷入死循環(huán)
{
cin.unget();
cin >> user;
temp.push_back(user);
}
if (temp.size()!=0)
{
vec.push_back(temp);
}
}
cout << recommendFriends(vec, in[1]) << endl;
return 0;
}
全部代碼:
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include<sstream>
#include<fstream>
using namespace std;
#define cin infile //一定不能再oj系統(tǒng)中,有錯(cuò),導(dǎo)致超時(shí)等?。?!
//C++文件輸入
ifstream infile("ini.txt", ifstream::in);
//函數(shù)功能:將輸入字符串s,以字符串c(;)進(jìn)行拆分,拆分結(jié)果放在v中
//函數(shù)參數(shù)說明:s為輸入字符串;c為拆分的字符串;v為拆分結(jié)果
//函數(shù)返回值:正常返回0
int split_string(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while (std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
return 0;
}
int main()
{
string line;
int k = 1;
/*cout << "===============case1================" << endl;;
while (getline(cin, line))
{
int sum = 0, x;
stringstream ss(line);
while (ss >> x)
{
sum += x;
}
cout << "the sum is :" << sum << endl;
++k;
cout << endl;
cout << "===============case" << k << "================" << endl;;
}*/
//cout << "===============case1================" << endl;;
//while (getline(cin, line))
//{
// string out, x;
// stringstream ss(line);
// while (ss >> x)
// {
// cout << x << ";";
// }
// ++k;
// cout << endl;
// cout << "===============case" << k << "================" << endl;;
//}
////測(cè)試矩陣形式的輸入:
//string input;
//int n;
////cin >> n; //輸入n行數(shù)據(jù),如果后面用getline()后面的換行符不能處理
//getline(cin, input);
//stringstream ss(input);
//ss >> n;
//
//vector<vector<int> > vec;
//for (int i = 0; i < n;i++)
//{
// getline(cin,input); //會(huì)將換行符當(dāng)做一行
// stringstream ss(input);
// int x;
// vector<int> temp;
// while (ss>>x) //只能以空格處理分離
// {
// temp.push_back(x);
// }
// vec.push_back(temp);
//}
int row, col;
getline(cin, line);
stringstream ss(line);
ss >> row >> col;
getline(cin, line); //第二行
stringstream s(line);
string temp1, temp2, temp3;;
vector<pair<pair<int,int>,pair<int,int>>> vec;
while (getline(s, temp1, ';'))
{
vector<pair<int, int>> pair1;
stringstream s3(temp1);
while (getline(s3, temp2)) //以換行符結(jié)束,中間為空格
{
pair<int, int> pair2;
stringstream s4(temp2);
vector<int> res;
while (getline(s4, temp3, ','))
{
res.push_back(stoi(temp3));
}
pair2.first = res[0];
pair2.second = res[1];
pair1.push_back(pair2);
}
vec.push_back(make_pair(pair1[0],pair1[1]));
}
return 0;
}
到此這篇關(guān)于C++ stringstream格式化輸出輸入詳情的文章就介紹到這了,更多相關(guān)C++ stringstream格式化輸出輸入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用c++編程實(shí)現(xiàn)簡(jiǎn)單的打字小游戲
這篇文章主要為大家介紹了使用c++編程語(yǔ)言來實(shí)現(xiàn)一個(gè)非常簡(jiǎn)單的打字小游戲過程實(shí)現(xiàn)的示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
基于Turbo C(V2.0)編譯錯(cuò)誤信息的詳細(xì)介紹
本篇文章對(duì)Turbo C(V2.0)編譯的錯(cuò)誤信息進(jìn)行了詳細(xì)的介紹。需要的朋友參考下2013-05-05
C++ 11實(shí)現(xiàn)檢查是否存在特定的成員函數(shù)
C++11/14相比以往的C++98/03在很多方面做了簡(jiǎn)化和增強(qiáng),尤其是在泛型編程方面,讓C++的泛型編程的威力變得更加強(qiáng)大,下面這篇文章主要介紹了利用C++ 11實(shí)現(xiàn)檢查是否存在特定成員函數(shù)的相關(guān)資料,需要的朋友可以參考下。2017-02-02
C++實(shí)現(xiàn)簡(jiǎn)單信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
opencv實(shí)現(xiàn)定時(shí)錄像功能
這篇文章主要為大家詳細(xì)介紹了opencv實(shí)現(xiàn)定時(shí)錄像功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
C語(yǔ)言實(shí)現(xiàn)飛機(jī)游戲(1)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)飛機(jī)游戲的第一部分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
C語(yǔ)言中g(shù)etchar()的返回類型為什么是int詳解
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中g(shù)etchar()的返回類型為什么是int的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11

