ตัวเลือกที่เป็นไปได้อธิบายไว้ด้านล่าง:
1. ตัวเลือกแรก: sscanf ()
    #include <cstdio>
    #include <string>
        int i;
        float f;
        double d;
        std::string str;
        // string -> integer
        if(sscanf(str.c_str(), "%d", &i) != 1)
            // error management
        // string -> float
        if(sscanf(str.c_str(), "%f", &f) != 1)
            // error management
        // string -> double 
        if(sscanf(str.c_str(), "%lf", &d) != 1)
            // error management
นี่เป็นข้อผิดพลาด (แสดงโดย cppcheck) เนื่องจาก"scanf ที่ไม่มีขีด จำกัด ความกว้างของฟิลด์สามารถชนกับข้อมูลอินพุตจำนวนมากใน libc บางรุ่น" (ดูที่นี่และที่นี่ )
2. ตัวเลือกที่สอง: std :: sto * ()
    #include <iostream>
    #include <string>
        int i;
        float f;
        double d;
        std::string str;
        try {
            // string -> integer
            int i = std::stoi(str);
            // string -> float
            float f = std::stof(str);
            // string -> double 
            double d = std::stod(str);
        } catch (...) {
            // error management
        }   
โซลูชันนี้สั้นและสง่างาม แต่มีให้บริการเฉพาะบนคอมไพเลอร์ที่สอดคล้องกับ C ++ 11 เท่านั้น
3. ตัวเลือกที่สาม: sstreams
    #include <string>
    #include <sstream>
        int i;
        float f;
        double d;
        std::string str;
        // string -> integer
        std::istringstream ( str ) >> i;
        // string -> float
        std::istringstream ( str ) >> f;
        // string -> double 
        std::istringstream ( str ) >> d;
        // error management ??
อย่างไรก็ตามด้วยวิธีนี้ยากที่จะแยกแยะระหว่างอินพุตที่ไม่ดี (ดูที่นี่ )
4. ตัวเลือกที่สี่: เพิ่ม lexical_cast
    #include <boost/lexical_cast.hpp>
    #include <string>
        std::string str;
        try {
            int i = boost::lexical_cast<int>( str.c_str());
            float f = boost::lexical_cast<int>( str.c_str());
            double d = boost::lexical_cast<int>( str.c_str());
            } catch( boost::bad_lexical_cast const& ) {
                // Error management
        }
อย่างไรก็ตามนี่เป็นเพียงตัวปิดsstreamและเอกสารแนะนำให้ใช้sstreamสำหรับการจัดการข้อผิดพลาดที่ดีขึ้น (ดูที่นี่ )
5. ตัวเลือกที่ห้า: strto * ()
วิธีนี้มีความยาวมากเนื่องจากการจัดการข้อผิดพลาดและอธิบายไว้ที่นี่ เนื่องจากไม่มีฟังก์ชั่นคืนค่า int ธรรมดาการแปลงจึงจำเป็นในกรณีที่เป็นจำนวนเต็ม (ดูที่นี่สำหรับวิธีการแปลงนี้สามารถทำได้)
6. ตัวเลือกที่หก: Qt
    #include <QString>
    #include <string>
        bool ok;
        std::string;
        int i = QString::fromStdString(str).toInt(&ok);
        if (!ok)
            // Error management
        float f = QString::fromStdString(str).toFloat(&ok);
        if (!ok)
            // Error management 
        double d = QString::fromStdString(str).toDouble(&ok);
        if (!ok)
    // Error management     
สรุปผลการวิจัย
สรุปแล้วทางออกที่ดีที่สุดคือ C ++ 11 std::stoi()หรือเป็นตัวเลือกที่สองคือการใช้ไลบรารี Qt โซลูชันอื่นทั้งหมดไม่สนับสนุนหรือเป็นข้อบกพร่อง
               
              
atoi()ไหม