C++ stringstream格式化输出输入探索
- 最近在笔试时经常遇见各种输入问题,于是细心总结一波;
- 首先string str; cin>>str;遇到空格结束;
- 于是乎产生了getline(),可与得到一行字符串;空格自动去掉,只要不讲cin和getline混用即可
、cin.getline(s,k);
接收一行中k个字符,可以接收空格
cin.getline()实际有三个参数,cin.getline(字符串,接收个数,结束字符);
当第三个参数省略时,系统默认为'\0';
、getline(cin,s);
和cin.getline()类似,读入一行字符串,值得注意的是cin.getline()属于istream流,而getline()属于string流,二者并不相同。
StringStream
这个东西单独讲,比较重要,包含在sstream库中。
istringstream类用于执行C++风格的串流的输入操作。
ostringstream类用于执行C风格的串流的输出操作。
stringstream类同时可以支持C风格的串流的输入输出操作。
然后stringstream的作用就是从string对象读取字符或字符串。
string s = "ABCD";
stringstream ss(s);
char ch;
while(ss>>ch){
cout << ch << " ";
} //运行结果
//A B C D
又例如
string s = "hello world";
stringstream ss(s);
string str;
while(ss>>str){
cout << str << " ";
} //运行结果
//hello world
在某些题目需要处理字符串时,这些题目往往是输入的一行中包含多个字符以及空格,这个时候就可以利用 stringstream进行单个字符或者单个字符串分析处理了
- 例子程序:
int main()
{ string line;
int k = ;
cout << "===============case1================" << endl;;
while (getline(cin, line)) //可与读到包含空格, ;等字符;但是在ss>>x时被截断
{
int sum = , x;
stringstream ss(line);
while (ss >> x)
{
sum += x;
}
cout << "the sum is :" << sum << endl;
++k;
cout << endl;
cout << "===============case" << k << "================" << endl;;
} return ;
}
- 输出
===============case1================ the sum is : ===============case2================
,,,,
the sum is : ===============case3================
a b
the sum is : ===============case4================
a
the sum is : ===============case5================
- 另外一组:
int main()
{ string line;
int k = ; 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 ;
}
- 输出:
===============case1================
this is very good!
this;is;very;good!;
===============case2================
this,is,very,good!
this,is,very,good!;
===============case3================
- 实验矩阵类型的输入:
3
0 1 2
2 3 4
5 6 7
int main()
{ string line;
int k = ;
//测试矩阵形式的输入:
string input;
int n;
//cin >> n; //输入n行数据,如果后面用getline()后面的换行符不能处理
getline(cin, input);
stringstream ss(input);
ss >> n; vector<vector<int> > vec;
for (int i = ; i < n;i++)
{
getline(cin,input); //会将换行符当做一行
stringstream ss(input);
int x; vector<int> temp;
while (ss>>x) //只能以空格处理分离
{
temp.push_back(x);
}
vec.push_back(temp);
} return ;
}
- 使用方法:
- input1是没有空格的,带有,;将矩阵的行列分出来;
- 但是使用cin>>k后,没有使用getline(input1), 应为input1本身是没有空格的字符串
- 经上面启发:练习这样的输入:
, ,;, ,;, ,;, ,;, ,;, ,;, ,;, ,;, ,
- 测试代码:
int main()
{ string line;
int k = ; 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,' ')) //以换行符结束,中间为空格
{
pair<int, int> pair2;
stringstream s4(temp2);
vector<int> res;
while (getline(s4, temp3, ','))
{
res.push_back(stoi(temp3));
}
pair2.first = res[];
pair2.second = res[]; pair1.push_back(pair2);
} vec.push_back(make_pair(pair1[],pair1[]));
} return ;
}
- 输出:

- 另外参考输入遇到过问题的拼多多题解:推荐朋友 - LintCode
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) //以空格进行划分
// {
// temp.push_back(user);
// }
// vec.push_back(temp);
//} while (getline(cin, str)) //其实可都可以不知道行数
{
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 ;
} int test()
{
//int N,id;
//cin >> N >> id; //直接输入用户数和需要查找的用户id ; 这样就会产生换行符 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 = ; i < in[]; i++)
{
vector<int> temp;
int user; while ((c=cin.get())!= '\n') //文件结果没有换行符了,所以陷入死循环
{
cin.unget();
cin >> user;
temp.push_back(user);
}
if (temp.size()!=)
{
vec.push_back(temp);
} } cout << recommendFriends(vec, in[]) << endl; return ;
}
- 全部代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set> #include<sstream>
#include<fstream>
using namespace std; #define cin infile //一定不能再oj系统中,有错,导致超时等!!!
//C++文件输入
ifstream infile("ini.txt", ifstream::in); //函数功能:将输入字符串s,以字符串c(;)进行拆分,拆分结果放在v中
//函数参数说明:s为输入字符串;c为拆分的字符串;v为拆分结果
//函数返回值:正常返回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 = ;
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 ;
} int main()
{ string line;
int k = ;
/*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;;
//} ////测试矩阵形式的输入:
//string input;
//int n;
////cin >> n; //输入n行数据,如果后面用getline()后面的换行符不能处理
//getline(cin, input);
//stringstream ss(input);
//ss >> n;
//
//vector<vector<int> > vec;
//for (int i = 0; i < n;i++)
//{
// getline(cin,input); //会将换行符当做一行
// 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)) //以换行符结束,中间为空格
{
pair<int, int> pair2;
stringstream s4(temp2);
vector<int> res;
while (getline(s4, temp3, ','))
{
res.push_back(stoi(temp3));
}
pair2.first = res[];
pair2.second = res[]; pair1.push_back(pair2);
} vec.push_back(make_pair(pair1[],pair1[]));
} return ;
}
C++ stringstream格式化输出输入探索的更多相关文章
- Python基础之注释,算数运算符,变量,输入和格式化输出
Python的注释 注释的作用:用自己熟悉的语言,对某些代码进行标注说明,增强程序的可读性: 在python解释器解释代码的过程中,凡是#右边的,解释器都直接跳过这一行: 注释的分类 单行注释 # 这 ...
- go语言基础之格式化输出
1.fmt包的格式化输出输入 格式说明 格式 含义 %% 一个%字面量 %b 一个二进制整数值(基数为2),或者是一个(高级的)用科学计数法表示的指数为2的浮点数 %c 字符型.可以把输入的数字按照A ...
- python学习笔记(基础二:注释、用户输入、格式化输出)
注释 单行:# 多行:上下各用3个连续单引号或双引号 3个引号除了多行注释,还可以打印多行 举例: msg = ''' name = "Alex Li" name2 = name ...
- python学习道路(day1note)(变量,注释,用户输入,格式化输出,if,while,for循环并扩展练习)
python是一门动态解释性的强类型定义语言,其应用范围非常之广 1:进入python语言 #!/usr/bin/env python #_*_coding:utf-8_*_ print(" ...
- (Go)06. Printf格式化输出、Scanf格式化输入详解
Print.Println .Printf .Sprintf .Fprintf都是fmt 包中的公共方法,在需要打印信息时需要用到这些函数,那么这些函数有什么区别呢? Print: 输出到控制台(不接 ...
- C++输入输出流 cin/cout 及格式化输出简介
C++ 可通过流的概念进行程序与外界环境( 用户.文件等 )之间的交互.流是一种将数据自源( source )推送至目的地( destination )的管道.在 C++ 中,与标准输入/输出相关的流 ...
- 四、用户交互(输入input,格式化输出)与运算符
1.接收用户的输入 在Python3:input会将用户输入的所有内容都存成字符串类型 列: username = input("请输入您的账号:") # "egon&q ...
- [C/C++] zltabout(带缩进的格式化输出)v1.0。能以相同的代码绑定到 C FILE 或 C++流
作者:zyl910 一.缘由 在写一些生成文本的程序时,经常需要使用带缩进的格式化输出的功能.以前为此写过不少类似的函数,可惜它们的可重用性很差. 这是因为——1) C语言的FILE*不支持重定向到自 ...
- Javascript实现格式化输出
前两天看面试题,其中有一道要实现js的格式化输出,具体给出的是: Javascript实现格式化输出,比如输入999999999,输出为999,999,999 我的实现方式是 function for ...
随机推荐
- 容器(Container)Frames和Panels
Frames 1)是Window的子类 2)具有标题和缩放角 3)从容器继承并以add方式添加组件 4)能以字符串规定的标题来创建不可见框架对象 5)能将BorderLayout当做缺省布局管理器 6 ...
- String和StringBuilder、StringBuffer的区别?
估计很多Java初学者在学习Java的过程中都会遇到这个问题,那就是String,StringBuilder,StringBuffer这三个类之间有什么区别?今天在这里整理一下,希望对大家有帮助哈.如 ...
- 1、QThreadPool线程池的使用,线程和Widget通过QMetaObject::invokeMethod交互。
自定义一个QThreadPool,N个线程QRunnable,线程和Widget通过QMetaObject::invokeMethod交互. QRunnable非继承自QObject,所以不可以用信号 ...
- SlickMaster.NET 开源表单设计器快速使用指南
前言:在企业数据处理过程中,经常需要通过定制表单来输入业务数据.由于涉及的数据比较离散,并不同于ERP系统的紧密关联数据.假如由开发人员每个增加页面,工作量会比较大,后期后期的维护很升级也耗费时间和精 ...
- 下载8000首儿歌的python代码
下载8000首儿歌的python的代码: #-*- coding: UTF-8 -*- from pyquery import PyQuery as py from lxml import etree ...
- Asp.net core使用IIS在windows上进行托管
摘要 最近项目中,尝试使用asp.net core开发,在部署的时候,考虑现有硬件,只能部署在windows上,linux服务器暂时没有. 部署注意事项 代码中启用iis和Kestrel public ...
- 采用模拟账号读取Exchange server未读邮件的注意事项(链接邮箱问题)【转】
最近做项目碰到Exchange中,用EWS API方法读取的未读邮箱(ConnectingIdType.PrincipalName设置该属性的方法)附带代码部分: 核心代码 using Microso ...
- AutoMapper在MVC中的运用01-配置、使用、单元测试、举例
MVC中,如果想在Domain Model和View Model之间建立映射,用AutoMapper是一个不错的选择.不仅如此,AutoMapper能在不同对象之间建立映射,比如string与int类 ...
- delphi 合并两个 Wav 文件流的函数
合并两个 Wav 文件的函数 实例一 unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphic ...
- iOS开发25:使用SOAP访问Web服务
SOAP是简单对象访问协议,它可看成是HTTP与XML的结合,其中XML部分是作为HTTP报文的实体主体部分.具体信息可以参考百度百科. 在iOS中使用SOAP,需要我们自己组装XML格式的字符串,当 ...