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

本文提供了一个使用C++实现的控制台应用程序实例,该程序演示了如何创建字符串向量,并通过各种函数来对这些字符串进行边框绘制、水平拼接及垂直拼接等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

  

转载于:https://www.cnblogs.com/itdef/p/5913187.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值