关于sstream中的stringstream的巧用解题

本文介绍如何使用C++中的stringstream库进行不同数据类型间的转换,并利用其缓冲功能轻松实现英文句子的单词逆序排列。通过示例代码详细解释了stringstream在处理字符串输入输出时的便捷性。

最近在做一道编程题的时候,发现用c++中sstream库函数的stringstream对象用起来真的很方便,所以写出来把自己的理解写一下。其实string stream运用更多的地方应该是不同类型格式之间的转化,例如:

头文件是:#include<sstream>

#include <string>
#include <sstream>
#include <iostream> 
 using namespace std;
int main()
{
    stringstream stream;
    string result;
    int i = 2333;
    stream << i; //将int类型的i输入流
    stream >> result; //从stream中抽取前面插入的int值
    cout << result << endl; // 打印出来的就是“2333”
} 

关于c++string stream的用法还有很多,今后有时间会继续补充,网上也很容易找到相关资料。

言归正传,我今天要说的是他在转换过程中的缓冲功能,说回我遇到的编程题目,题目的要求是将句子逆序

将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”
所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符

简单来说就是,我输入一个句子,比如I am a man.  输出要求是man a am I.

你如果想着纯用c语言去做,可能可以实现,但是比起用c++就复杂多了。stringstream对象在类型转化的过程中还提供了缓冲作用,简单说就是如果你输入的是“I am a man.”,通过while循环,那么每循环一次,string stream里面就存一个单词,这些单词以空格区分,是自动的。看下面的例子:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
	string s;
    getline(cin,s);//读入一行
    stringstream ss(s);
    string res="", tmp;
    while(ss>>tmp){
    	
	}
//输入一句话,如:I am a boy.
    cout << tmp; //输出的是你这一句话的最后一个单词。
	return 0;
}

所以如果很好的利用stringstream的这个特点,就能很简单的解决掉这个题目;

/*
题目描述
将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”
所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符
输入描述:
将一个英文语句以单词为单位逆序排放。

输出描述:
得到逆序的句子
*/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
 
int main() {
    string s;
    getline(cin,s);
    stringstream ss(s);
    string res="", tmp;
    //题目的具体实现;
    while (ss>>tmp) {
        if (res=="")
            res=tmp;
        else
            res=tmp+" "+res;
    }
    cout<<res;
   
    return 0;
}

 

#include <iostream> #include <fstream> #include <sstream> #include <string> #include <map> #include <vector> #include <iomanip> using namespace std; // 定义学生信息结构体 struct StudentInfo { string id; float averageScore; }; // 添加了分号 // 定义学生答题情况 struct StudentAnswer { string id; string className; vector<string> answers; }; // 添加了分号 // 读取文件函数 map<string, vector<StudentAnswer> > readFile(const string& filename) { ifstream file(filename.c_str()); // 使用 c_str() 转换 map<string, vector<StudentAnswer> > data; if (!file.is_open()) { cerr << "无法打开文件: " << filename << endl; return data; } // 假设第一行为课程名称、正确答案 string line; getline(file, line); // 获取标题行 // 略过标题行 getline(file, line); while (getline(file, line)) { stringstream ss(line); string id, className, answer; vector<string> answers; ss >> id >> className; while (ss >> answer) { answers.push_back(answer); } data[className].push_back({id, className, answers}); } file.close(); // 移动到循环外部 return data; } // 计算成绩函数 void calculateScore(map<string, vector<StudentAnswer> >& data, const vector<string>& correctAnswers) { for (auto& entry : data) { for (auto& student : entry.second) { int totalScore = 0; for (size_t i = 0; i < student.answers.size() && i < correctAnswers.size(); ++i) { if (student.answers[i] == correctAnswers[i]) { totalScore += 5; } } student.averageScore = static_cast<float>(totalScore) / correctAnswers.size(); } } } // 冒泡排序函数(可选) void bubbleSort(vector<StudentInfo>& students) { for (size_t i = 0; i < students.size() - 1; ++i) { for (size_t j = 0; j < students.size() - i - 1; ++j) { if (students[j].averageScore < students[j + 1].averageScore) { swap(students[j], students[j + 1]); } } } }
最新发布
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值