boost——字符串与文本处理format

    xiaoxiao2021-12-15  36

    #include <iostream> #include <string> #include <vector> #include <set> #include <map> #include <algorithm> #include <boost/format.hpp> #include <iomanip> using namespace boost; using namespace std; using boost::io::group; /*format类摘要 namespace boost { template<class charT, class Traits=std::char_traits<charT> > class basic_format { public: typedef std::basic_string<charT, Traits> string_t; typedef typename string_t::size_type size_type; basic_format(const charT* str); basic_format(const charT* str, const std::locale & loc); basic_format(const string_t& s); basic_format(const string_t& s, const std::locale & loc); basic_format& operator= (const basic_format& x); void clear(); // reset buffers basic_format& parse(const string_t&); // clears and parse a new format string string_t str() const; size_type size() const; // pass arguments through those operators : template<class T> basic_format& operator%(T& x); template<class T> basic_format& operator%(const T& x); // dump buffers to ostream : friend std::basic_ostream<charT, Traits>& operator<< <> ( std::basic_ostream<charT, Traits>& , basic_format& ); // Choosing which errors will throw exceptions : unsigned char exceptions() const; unsigned char exceptions(unsigned char newexcept); // ............ this is just an extract ....... }; // basic_format typedef basic_format<char > format; typedef basic_format<wchar_t > wformat; // free function for ease of use : template<class charT, class Traits> std::basic_string<charT,Traits> str(const basic_format<charT,Traits>& f) { return f.str(); } } // namespace boost */ /*格式化语法: * 1.d:输出宽度为5的整数,不足位用0填充 * 2.%-8.3f:输出左对齐,总宽度为8,小数位3位的浮点数 * 3.s:输出10位的字符串,不足位用空格填充 * 4.x:输出宽度为5的大写16进制数,不足位用0填充 * 5.%|spec|:与printf格式选项功能相同,但两边增加了竖线分隔,可以更好地区分格式化选项与普通字符 * 6.%N%:标记第N个参数,相当于占位符,不带任何奇特的格式化选项 * * 建议: * 1.format由于要做安全检查工作,性能略差,约比printf25倍,为了提高format的性能,建议在编程中应该先建立const format对象,然后拷贝这个对象进行格式化操作 * * 高级功能: * 1.basic_format& bind_arg(int argN,const T& val); //将第argN个参数绑定为val,即使调用clear()也保持不变 * 2.basic_format& clear_bind(int argN); //取消格式化字符串第argN位置的参数绑定 * 3.basic_format& clear_binds(); //取消格式化字符串所有位置参数绑定,并调用argN * 4.basic_format& modify_item(int itemN,T manipulator); //设置格式化字符串第itemN位置的格式化选项,manipulator是一个boost::io::group返回的对象 * */ int main( int argc,char **argv) { cout << format("%s:%d+%d=%d\n")%"sum"% 1%2%(1+2); //经典用法 cout << format("%1% %2%") % 36 7; //简单风格,可以重新排列输出 cout << format("(x,y)=(%1$+5d,%2$+5d)\n") %-23 5; //精确的格式化,带posix_printf位置提示符 format fmt("(%1%+%2%)*%2%=%3%"); fmt % 2 % 5; fmt % ((2+5*5)); format fmt1("d\n%-8.3f\ns\nx\n"); cout << fmt1 b %2.236 %" 12345678" H; cout << fmt.str() << endl; format fmt2("%1% %2% %3% %2% %1%\n"); cout << fmt2 %1 %2 %3; fmt2.bind_arg(2,10); cout << fmt2 %1 %3; fmt2.clear(); cout << fmt2 % group(showbase,oct,111) % 333; fmt2.clear_binds(); fmt2.modify_item(1,group(hex,right,showbase,setw(8),setfill('*'))); cout << fmt2 I 0; return (0); }
    转载请注明原文地址: https://ju.6miu.com/read-1000226.html

    最新回复(0)