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;
    }
    

      

  • 相关阅读:
    NOI 2016 区间 解题报告
    有关莫队
    [JSOI2008]最大数 线段树解法
    HDU P3341 Lost's revenge 题解+数据生成器
    BZOJ P1212 [HNOI2004] L语言
    洛谷P3168 [CQOI2015]任务查询系统
    普通平衡树Tyvj1728、luogu P3369 (splay)
    洛谷P3384 树链剖分
    BZOJ P2157 旅游
    【算法导论】第6章,堆排序
  • 原文地址:https://www.cnblogs.com/itdef/p/5913187.html
Copyright © 2011-2022 走看看