Thursday, October 27, 2016

Using the to_string() function

The to_string() function is described on these reference pages:
On both pages, you'll see that there are code examples of how to use the function. Both examples have the following line near the top of the code:

#include <string> // std::string, std::to_string

Unfortunately, different programming environments may have different requirements regarding this include statement. The function is part of the standard C++ library, but you may (or may not) have to put this include directive in your code.
  • On Monday, I did not have to put this line in my Xcode file.
  • Others have reported they must use this line if using DevC++.
  • Visual Studio? I have no reports.
  • Unix? I have no reports.
This applies to other functions and libraries too. There may be inconsistencies between the different platforms. The lesson here is to keep your eyes on the reference pages and use the include directives they suggest, just to be safe. To be fair, we did discuss this on Monday night, but things were going pretty fast.

In the future, it's a good idea to always include <iostream>, <string>, <cmath>, and starting soon, <vector>.


No comments:

Post a Comment