字符串分割单词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;
}
相关推荐
沐怡旸6 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
NAGNIP6 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队7 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
River4169 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥11 小时前
C++ std::set
c++
Fanxt_Ja12 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下12 小时前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶12 小时前
算法 --- 字符串
算法
博笙困了12 小时前
AcWing学习——差分
c++·算法
NAGNIP12 小时前
认识 Unsloth 框架:大模型高效微调的利器
算法