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