字符串分割单词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;
}
相关推荐
kyle~4 分钟前
C++--- override 关键字 强制编译器验证当前函数是否重写基类的虚函数
java·前端·c++
寻找华年的锦瑟5 分钟前
Qt-配置文件(INI/JSON/XML)
开发语言·qt
HY小海17 分钟前
【C++】AVL树实现
开发语言·数据结构·c++
workflower19 分钟前
Fundamentals of Architectural Styles and patterns
开发语言·算法·django·bug·结对编程
Roc-xb22 分钟前
ModuleNotFoundError: No module named ‘conda_token‘
开发语言·python·conda
仰泳的熊猫22 分钟前
LeetCode:701. 二叉搜索树中的插入操作
数据结构·c++·算法·leetcode
kali-Myon29 分钟前
NewStarCTF2025-Week2-Pwn
算法·安全·gdb·pwn·ctf·栈溢出
老四啊laosi32 分钟前
[双指针] 1. 力扣283.移动零
算法·leetcode·双指针·移动零
每天学一点儿43 分钟前
感知机:单层,多层(二分类,多分类)
人工智能·算法
磊灬泽1 小时前
【Linux驱动开发】PWM子系统-servo
linux·运维·算法