如何使用 std::istringstream 处理字符串?
介绍
在本教程中,我们将学习 istringstream 以及如何使用它来处理字符串。istringstream 是在 <sstream> 头文件中定义的 string 类的对象。它用于从字符串流中读取数据。流是在输入输出设备和程序之间的数据流(字符集合)。
<sstream> 头文件定义了 3 个 string 类对象,如下所示:
istringstream
ostringstream
stringstream
它们都用于不同的操作,例如 istringstream 负责流输入,ostringstream 负责流输出,而 stringstream 则同时处理流的输入和输出。
istringstreams 类对象使用包含字符序列的缓冲区来处理流。要使用 istringstream 的属性,请在代码中包含此头文件。
#include <sstream>
读取字符串流中的整数(直到空格)
用于从字符串流中读取整数。
创建一个整数字符串。
创建一个 istringstream 对象,并将整数字符串变量作为参数传递。
使用提取运算符 (>>) 检索字符串变量的值。
>> 运算符执行与 cin 类似的功能,并且为了将字符串分割成标记,它使用空格。它在找到空格时将字符串分割成片段。
示例
以下是 istringstream 的 C++ 实现。istringstream 对象处理数字字符串变量,直到遇到空格,遇到空格后停止。
#include <iostream>
#include <sstream>
int main (){
std::string stringVar = "1 2 3 4 5";
std::istringstream issVar (stringVar);
int number;
issVar >> number;
std:: cout << "the first number is " << number;
return 0;
}
输出
the first number is 1
读取整个整数流
istringstream 可以使用循环来处理整个字符串流。循环可以是 for 循环,具体取决于所需的代码。
创建一个整数字符串变量。
创建一个 istringstream 对象。
将整数字符串变量作为参数传递给 istringstream 对象。
创建一个新变量 number。
使用 while 循环处理字符串,直到 istringstream 对象为空。
使用提取运算符 (<<) 处理 istringstream 对象的 number 变量。
打印结果。
示例 1
让我们实现上述代码:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// Main code
int main(){
// Input number string
string strVar = "1 2 3 4";
// creating object of istringstream
istringstream issVar(strVar);
// Variable for string the values of strVar
int number;
// Conditional statement for checking all integers of the string
while (issVar >> number){
cout << "That stream has number: "
<< number << "\n";
}
cout << "That stream has reached the end successfully"
<< "\n";
return 0;
}
输出
That stream has number: 1 That stream has number: 2 That stream has number: 3 That stream has number: 4 That stream has reached the end successfully
示例 2
可以使用 if-else 条件修改上述代码,以检查流是否成功处理。
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// Main code
int main(){
// Number string
string strVar = "1 2 3 4";
// creating Object for istringstream
istringstream issVar(strVar);
// Variable for string the values
int number;
// Conditional statement to check the issVar
while (issVar){
//streaming the string using >> operator till white space
issVar >> number;
// processing till the end of the issVar
if (issVar){
cout << "The stream has number: "
<< number << "\n";
} else {
cout << "That stream has ended"
<< "\n";
}
}
return 0;
}
输出
The stream has number: 1 The stream has number: 2 The stream has number: 3 The stream has number: 4 That stream has ended
使用 istringstream 从浮点数中提取整数
在上面的示例中,我们从字符串中提取整数。在这里,我们从字符串中提取浮点数和整数。
创建一个字符串变量,并将其初始化为浮点数。
创建一个 istringstream 类对象,并将字符串变量传递给它。
声明一个 int 类型的变量用于整数。
声明一个 float 类型的变量用于浮点数。
使用提取运算符 (<<) 以及 int 和 float 变量来提取字符串。
示例
上述代码的 C++ 实现:
#include <iostream>
#include <sstream>
using std:: istringstream;
using namespace std;
int main(){
//initializing string
string strVar = "45.65";
// istringstream object containing string variable
std::istringstream issVar(strVar);
//variable for the integer data type
int number;
//variable for the float data type
double floatVar;
issVar >> number;
//printing the output while separating integer and float
cout<< "Integer number is: " << number <<endl;
issVar >> floatVar;
cout << "Floating number is: " << floatVar;
return 0;
}
输出
Integer number is: 45 Floating number is: 0.65
使用 std::istringstream 提取混合数据类型
到目前为止,我们已经提取了相同的数据类型和空格。使用 istringstream,您可以同时提取不同类型的数据。
在这个示例中,我们使用一个包含不同数据类型(例如字符串和数字)的字符串。
创建一个字符串变量并对其进行初始化。
创建一个 istringstream 对象,并将字符串变量传递给它。
为字符串的每种数据类型创建一个变量。
使用 << 运算符使用 istringstream 对象和变量提取字符串。
使用在步骤 c 中声明的变量打印输出。
示例
以下是 C++ 实现。
#include <iostream>
#include <sstream>
using namespace std;
int main() {
//string variable
string strVar = "Tutorials Point 34";
// variables to store different values of the string
string first;
string second;
string third;
//istringstream object
std:: istringstream issVar;
issVar.str(strVar);
//streaming string
issVar >> first;
cout << "First word is : " << first << endl;
issVar >> second;
cout << "Second word is: " << second << endl;
issVar >> third;
cout << "Third word is: " << third << endl;
return 0;
}
输出
First word is : Tutorials Second word is: Point Third word is: 34
istringstream 的优点
它用于提取字符串。
它可以通过创建 istringstream 对象来提取多种数据类型。
它可以用于确定提取是否成功。
结论
我们已经到达本教程的结尾。在本教程中,我们通过 istringstream 处理了字符串。Istringstream 使用提取运算符 (>>) 及其对象来提取字符串。它可以与不同的数据类型一起使用。本教程探讨了 istringstream 的含义及其在不同场景中的用途。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP