stringstream
时间: 2023-10-12 16:16:12 浏览: 92
stringstream is a class in C++ that provides an easy way to manipulate strings as if they were input/output streams. It allows you to convert a string to a stream and vice versa. You can use it to read data from a string, write data to a string, or both. The stringstream class is part of the header file <sstream>.
Here is an example of how to use stringstream:
```
#include <iostream>
#include <sstream>
int main() {
std::string str = "10 20 30";
std::stringstream ss(str); // Create a stringstream object from the string
int num;
while (ss >> num) { // Read integers from the stringstream
std::cout << num << std::endl; // Print the integers
}
return 0;
}
```
In this example, a string "10 20 30" is converted into a stringstream object. The while loop reads integers from the stringstream object until there are no more integers to read. Each integer is then printed to the console.
阅读全文