按空格分隔字符串(利用sstream实现)

4 篇文章 0 订阅

记录一个根据空格分隔字符串的函数,自己写的split

代码:

#include <iostream>
#include <vector>
#include <string>
#include<sstream>
using namespace std;

vector<string> split(string temp)//按空格分隔字符串
{
	vector<string> str;
	string word;
	stringstream input;//利用字符串流输入
	input << temp;
	while (input >> word)//循环把缓冲区数据读出
	{
		str.push_back(word);
	}
	return str;
}
int main()
{
	string str = "hello world !";//测试
	vector<string> vec = split(str);
	for (size_t i=0;i<vec.size();i++)//输出
	{
		cout << vec[i] << endl;
	}
	system("pause");
	return 0;
}

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要按空格分割字符串,可以使用以下几种方法: 1. 使用istringstream和getline()函数: ``` #include <iostream> #include <sstream> #include <vector> int main() { std::string str = "abc def ghi"; std::istringstream ss(str); std::string token; std::vector<std::string> tokens; while(std::getline(ss, token, ' ')) { tokens.push_back(token); } for(const auto& token : tokens) { std::cout << token << std::endl; } return 0; } ``` 这段代码使用istringstream和getline()函数来逐个读取字符串的单词,并将它们存储在一个vector。 2. 使用C风格字符串的strtok()函数: ``` #include <iostream> #include <cstring> #include <vector> int main() { std::string str = "abc def ghi"; char* dup = strdup(str.c_str()); char* token = strtok(dup, " "); std::vector<std::string> tokens; while(token != NULL) { tokens.push_back(std::string(token)); token = strtok(NULL, " "); } free(dup); for(const auto& token : tokens) { std::cout << token << std::endl; } return 0; } ``` 这段代码使用strtok()函数将字符串分割成单词,并将它们存储在一个vector。需要注意的是,由于strtok()函数修改了原始字符串,我们需要首先创建一个副本,然后在使用完毕后释放它。 3. 使用find和substr函数: ``` #include <iostream> #include <string> #include <vector> int main() { std::string str = "abc def ghi"; std::vector<std::string> tokens; std::size_t pos = 0; std::string delimiter = " "; while((pos = str.find(delimiter)) != std::string::npos) { std::string token = str.substr(0, pos); tokens.push_back(token); str.erase(0, pos + delimiter.length()); } tokens.push_back(str); for(const auto& token : tokens) { std::cout << token << std::endl; } return 0; } ``` 这段代码使用find和substr函数来逐个查找并提取字符串的单词,并将它们存储在一个vector。通过不断更新pos和擦除已提取的部分,我们可以实现字符串的分割。 以上是三种常用的在C++空格分割字符串的方法。根据实际需求和个人喜好,可以选择其任意一种方法来实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C++如何进行字符串分割,C++如何按照空格字符串进行解析,C++如何按照逗号对字符串解析](https://blog.csdn.net/Sansipi/article/details/127597697)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值