zoukankan      html  css  js  c++  java
  • 将字符串拆成单词,并算最长的长度

    #include <vector>
    #include <string>
    #include <cctype>
    #include <iostream>
    
    using namespace std;
    
    //根据空格等将字符串,拆分成多个单词。
    vector<string> split(const string& s)
    {
        vector<string> ret;
        typedef string::size_type string_size;
        string_size i=0;
    
        while(i!=s.size())
        {
            while(i!=s.size() && isspace(s[i]))
            {
                ++i;
            }
            string_size j=i;
            while(j!=s.size() && !isspace(s[j]))
            {
                ++j;
            }
            if(i!=j)
            {
                ret.push_back(s.substr(i,j-i));
                i=j;
            }        
        }
        return ret;
    }
    
    //求上述vector<string>中最长的长度
    string::size_type GetmaxLen(const vector<string>& v)
    {
        string::size_type maxLen=0;
        for(vector<string>::const_iterator i=v.begin();i!=v.end();++i)
        {
            maxLen=max(maxLen,(*i).size());
        }
        return maxLen;
    }
    
    //用一个“*”组成的矩形,把上面的vector<string>装框!
    vector<string> Load(const vector<string>& v)
    {
        vector<string> ret;
        string::size_type maxLen=GetmaxLen(v);
        string border(maxLen+4,'*');
        ret.push_back(border); //top
        
        for(vector<string>::size_type i=0;i!=v.size();++i)
        {
            ret.push_back("* "+v[i]+string(maxLen-v[i].size(),' ')+" *");
        }
        ret.push_back(border);//bottom
        return ret;
    }
  • 相关阅读:
    使用IDEA整合SSM框架
    宏任务与微任务
    setTimeout的实现及其问题
    JS的闭合(Closure)
    this详解
    JS的作用域和作用域链
    JS的执行上下文
    JS内存机制
    抽象工厂模式(c++实现)
    迭代器模式(c++实现)
  • 原文地址:https://www.cnblogs.com/hometown/p/3365643.html
Copyright © 2011-2022 走看看