字符串分割单词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;
}
相关推荐
漫随流水2 分钟前
leetcode算法(429.N叉树的层序遍历)
数据结构·算法·leetcode·二叉树
漫随流水5 分钟前
leetcode算法(116.填充每个节点的下一个右侧节点指针)
数据结构·算法·leetcode·二叉树
浪客川11 分钟前
【百例RUST - 005】所有权和切片
开发语言·后端·rust
古城小栈14 分钟前
Axum: Rust 好用的 Web 框架
开发语言·rust
_OP_CHEN15 分钟前
【算法基础篇】(四十四)数论之欧拉定理与扩展欧拉定理深度解析:从降幂到超大规模幂运算
c++·算法·蓝桥杯·算法竞赛·欧拉定理·扩展欧拉定理·acm/icpc
lfwh18 分钟前
Java 中基于 DBSCAN 算法的车辆交汇点计算实现详解
java·开发语言·算法
liulilittle20 分钟前
DeepWiki: OPENPPP2 工程价值
网络·c++·网络协议·ai·信息与通信·通信
数据大魔方21 分钟前
【期货量化入门】期权交易入门:从零开始学期权量化(TqSdk完整教程)
数据库·python·mysql·算法·区块链·程序员创富
星河耀银海22 分钟前
C++面向对象编程:从基础到实战
开发语言·c++
期货资管源码29 分钟前
期货资管分仓软件开发/平台搭建经验分享
经验分享·算法·eclipse·区块链