?? 11-7.cpp
字號:
#include<iostream>
#include<memory>
#include<string>
#include<cctype>
using std::string;
using std::getline;
using std::cout;
using std::endl;
using std::cin;
using std::allocator;
using std::_MAX;
using std::uninitialized_copy;
using std::uninitialized_fill;
template <class T> class Vec
{
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef T value_type;
Vec() { create(); }
explicit Vec(size_type n,const T& t=T()) { create(n,t);}
Vec(const Vec& v) { create(v.begin(),v.end()); }
Vec& operator=(const Vec&);
~Vec() { uncreate();}
T& operator[](size_type i) { return data[i]; }
const T& operator[](size_type i) const {return data[i];}
void push_back(const T& t)
{
if(avail==limit)
grow();
unchecked_append(t);
}
iterator erase(iterator);
void clear();
size_type size() const {return avail-data;}
iterator begin() { return data;}
const_iterator begin() const { return data;}
iterator end() { return avail;}
const_iterator end() const { return avail; }
private:
iterator data; //first element in the Vec
iterator avail; //(one past) the last of element in the Vec
iterator limit; //(one past) the allocated menory
//facilities for memory allocation
allocator<T> alloc; //object to handle memory allocation
//allocation and initialize the underlying array
void create();
void create(size_type,const T&);
void create(const_iterator, const_iterator);
//destroy the element in the array and free the memory
void uncreate();
//support funcions for push_back
void grow();
void unchecked_append(const T&);
};
template<class T>
Vec<T>& Vec<T>::operator =(const Vec& rhs)
{
//check for self-assignment
if(&rhs!=this)
{
//free the array in the left-hand side
uncreate();
//copy elements from the right-hand to the left-hand side
create(rhs.begin(),rhs.end());
}
return *this;
}
template <class T> void Vec<T>::create()
{
data=avail=limit=0;
}
template <class T> void Vec<T>::create(size_type n, const T& val)
{
data=alloc.allocate(n,0);
limit=avail=data+n;
uninitialized_fill(data,limit,val);
}
template <class T>
void Vec<T>::create(const_iterator i,const_iterator j)
{
data=alloc.allocate(j-i,0);
limit=avail=uninitialized_copy(i,j,data);
}
template <class T> void Vec<T>::uncreate()
{
if(data)
{
//destroy (in reverse orde) the elements that were constructed
iterator it=avail;
while(it!=data)
alloc.destroy(--it);
//return all the space that was allocated
alloc.deallocate(data,limit-data);
}
//reset pointers to indicate that the Vec is empty again
data=limit=avail=0;
}
template <class T> void Vec<T>::grow()
{
//when growing, allocate twice as much space as currently in use
size_type new_size=_MAX(2*(limit-data),ptrdiff_t(1));
//allocate new space and copy existing elements to the new space
iterator new_data=alloc.allocate(new_size,0);
iterator new_avail=uninitialized_copy(data,avail,new_data);
//return the old space
uncreate();
//reset pointers to point to the newly allocate space
data=new_data;
avail=new_avail;
limit=data+new_size;
}
template <class T> void Vec<T>::unchecked_append(const T& val)
{
alloc.construct(avail++,val);
}
template <class T> Vec<T>::iterator Vec<T>::erase(iterator i)
{
std::copy(i+1,avail,i);
--avail;
return i;
}
template <class T> void Vec<T>::clear()
{
uncreate();
}
Vec<string> split(const string& s)
{
Vec<string> ret;
typedef string::size_type string_size;
string_size i=0;
//ivarinant: we have processed characters [original value of i, i)
while(i!=s.size())
{
//ignore leading blanks
//invariant: characters in range [original i, current i) are all spaces
while(i!=s.size()&&isspace(s[i]))
++i;
//find end of next word
string_size j=i;
//invariant: none of the characters in range [original j,current j)is a space
while(j!=s.size()&&!isspace(s[j]))
j++;
//if we found some nonwhitespace characters
if(i!=j)
{
//copy from s starting at i and taking j-i chars
ret.push_back(s.substr(i,j-i));
i=j;
}
}
return ret;
}
string::size_type width(const Vec<string>& v)
{
string::size_type maxlen=0;
for(Vec<string>::size_type i=0;i!=v.size();++i)
maxlen=_MAX(maxlen,v[i].size());
return maxlen;
}
Vec<string> frame(const Vec<string>& v)
{
Vec<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 asterisk and a space
for(Vec<string>::size_type i=0;i!=v.size();++i)
{
Vec<string>::size_type l=maxlen-v[i].size();
ret.push_back("* " + string(l/2,' ')+ v[i] + string(l-l/2,' ') + " *");
}
//write the bottom border
ret.push_back(border);
return ret;
}
int main()
{
string s;
getline(cin,s); //int put a line string
Vec<string> v=split(s); //split string to words
Vec<string> u=frame(v); //add a border to words
for(Vec<string>::size_type i=0;i<u.size();++i) //output
cout<<u[i]<<endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -