C++ stringstream

C++ stringstream,第1张

C++ stringstream 简介

        在做LeetCode算法题的时候看到有使用stringstream类处理字符串,自己对这个类不是很了解,查了资料在这里记录一下。

        首先,需要包含头文件

#include 

         定义了三个类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出 *** 作。本文以 stringstream 为主,介绍流的输入和输出 *** 作。

         主要用来进行数据类型转换,由于 使用 string 对象来代替字符数组(snprintf 方式),避免了缓冲区溢出的危险;而且,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符号的问题。简单说,相比 C 编程语言库的数据类型转换, 更加安全、自动和直接。


数据类型转换

        使用stringstream将 int 类型转换为 string 类型:

#include 
#include 

int main()
{
    std::stringstream sstream;
	std::string strResult;
	int nValue = 1000;

	// 将int类型的值放入输入流中
	sstream << nValue;
	// 从sstream中抽取前面插入的int类型的值,赋给string类型
	sstream >> strResult;

	std::cout << "[cout]strResult is: " << strResult << std::endl;    // [cout]strResult is: 1000
	printf("[printf]strResult is: %sn", strResult.c_str());          // [printf]strResult is: 1000
 
    return 0;
}
多个字符串拼接

        在 stringstream 中存放多个字符串,实现多个字符串拼接的目的(其实完全可以使用 string 类实现):

#include 
#include 
 
int main()
{
	std::stringstream sstream;

	// 将多个字符串放入 sstream 中
	sstream << "first" << " " << "string,";
	sstream << " second string";

    // 使用 str() 方法,可以将 stringstream 类型转换为 string 类型;
	std::cout << "strResult is: " << sstream.str() << std::endl;     // strResult is: first string, second string
 
    return 0;
}
清空stringstream

        清空 stringstream 有两种方法:clear() 方法以及 str("") 方法。如果想清空 stringstream,使用 str("") 的方式;clear() 方法适用于进行多次数据类型转换的场景。

#include 
#include 
 
int main()
{
    std::stringstream sstream;
	int first, second;

	// 插入字符串
	sstream << "456";
	// 转换为int类型
	sstream >> first;
	std::cout << first << std::endl;             // 456

	// 在进行多次类型转换前,必须先运行clear(),不能使用str(""),否则可能会有问题
	sstream.clear();

	// 插入bool值
	sstream << true;
	// 转换为int类型
	sstream >> second;
	std::cout << second << std::endl;            // 1
 
    return 0;
}
分割字符串

        标准库中的string类没有实现像C#和Java中string类的split函数,所以想要分割字符串的时候需要我们自己手动实现。但是有了stringstream类就可以很容易的实现,stringstream默认遇到空格、tab、回车换行会停止字节流输出。

#include 
#include 
 
int main()
{
	std::stringstream ss("this apple is sweet");
	std::string word;
	while (ss >> word)
	{
		std::cout << word << std::endl;   // 这里依次输出this、apple、is、sweet四个单词
	}
 
    return 0;
}

         也可以这样写:

#include 
#include 
 
int main()
{
    std::vector v1;         // 创建一个vector数组来保存分割后的字符串
	
	auto insert = [&](const std::string& s) {
		std::stringstream ss(s);
		std::string word;
		while (ss >> word)
		{
			v1.push_back(move(word));
		}
	};

    // 这样可以多次调用
	insert("this apple");
    insert("is sweet");

    for (auto& a : v1)
    {
        std::cout << a << std::endl;     // 这里依次输出this、apple、is、sweet四个单词
    }
 
    return 0;
}

         可以使用getline()函数用其他字符分割字符串,第一个参数 - 流中获取数据,第二个参数 - 把数据转换成字符串,第三个参数 - 分隔符

#include 
#include 
 
int main()
{
    std::string str = "this,is,apple";
	std::istringstream ss(str);
	std::string token;

	while (std::getline(ss, token, ',')) {
		std::cout << token << 'n';        // 这里依次输出this、is、apple三个单词
	}
 
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5718855.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存