字符串分割单词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;
}
相关推荐
bikong710 小时前
一种高效绘制余晖波形的方法Qt/C++
数据库·c++·qt
方案开发PCBA抄板芯片解密10 小时前
什么是算法:高效解决问题的逻辑框架
算法
深耕AI10 小时前
【MFC文档与视图结构:数据“仓库”与“橱窗”的梦幻联动 + 初始化“黑箱”大揭秘!】
c++·mfc
songx_9910 小时前
leetcode9(跳跃游戏)
数据结构·算法·游戏
上位机付工10 小时前
C#与倍福TwinCAT3进行ADS通信
开发语言·c#
励志不掉头发的内向程序员11 小时前
STL库——二叉搜索树
开发语言·c++·学习
小白狮ww11 小时前
RStudio 教程:以抑郁量表测评数据分析为例
人工智能·算法·机器学习
AAA修煤气灶刘哥11 小时前
接口又被冲崩了?Sentinel 这 4 种限流算法,帮你守住后端『流量安全阀』
后端·算法·spring cloud
至此流年莫相忘11 小时前
设计模式:模板方法模式
java·开发语言·设计模式
tan180°11 小时前
Boost搜索引擎 查找并去重(3)
linux·c++·后端·搜索引擎