oj输入输出--字符串流,不告知行数

输入未知行整数,整数之间用空格隔开;

#include<iostream>
using namespace std;

int main(){
    int n;
    while(cin>>n){
        int sum=0;
        sum+=n;
        while(cin.get()!='\n'){
            int k=0;
            cin>>k;
            sum+=k;
        }
        cout<<sum<<endl;
    }
    return 0;
}

输入未知行字符串,字符串之间用空格隔开;每行排序后输出。
链接:https://ac.nowcoder.com/acm/contest/5657/I
来源:牛客网

示例1
输入
a c bb
f dddd
nowcoder
输出
a bb c
dddd f
nowcoder

#include<bits/stdc++.h>
using namespace std;
vector<string> v;

int main(){
    string s;
    while(cin>>s){
        v.push_back(s);
        if(cin.get()=='\n'){
            sort(v.begin(),v.end());
            for(string a:v){
                cout<<a<<' ';
            }
            v.clear();
            cout<<endl;
        }
        
    }
    return 0;
}

s表示的其实就是v中得任意一个元素,v可以是数组,也可以是集合,如list或set。这种语句叫做foreach语句。其实就是Iterator迭代器的简化,意思就是循环的从v中拿出一个元素s进行操作。

3、
链接:https://ac.nowcoder.com/acm/contest/5657/J
来源:牛客网

输入描述:
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100
输出描述:
对于每组用例输出一行排序后的字符串,用’,'隔开,无结尾空格
示例1
输入
a,c,bb
f,dddd
nowcoder
输出
a,bb,c
dddd,f
nowcoder

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    string str;
    vector<string> new_s;
    while(cin>>s)
    {
        istringstream iss(s);
        while(getline(iss, str, ','))
        {
            new_s.push_back(str);
        }
        sort(new_s.begin(), new_s.end());
        for(int i=0; i<new_s.size()-1; i++)
        {
            cout<<new_s[i]<<",";
        }
        cout<<new_s[new_s.size()-1]<<endl;
        new_s.clear();
    }
     return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
 
int main()
{
    vector<string> str;
    string s;
    while(cin >> s)
    {
        stringstream stream;
        stream << s;

        while(getline(stream, s, ','))
        {
            str.push_back(s);
        }
        sort(str.begin(), str.end());
        for(int i = 0; i < str.size() - 1; i++)
        {
            cout << str[i] << ',';
        }
        cout<< str.back() << endl;
        str.clear();
    }
 
}

定义了三个类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作。
1、istringstream类用于执行C++风格的串流的输入操作。//它的作用是从string对象str中读取字符。遇空格结束

string str ="I am boy";
string b;
istringstream in(str)while(in >> b){
    cout << b<<endl;//
}
    //输出:I
    am
    boy

2、ostringstream类用于执行C风格的串流的输出操作。
//通过ostringstream实现任意类型转string

ostringstream oss;
    double a=2.21;
    int b=4;
    string str;
    oss<<a<<"_"<<b<<endl;
    str = oss.str();
    //输出就是 2.21_4;

3、strstream类同时可以支持C风格的串流的输入输出操作。

    string str= "hello world I am very happy!";                           
    stringstream sstream(str);                                              //sstream<<
 
    while (sstream)
      {
        string substr;
 
        sstream>>substr;
        cout << substr << endl;    //也可vec.push_back(substr);
      } 

类型转换:

/--------------------int转string------------------/
        stringstream stream;
        string result;
 
        int i = 1000;
 
        stream << i;                //将int输入流
        stream >> result;           //从stream中抽取前面插入的int值
        cout << result << endl;     // cout the string "1000"
 
 /--------------------intchar *------------------/
 
	 stringstream stream;
	 char result[8] ;
 
	 stream << 8888;             //向stream中插入8888
	 stream >> result;           //抽取stream中的值到result
	 cout << result <<endl;      // 屏幕显示 "8888"

在多次转换中使用同一个stringstream对象,记住再每次转换前要使用clear()方法;

    stringstream stream;
    int first, second;
 
    stream<< "456"; //插入字符串
    stream >> first; //转换成int
    cout << first << endl;
 
    stream.clear(); //在进行多次转换前,必须清除stream,否则第二个数据可能输出乱码
    
    stream << true; //插入bool值
    stream >> second; //提取出int
    cout << second << endl;

1.2利用to_string() 进行常见类型转换, 返回值为string

s1=to_string(10.5); //double到string

s2=to_string(123); //int到string

多个字符串拼接

#include <string>
#include <sstream>
#include <iostream>
 
using namespace std;
 
int main()
{
    stringstream sstream;
 
    // 将多个字符串放入 sstream 中
    sstream << "first" << " " << "string,";
    sstream << " second string";
    cout << "strResult is: " << sstream.str() << endl;
 
    // 清空 sstream
    sstream.str("");
    sstream << "third string";
    cout << "After clear, strResult is: " << sstream.str() << endl;
 
    return 0;
}

从上述代码执行结果能够知道:

可以使用 str() 方法,将 stringstream 类型转换为 string 类型;
可以将多个字符串放入 stringstream 中,实现字符串的拼接目的;
如果想清空 stringstream,必须使用 sstream.str(""); 方式;clear() 方法适用于进行多次数据类型转换的场景。
————————————————
版权声明:本文为CSDN博主「liitdar」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liitdar/article/details/82598039

string的getline函数:

getline()函数的定义如下所示

    1. istream& getline ( istream &is , string &str , char delim );

    2. istream& getline ( istream &is , string &str )

例如,可使用cin;
string str ; getline(cin ,str)
也可以使用 stringstream
stringstream ss(“test#”) ; getline(ss,str)
char delim表示遇到这个字符停止读入,通常系统默认该字符为’\n’,也可以自定义字符.

int main()
{
	string str;	
	string str_cin("one#two#three");
	stringstream ss;
	ss << str_cin;
	while (getline(ss, str, '#'))
		cout << str<< endl;
	system("pause");
	return 0;
}

结果:

one
two
three

c++ 实现 split

vector<string> split(string str, char del) {
	stringstream ss(str);
	string temp;
	vector<string> ret;
	while (getline(ss, temp, del)) {
		ret.push_back(temp);
	}
	return ret;
}
int main()
{
	string str_cin("one#two#three");
	vector<string> res= split(str_cin, '#');
	for (auto c : res)
		cout << c << endl;
	system("pause");
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值