The stringstream class is part of the C++ Standard Library. It allows you to manipulate strings as if they were streams, and is often used to convert string representations of data into their actual types.
Here are some examples of how to use stringstream:
Example 1: Convert a string to an integer
```c++ #include #include
int main() { std::string str = "42"; std::stringstream ss(str); int num; ss >> num; std::cout << num << std::endl; return 0; }
This code creates a stringstream object, inserts a string into it, and then extracts an integer from the stream. The resulting integer is printed to the console.
Example 2: Convert a string to a float
c++
#include
#include
int main()
{
std::string str = "3.14";
std::stringstream ss(str);
float num;
ss >> num;
std::cout << num << std::endl;
return 0;
}
```
This code is similar to the previous example, but instead extracts a float from the stream.
In both examples, the `std::stringstream` class is used, which is part of the C++ Standard Library.
C++ (Cpp) StringStream::str - 30 examples found. These are the top rated real world C++ (Cpp) examples of StringStream::str extracted from open source projects. You can rate examples to help us improve the quality of examples.