字符串分割单词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;
}
相关推荐
森林里的程序猿猿6 分钟前
并发设计模式
java·开发语言·jvm
222you15 分钟前
四个主要的函数式接口
java·开发语言
小O的算法实验室1 小时前
2026年KBS,赏金猎人优化算法+多无人机移动边缘计算与路径规划,深度解析+性能实测
算法·无人机·边缘计算
用户5671504710211 小时前
OpenClaw 记忆管理系统技术文档
算法
smchaopiao1 小时前
Python中字典与列表合并的问题与解决方法
开发语言·python
935961 小时前
练习题53-60
算法·深度优先
霖大侠2 小时前
Wavelet Meets Adam: Compressing Gradients forMemory-Efficient Training
人工智能·深度学习·算法·机器学习·transformer
敲代码的瓦龙2 小时前
Java?面向对象三大特性!!!
java·开发语言
2501_921649492 小时前
期货 Tick 级数据与基金净值历史数据 API 接口详解
开发语言·后端·python·websocket·金融·区块链