字符串分割单词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 分钟前
中科院1区SCI-哲学命题优化算法Philosophical proposition optimizer-附Matlab免费代码
linux·算法·matlab
2401_892000528 分钟前
Flutter for OpenHarmony 猫咪管家App实战 - 疫苗记录实现
开发语言·javascript·flutter
rit84324998 分钟前
基于光流场的 Demons 算法
算法
哈哈不让取名字9 分钟前
C++代码冗余消除
开发语言·c++·算法
heart_fly_in_sky10 分钟前
RK3576平台OpenCL GPU编程实战指南(Lesson 2)
c++
ghie909011 分钟前
基于C#实现俄罗斯方块游戏
开发语言·游戏·c#
棱镜Coding12 分钟前
LeetCode-Hot100 27.合并两个有序链表
算法·leetcode·链表
燕山石头16 分钟前
java模拟Modbus-tcp从站
java·开发语言·tcp/ip
lixzest18 分钟前
C++工程师的成长
开发语言·c++·程序人生·职场和发展