看了一個電影,叫<<劍雨>>,有一句臺詞,在此記錄。spa
「佛陀弟子阿難出家前在道上遇一少女,今後愛慕難捨。佛祖問他,「你有多愛那少女 」?阿難說:「我願化成石橋,受五百年風吹,五百年雨打,五百年日曬,但求此少女從橋上走過」code
寫了一段代碼,在此記錄。orm
-------排版·分隔符-------get
//Given an STL (key,value) pairs, selected2nd returnstring
//extract the second part of a pairit
template<typename P>
struct selected2nd {
typename P::second_type operator()(const P& p) const {
return p.second;
}
};
//A utility template function to make the use of select2nd easy.
//Pass an STL container consisting of (key, value) pairs and it
//automatically creates a select2nd that utilizes the
//value type. This works nicely as the template functions can deduce the
//template parameters based on the function parameters.
template<typename C>
selected2nd<typename C::value_type > make_select2nd( const C& container ) {
return selected2nd<typename C::value_type >();
}io
-------排版·分隔符-------ast
operator std::string() const{
std::stringstream sstr;
sstr<<"Vertex"<<" "
<<id<<" "<<coords.getX()<<" "
<<coords.getY()<<" "
<<coords.getZ()<<" "
<<"{normal("
<<normal.getX()<<" "
<<normal.getY()<<" "
<<normal.getZ()<<")}";
return sstr.str();
}
friend std::ostream& operator<<(std::ostream& out,dcel_vertex const* v){
out<<static_cast<string>(*v);
return out;
}
-------排版·分隔符-------function
void dcel_solid::save_to_m_file(string fileName){
std::ofstream fout(fileName);
//export the vertexs information
std::transform(
vertexs.begin(),vertexs.end(),
ostream_iterator(fout,"\n"),
make_select2nd(vertexs));
//write a blank line
fout<
//export the face information
std::transform(
faces.begin(),faces.end(),
ostream_iterator(fout,"\n"),
make_select2nd(faces));
}
-------排版·分隔符-------form
感受,這樣的代碼比較符合STL的風格,可是有不少代碼沒辦法寫的這樣優雅,老是一個for套一個for,應該能夠被重構成此類代碼,要從新組織邏輯。