example4_string_conversion.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main() {
    // String to number
    std::string numStr = "123";
    int num = std::stoi(numStr);
    
    // Number to string
    double pi = 3.14159;
    std::string piStr = std::to_string(pi);
    
    std::cout << "Converted number: " << num << std::endl;
    std::cout << "Converted string: " << piStr << std::endl;
    
    return 0;
}
Back to string