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

      

  • 相关阅读:
    2008年10月小记(SQL删除重复记录,生成表结构,字符串特性,statistics io)
    mysql 中 @
    使用单个innodb表,实现锁,防止游戏被刷物品或者其它资源!
    PSL
    8年PHP,懂点内核, 能写PHP扩展,5年网站经历+3年webgame经历,找个兼职
    Php aes算法
    MySQL触发器自动更新memcache
    怎么配置MySQL服务器(Incorrect string value)
    使用 PHP 将 XML 转化为易读的数组!
    使用Valgrind 查找内存泄露
  • 原文地址:https://www.cnblogs.com/itdef/p/5913187.html
Copyright © 2020-2023  润新知