字符串分割单词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;
}
相关推荐
Miraitowa_cheems23 分钟前
LeetCode算法日记 - Day 11: 寻找峰值、山脉数组的峰顶索引
java·算法·leetcode
Sammyyyyy36 分钟前
2025年,Javascript后端应该用 Bun、Node.js 还是 Deno?
开发语言·javascript·node.js
CoovallyAIHub1 小时前
方案 | 动车底部零部件检测实时流水线检测算法改进
深度学习·算法·计算机视觉
CoovallyAIHub1 小时前
方案 | 光伏清洁机器人系统详细技术实施方案
深度学习·算法·计算机视觉
lxmyzzs1 小时前
【图像算法 - 14】精准识别路面墙体裂缝:基于YOLO12与OpenCV的实例分割智能检测实战(附完整代码)
人工智能·opencv·算法·计算机视觉·裂缝检测·yolo12
洋曼巴-young1 小时前
240. 搜索二维矩阵 II
数据结构·算法·矩阵
汉汉汉汉汉1 小时前
C++11新特性详解:从列表初始化到线程库
c++
William一直在路上1 小时前
Python数据类型转换详解:从基础到实践
开发语言·python
看到我,请让我去学习2 小时前
Qt— 布局综合项目(Splitter,Stacked,Dock)
开发语言·qt
GUET_一路向前2 小时前
【C语言防御性编程】if条件常量在前,变量在后
c语言·开发语言·if-else·防御性编程