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

作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
浙公网安备 33010602011771号