zoukankan      html  css  js  c++  java
  • c++沉思录中 对字符串进行围边 横向连接 竖向连接操作的练习

    // MyPics.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    string::size_type width(const vector<string>& v)
    {
    	string::size_type maxLen = 0;
    	for (vector<string>::const_iterator it = v.begin(); it != v.end(); it++) {
    		if (maxLen < it->size())
    			maxLen = it->size();
    	}
    	return maxLen;
    }
    
    
    vector<string> frame(const vector<string>& p) {
    	vector<string> v;
    	string::size_type maxLen = width(p);
    	string s(maxLen + 4, '*');
    	v.push_back(s);
    	for (vector<string>::const_iterator it = p.begin();
    		it != p.end(); it++){
    		v.push_back("* " + *it + string(maxLen - it->size(),' ') + " *");
    	}
    	v.push_back(s);
    
    	return v;
    }
    
    vector<string> hcat(const vector<string>& right,
    	const vector<string>& left) {
    	vector<string> v;
    	string::size_type rightMaxLen = width(right);
    	int index = 0;
    	while (index < right.size() || index < left.size()) {
    		string s;
    		if (index < right.size()) {
    			s = right[index];
    			s += string(rightMaxLen - right[index].size(),' ' );
    		}
    		else
    			s = string(rightMaxLen,' ');
    
    		if (index < left.size())
    			s += left[index];
    		
    		index++;
    		v.push_back(s);
    	}
    
    	return v;
    }
    
    vector<string> vcat(const vector<string>& top,
    	const vector<string>& bottom) {
    	vector<string> v = top;
    	for (vector<string>::const_iterator it = bottom.begin();
    		it != bottom.end(); it++)
    	{
    		v.push_back(*it);
    	}
    	
    	return v;
    }
    
    
    
    
    int main()
    {
    	vector<string> p;
    	p.push_back("this is an");
    	p.push_back("example");
    	p.push_back("to");
    	p.push_back("illustrate");
    	p.push_back("framing");
    
    	vector<string> v = frame(p);
    	for (vector<string>::const_iterator it = v.begin();
    		it != v.end(); it++) {
    		std::cout << *it << std::endl;
    	}
    	std::cout << std::endl << std::endl;
    
    	v.clear();
    	v = vcat(p,frame(p));
    	for (vector<string>::const_iterator it = v.begin();
    		it != v.end(); it++) {
    		std::cout << *it << std::endl;
    	}
    
    	std::cout << std::endl << std::endl;
    
    	v.clear();
    	v = hcat(p, frame(p));
    	for (vector<string>::const_iterator it = v.begin();
    		it != v.end(); it++) {
    		std::cout << *it << std::endl;
    	}
    
    	std::cout << std::endl << std::endl;
    
    	v.clear();
    	v = hcat(frame(p),p);
    	for (vector<string>::const_iterator it = v.begin();
    		it != v.end(); it++) {
    		std::cout << *it << std::endl;
    	}
    
        return 0;
    }
    

      

  • 相关阅读:
    使用 ASP.NET Core MVC 创建 Web API(五)
    使用 ASP.NET Core MVC 创建 Web API(四)
    使用 ASP.NET Core MVC 创建 Web API(三)
    使用 ASP.NET Core MVC 创建 Web API(二)
    使用 ASP.NET Core MVC 创建 Web API(一)
    学习ASP.NET Core Razor 编程系列十九——分页
    学习ASP.NET Core Razor 编程系列十八——并发解决方案
    一个屌丝程序猿的人生(九十八)
    一个屌丝程序猿的人生(九十七)
    一个屌丝程序猿的人生(九十五)
  • 原文地址:https://www.cnblogs.com/itdef/p/5913187.html
Copyright © 2011-2022 走看看