字符串分割单词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;
}
相关推荐
希望之晨10 分钟前
c++ 11 学习 函数模板
linux·开发语言·c++
独自破碎E12 分钟前
BISHI73 【模板】欧拉函数计算Ⅰ ‖ 朴素求值:试除法
java·开发语言
pen-ai12 分钟前
【Yolo系列】Yolov3 目标检测算法原理详解
算法·yolo·目标检测
田里的水稻12 分钟前
EP_基于UWB和单线激光雷达的托盘转送
人工智能·算法·数学建模·机器人·自动驾驶
List<String> error_P14 分钟前
DFS(深度优先搜索)
数据结构·算法·dfs
m0_5312371717 分钟前
C语言-结构体进阶
c语言·开发语言
今儿敲了吗17 分钟前
27| 魔法封印
数据结构·c++·笔记·学习·算法
遨游xyz18 分钟前
BM算法(Boyer-Moore)
开发语言·python
honortech22 分钟前
算法题中的 mid 中点问题
算法
今儿敲了吗23 分钟前
30| 木材加工
数据结构·c++·笔记·学习·算法