字符串分割单词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;
}
相关推荐
余衫马几秒前
在Win10下编译 Poppler
c++·windows·qt·pdf·poppler
工程师00710 分钟前
C#中的AutoUpdater自动更新类
开发语言·c#·自动更新开源库·autoupdate
lsx20240613 分钟前
Java 泛型
开发语言
王老师青少年编程13 分钟前
2024年3月GESP真题及题解(C++七级): 俄罗斯方块
c++·题解·真题·gesp·csp·俄罗斯方块·七级
wzf@robotics_notes13 分钟前
振动控制提升 3D 打印机器性能
嵌入式硬件·算法·机器人
oioihoii20 分钟前
拆解融合:测试开发,一个关于“更好”的悖论
c++
jghhh0127 分钟前
基于MATLAB的可见光通信系统仿真实现
开发语言·matlab
机器学习之心31 分钟前
MATLAB基于多指标定量测定联合PCA、OPLS-DA、FA及熵权TOPSIS模型的等级预测
人工智能·算法·matlab·opls-da
xiaoqider38 分钟前
C++模板进阶
开发语言·c++
yaonoran38 分钟前
【无标题】
java·开发语言·变量