C++:istringstream使用自定义字符代替默认空格来划分字符串

问题:
一般对于一整行string句子,我们默认使用istringstram>>word来根据空格来划分单词,但是我们如何自定义划分符来划分string句子呢?

解决方法:
使用getline来自定义字符来划分句子,来获得单词。

参考代码如下:

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

int main()
{
 string str1 = "test istringstream divide string with space";
 string str2 = "test,istringstream,divide,with,other,char";
 string word;
 istringstream test1(str1), test2(str2);
 while (test1>>word)
  cout << word << ".";
 cout << endl;
 while (getline(test2, word, ','))//使用逗号来划分句子
  cout << word << " ";
 cout << endl;
 system("pause");
 return 0;
}

测试结果如下:
在这里插入图片描述

参考:
https://bbs.csdn.net/topics/360194050
https://bbs.csdn.net/topics/390667327

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要按空格分割字符串,可以使用以下几种方法: 1. 使用istringstreamgetline()函数: ``` #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; } ``` 这段代码使用istringstreamgetline()函数来逐个读取字符串中的单词,并将它们存储在一个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、付费专栏及课程。

余额充值