字符串分割单词C++

复制代码
#include<iostream>
#include<bits/stdc++.h>

using namespace std;

vector<string> split(string s) {
        int len = s.length();
        int i(0);
        string ans= "", temp;
        vector<string> res;

        while(i<len) {
            temp = "";
            while(i<len&&s[i]==' ') {i++;}
            while(i<len&&s[i]!=' ') {
                temp += s[i++];
            }
            res.push_back(temp);
        }

        return res;
}


int main(int argc, char** argv) {

    string s = "dog cat cat dog";
    vector<string> res;

    res = split(s);
    cout<<"字符串单词数量   "<<res.size()<<"------"<<endl;
    for(auto i:res) {
        cout<<i<<endl;
    }
    return 0;
}
相关推荐
mask哥2 分钟前
力扣算法java实现汇总整理(上)
java·算法·leetcode
2zcode5 分钟前
运动模糊图像复原的MATLAB仿真与优化
开发语言·matlab
袁雅倩199716 分钟前
当吸尘器、筋膜枪都用上Type-C,供电方案该怎么选?浅谈PD取电芯片ECP5702的应用
c语言·开发语言·支持向量机·动态规划·推荐算法·最小二乘法·图搜索算法
如果'\'真能转义说44 分钟前
OOXML 文档格式剖析:哈希、ZIP结构与识别
xml·算法·c#·哈希算法
Aaswk1 小时前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite1 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
王老师青少年编程2 小时前
csp信奥赛C++高频考点专项训练之字符串 --【子串查找】:[NOIP 2009 提高组] 潜伏者
c++·字符串·csp·高频考点·信奥赛·子串查找·潜伏者
Cyber4K2 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
梦梦代码精2 小时前
BuildingAI 上部署自定义工作流智能体:5 个实用技巧
大数据·人工智能·算法·开源软件
初願致夕霞2 小时前
基于系统调用的Linux网络编程——UDP与TCP
linux·网络·c++·tcp/ip·udp