'to_string' was not declared in this scope

#include <iostream>

int main() {
    int num = 42;
    std::string str = std::to_string(num);
    std::cout << "Converted string: " << str << std::endl;
    return 0;
}

Explanation:

  1. #include <iostream>: This line includes the necessary header file for input and output operations in C++.

  2. int main() {: This line declares the main function, the starting point of the C++ program.

  3. int num = 42;: This line declares an integer variable named num and initializes it with the value 42.

  4. std::string str = std::to_string(num);: This line converts the integer num to a string using the std::to_string function and assigns the result to a string variable named str.

  5. std::cout << "Converted string: " << str << std::endl;: This line prints the converted string to the standard output (console).

  6. return 0;: This line indicates that the program has executed successfully, and the main function returns 0.

  7. }: Closing brace indicating the end of the main function and the end of the program.