2014-05-01 7 views
16

सी ++ 11 मानक में Boost.Format जैसी कुछ भी? मैं बूस्ट का उपयोग करके अन्य सभी जरूरतों के लिए बेहतर सी ++ 11 विकल्प के साथ बचने में सक्षम हूं।सी ++ 11 बूस्ट के बराबर। फोरम

उस मामले के लिए, Boost.Format पाइथन format() के सिंटैक्स को मोमबत्ती नहीं रखता है। ऐसा कुछ भी बेहतर होगा।

+0

क्या अच्छे पुराने सी/कश्मीर एंड आर xxprintf साथ कुछ गड़बड़ है()? – FoggyDay

+6

मुझे इसे सामान्य रूप से पसंद है लेकिन यह सीधे 'स्ट्रिंग' स्वीकार नहीं कर सकता है, जो परेशान है। मैं एक ऐसी विधि पसंद करूंगा जिसके लिए मुझे अपने सभी तारों पर '.c_str()' कॉल करने की आवश्यकता नहीं है। इसके अलावा, यह कहीं भी पाइथन के 'प्रारूप() 'के रूप में अच्छा नहीं है। –

+1

@FoggyDay: कोई प्रकार की सुरक्षा नहीं। बिलकुल। शून्य विस्तारशीलता। –

उत्तर

7

बूस्ट-प्रारूप के समान कुछ के लिए एक प्रस्ताव है। हालांकि, यह न तो सी ++ 11 और न ही सी ++ 14 का हिस्सा है, न ही स्ट्रिंग स्वरूपण से संबंधित कुछ भी जोड़ा जा सकता है।

यहां आप नवीनतम प्रस्ताव पा सकते हैं। बूस्ट-प्रारूप के विपरीत, यह विविध टेम्पलेट्स पर आधारित है।

11

के रूप में सही ढंग से nosid न सी ++ 11 है और न ही सी ++ 14 से बताया स्वरूप बूस्ट करने के लिए एक समान हैं।

std::string s = fmt::format("I'd rather be {1} than {0}.", "right", "happy"); 

और सुरक्षित विकल्पों *printf के कार्य::

fmt::printf("The answer is %d\n", 42); 

हालांकि, fmt library जो वैकल्पिक रूप से इस तरह के variadic टेम्पलेट्स के रूप में सी ++ 11 सुविधाओं का उपयोग करता अजगर की तरह format समारोह के एक कार्यान्वयन प्रदान करता है अस्वीकरण: मैं इस पुस्तकालय के लेखक हूं

0

सी ++ 11 रेगेक्स और वैरिएडिक टेम्पलेट्स के साथ पायथन-जैसी प्रारूप स्ट्रिंग फ़ंक्शन कार्यान्वयन।

/** 
    Helper code to unpack variadic arguments 
*/ 
namespace internal 
{ 
    template<typename T> 
    void unpack(std::vector<std::string> &vbuf, T t) 
    { 
     std::stringstream buf; 
     buf << t; 
     vbuf.push_back(buf.str()); 
    } 
    template<typename T, typename ...Args> 
    void unpack(std::vector<std::string> &vbuf, T t, Args &&... args) 
    { 
     std::stringstream buf; 
     buf << t; 
     vbuf.push_back(buf.str()); 
     unpack(vbuf, std::forward<Args>(args)...); 
    } 
} 

/** 
    Python-like string formatting 
*/ 
template<typename ... Args> 
std::string format(const std::string& fmt, Args ... args) 
{ 
    std::vector<std::string> vbuf; // store arguments as strings 
    std::string in(fmt), out; // unformatted and formatted strings 
    std::regex re_arg("\\{\\b\\d+\\b\\}"); // search for {0}, {1}, ... 
    std::regex re_idx("\\b\\d+\\b");  // search for 0, 1, ... 
    std::smatch m_arg, m_idx;    // store matches 
    size_t idx = 0;       // index of argument inside {...} 

    // Unpack arguments and store them in vbuf 
    internal::unpack(vbuf, std::forward<Args>(args)...); 

    // Replace all {x} with vbuf[x] 
    while (std::regex_search(in, m_arg, re_arg)) { 
     out += m_arg.prefix(); 
     if (std::regex_search(m_arg[0].str(), m_idx, re_idx)) { 
      idx = std::stoi(m_idx[0].str()); 
     } 
     if(idx < vbuf.size()) { 
      out += std::regex_replace(m_arg[0].str(), re_arg, vbuf[idx]); 
     } 
     in = m_arg.suffix(); 
    } 
    out += in; 
    return out; 
} 

उदाहरण: cpp.sh/6nli

संबंधित मुद्दे