字符串分割单词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;
}
相关推荐
lly2024067 分钟前
组合模式(Composite Pattern)
开发语言
Billlly9 分钟前
ABC 453 个人题解
算法·题解·atcoder
玉树临风ives17 分钟前
atcoder ABC 452 题解
数据结构·算法
游乐码27 分钟前
c#泛型约束
开发语言·c#
Dontla39 分钟前
go语言Windows安装教程(安装go安装Golang安装)(GOPATH、Go Modules)
开发语言·windows·golang
chushiyunen39 分钟前
python rest请求、requests
开发语言·python
feifeigo12340 分钟前
基于马尔可夫随机场模型的SAR图像变化检测源码实现
算法
铁东博客1 小时前
Go实现周易大衍筮法三变取爻
开发语言·后端·golang
baidu_huihui1 小时前
在 CentOS 9 上安装 pip(Python 的包管理工具)
开发语言·python·pip
南 阳1 小时前
Python从入门到精通day63
开发语言·python