Accelerated C++ Exercise 5-0(frame)

本文介绍了一个使用C++实现的简单图片处理库,该库提供了计算字符串宽度、为图片添加边框、垂直拼接及水平拼接等功能。通过具体示例展示了如何使用这些功能来操作文本数据。

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

#ifndef GUARD_pics_h
#define GUARD_pics_h

#include <string>
#include <vector>

std::string::size_type width(const std::vector<std::string>&);
std::vector<std::string> frame(const std::vector<std::string>&); 
std::vector<std::string> vcat(const std::vector<std::string>& , const std::vector<std::string>&); 
std::vector<std::string> hcat(const std::vector<std::string>& , const std::vector<std::string>&); 

#endif
#include <algorithm>   
#include <string>   
#include <vector>   
   
#include "pics.h"   
   
using std::string;   
using std::vector;   
   
using std::max;   
   
string::size_type width(const vector<string>& v)   
{   
    string::size_type maxlen = 0;   
    for(vector<string>::size_type i = 0; i != v.size(); ++i)   
        maxlen = max(maxlen, v[i].size());   
    return maxlen;   
}   
   
vector<string> frame(const vector<string>& v)   
{   
    vector<string> ret;   
    string::size_type maxlen = width(v);   
    string border(maxlen + 4, '*');   
   
    // write the top border   
    ret.push_back(border);   
   
    // write each interior row, bordered by an asterisk and a space   
    for (vector<string>::size_type i = 0; i != v.size(); ++i) {   
        ret.push_back("* " + v[i] + string(maxlen - v[i].size(), ' ') + " *");   
    }   
   
    // write the bottom border   
    ret.push_back(border);   
    return ret;   
}   
   
vector<string> vcat(const vector<string>& top,   
                    const vector<string>& bottom)   
{   
    // copy the `top' picture   
    vector<string> ret = top;   
   
    // copy entire `bottom' picture   
    for (vector<string>::const_iterator it = bottom.begin();   
         it != bottom.end(); ++it)   
        ret.push_back(*it);   
   
    return ret;   
}   
   
vector<string> hcat(const vector<string>& left, const vector<string>& right)   
{   
    vector<string> ret;   
   
    // add 1 to leave a space between pictures   
    string::size_type width1 = width(left) + 1;   
   
    // indices to look at elements from `left' and `right' respectively   
    vector<string>::size_type i = 0, j = 0;   
   
    // continue until we've seen all rows from both pictures   
    while (i != left.size() || j != right.size()) {   
        // construct new `string' to hold characters from both pictures   
        string s;   
   
        // copy a row from the left-hand side, if there is one   
        if (i != left.size())   
            s = left[i++];   
   
        // pad to full width   
        s += string(width1 - s.size(), ' ');   
   
        // copy a row from the right-hand side, if there is one   
        if (j != right.size())   
            s += right[j++];   
   
        // add `s' to the picture we're creating   
        ret.push_back(s);   
    }   
   
    return ret;   
}   

#include <algorithm>   
#include <iostream>   
#include <iterator>   
#include <string>   
#include <vector>   
   
#include "pics.h"   
   
using std::cout;   
using std::copy;   
using std::endl;   
using std::ostream_iterator;   
using std::string;   
using std::vector;   
   
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");   
   
    ostream_iterator<string>ofile(cout, "\n");   
    copy(p.begin(), p.end(), ofile);   
    cout << endl;   
   
    vector<string> f = frame(p);   
    copy(f.begin(), f.end(), ofile);    
	cout << endl; 
	cout << endl;
   
    vector<string> h = hcat(frame(p), p);   
    copy(h.begin(), h.end(), ofile);   
    cout << endl;   
	system("pause");
    return 0;   
}   

ostream_iterator是流迭代器。
流迭代器是标准模板库中的。因此是类模板。
ostream_iterator<int>
指定了类型,就是迭代器读写的类型。
通过这个流迭代器可以把你要输入的写入到指定的流中。
cout就是指定的流。就是标准输出。
可以改成一个输出流就可以,比如一个文件。
通俗的一点说,你把它看成一个指向输出流的指针。通过这个指针你可以把东西写的输出流中。
copy (v.begin(),v.end(),output);
这个意思就是说,把向量V中的数据放到cout输出流中,通过流迭代器output.
ostream_iterator<int> output(cout ,"*");
这个的意思说,放到输出流的时候,没放一个整数,就末尾添加一个*.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值