博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
string和stringstream用法
阅读量:4156 次
发布时间:2019-05-26

本文共 6168 字,大约阅读时间需要 20 分钟。

一、string

string 是 C++ 提供的字符串类型,和 C 的字串相比,除了有不限长度的优点外,还有其他许多方便的功能。要使用 string, 必须先加入这一行:

#include 

接下來要定义一个字串变量,可以写成:

string s;

我们也可以在定义的同时初始化字串:

string s = "you";

而要取得其中某一个字元,和传统C 的字串一样是用 s[i] 的方式取得。比较不一样的是如果 s 有三个字元,传統 C 的字串的 s[3] 是'\0' 字符,但是 C++ 的 string 则是只到 s[2] 这个字符而已。

做一个对照:

操作 string 字符数组
定义字符串
string s; char s[100];
取得第i个字符 s[i] s[i]
字符串长度 s.length()
或 s.size()
strlen(s)
读入一行 getline(cin, s); gets(s);
赋值 s = "you"; strcpy(s, "you");
字符串连接 s = s + "you";
s += "you";
strcat(s, "you");
字符串比较 s == "you" strcmp(s, "you");

两个string常用的方法是find和substr。在下面的代码当中:find函数从str的第3个位置查起,找到ssdf这个子串后,返回子串的位置。而substr函数从pos位置开始,截取5个字符,赋值给str2。也就是说,str2之后的内容将是ssdfs。

string str = "aaaaddddssdfsasdf";  size_t pos = str.find("ssdf", 3);//可以用if(pos == string::npos) 用来判断是否找到子串。//传送门: string str2 = str.substr(pos, 5);

二、stringstream

stringstream是 C++ 提供的另一个字串型的串流(stream)物件,和之前学过的iostreamfstream有类似的操作方式。要使用stringstream, 必须先加入这一行:

#include 

stringstream主要是用在將一个字符串分割,可以先用.clear( )以及.str( )將指定字串设定成一开始的內容,再用>>把个別的资料输出。

举个例子:

題目:输入的第一行有一个数字 N 代表接下來有 N 行资料,每一行资料里有不固定个数的整数(最多20个,每行最大200个字元),编程將每行的总和打印出來。

输入:

3

1 2 3
20 17 23 54 77 60
111 222 333 444 555 666 777 888 999

输出:

6

251
4995

代码:

#include 
#include
#include
using namespace std;int main(){    string s;    stringstream ss;    int n;    cin >> n;    getline(cin, s);  //读取换行    for (int i = 0; i < n; i++)    {        getline(cin, s);        ss.clear();        ss.str(s);        int sum = 0;        while (1)        {            int a;            ss >> a;            if(ss.fail())                break;            sum += a;        }        cout << sum << endl;    }    return 0;}

三、使用stringstream简化类型转换

C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性、类型安全和可扩展性。接下来,我将举例说明怎样使用这些库来实现安全和自动的类型转换。

一个例子:

#include 
int main(){ int n = 10000; char s[10]; sprintf(s, "%d", n); //s中的内容为“10000” //到目前为止看起来还不错。但是,对上面代码的一个微小的改变就会使程序发生错误 printf("%s\n", s); sprintf(s, "%f", n); //错误的格式化符 printf("%s\n", s); return 0;}
输出:

在这种情况下,由于错误地使用了 %f 格式化符来替代了%d。因此,s在调用完sprintf()后包含了一个不确定的字符串。要是能自动推导出正确的类型,那不是更好吗?

进入stringstream:

由于ns的类型在编译期就确定了,所以编译器拥有足够的信息来判断需要哪些转换。<sstream>库中声明的标准类就利用了这一点,自动选择所必需的转换。而且,转换结果保存在stringstream对象的内部缓冲中。你不必担心缓冲区溢出,因为这些对象会根据需要自动分配存储空间。

<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本。简单起见,我主要以stringstream为中心,因为每个转换都要涉及到输入和输出操作。
注意,<sstream>使用string对象来代替字符数组。这样可以避免缓冲区溢出的危险。而且,传入参数和目标对象的类型被自动推导出来,即使使用了不正确的格式化符也没有危险。

1、string到int的转换

string result = "10000";  int n = 0;  stream << result;  stream >> n;  //n等于10000

2.重复利用stringstream对象

如果你打算在多次转换中使用同一个stringstream对象,记住在每次转换前要使用clear()方法。

在多次转换中重复使用同一个stringstream(而不是每次都创建一个新的对象)对象最大的好处在于效率。stringstream对象的构造和析构函数通常是非常耗费CPU时间的。

3.在类型转换中使用模板

你可以轻松地定义函数模板来将一个任意的类型转换到特定的目标类型。例如,需要将各种数字值,如int、long、double等等转换成字符串,要使用以一个string类型和一个任意值t为参数的to_string()函数。to_string()函数将t转换为字符串并写入result中。使用str()成员函数来获取流内部缓冲的一份拷贝。

template
void to_string(string &result, const T &t) { ostringstream oss; //创建一个流 oss << t; //把值传递入流中 result = oss.str(); //获取转换后的字符并将其写入result } //这样,你就可以轻松地将多种数值转换成字符串了 to_string(s1, 10.5); //double到string to_string(s2, 123); //int到string to_string(s3, true); //bool到string //可以更进一步定义一个通用的转换模板,用于任意类型之间的转换。函数模板convert()含有两个模板参数out_type和in_value,功能是将in_value值转换成out_type类型: template
out_type convert(const in_value & t) { stringstream stream; stream << t; //向流中传值 out_type result; //这里存储转换结果 stream >> result; //向result中写入值 return result; }
测试代码:

#include 
#include
#include
using namespace std;template
void to_string(string &result, const T &t){ ostringstream oss; oss << t; result = oss.str();}template
out_type convert(const in_value & t){ stringstream stream; stream << t; out_type result; stream >> result; return result;}int main(){ //to_string实例 string s1, s2, s3; to_string(s1, 10.5); //double到string to_string(s2, 123); //int到string to_string(s3, true); //bool到string cout << s1 << endl << s2 << endl << s3 << endl << endl; //convert()例子 double d; string salary; string s = "12.56"; d = convert
(s); //d等于12.56 salary = convert
(9000.0); //salary等于"9000" cout << d << endl << salary << endl; return 0;}
输出:

4.结论

在过去留下来的程序代码和纯粹的C程序中,传统的<stdio.h>形式的转换伴随了我们很长的一段时间。但是,如文中所述,基于stringstream的转换拥有类型安全和不会溢出这样的特性,使我们有充足得理由去使用<sstream>。<sstream>库还提供了另外一个特性—可扩展性。你可以通过重载来支持自定义类型间的转换。

5.一些实例

stringstream通常是用来做数据转换的。相比c库的转换,它更加安全,自动和直接。

例子一: 基本数据类型转换例子 int 转 string

#include 
#include
#include
using namespace std;int main(){ stringstream ss; string s; int i = 1000; ss << i; ss >> s; cout << s << endl; return 0;}

运行结果:

例子二: 除了基本类型的转换,也支持char *的转换

#include 
#include
#include
using namespace std;int main(){ stringstream ss; char s[10]; ss << 8888; ss >> s; cout << s << endl; return 0;}

运行结果:

例子三: 再进行多次转换的时候,必须调用stringstream的成员函数.clear()

#include 
#include
#include
using namespace std;int main(){ stringstream ss; int first = 0, second = 0; ss << "456"; // 插入字符串 ss >> first; //转换成int cout << first << endl; ss.clear(); //在进行多次转换前, 必须清除ss ss << true; ss >> second; cout << second << endl; return 0;}

运行结果:

运行.clear()结果

没有运行.clear()结果

6.使用误区

如果stringstream使用不当,当心内存出问题。试试下面的代码,运行程序前打开任务管理器,看看内存变化。

复制代码,把 stream.str("");  那一行的注释去掉,再运行程序,内存就正常了。

看来stringstream似乎不打算主动释放内存( 或许是为了提高效率 ),但如果你要在程序中用同一个流,反复读写大量的数据,将会造成大量的内存消耗,因此这时候,需要适时地清除一下缓冲 ( 用 stream.str("")  )。

另外不要企图用  stream.str().resize(0) 或  stream.str().clear()  来清除缓冲,使用它们似乎可以让stringstream的内存消耗不要增长得那么快,但仍然不能达到清除stringstream缓冲的效果(做个实验就知道了,内存的消耗还在缓慢的增长)

#include 
#include
using namespace std;int main(){ std::stringstream stream; string str; while(1) { //clear()这个名字让很多人想当然地认为它会清除流的内容。 //实际上它并不清空任何内容,它只是重置了流的状态标志。 stream.clear(); //去掉下面这行注释,清空stringstream的缓冲,每次循环内存消耗将不再增加。 //stream.str(""); stream << "you see see you"; stream >> str; // 去掉下面这行注释,看看每次循环,你的内存消耗会增加多少 //cout << "Size of stream = " << stream.str().length() << endl; } return 0;}

参考链接:

你可能感兴趣的文章
MFC下支持中文的SQLite3封装类使用
查看>>
简单高效的多线程日志类
查看>>
研华USB4711A采集卡高速中断模式采集总结
查看>>
从零起步CMFCToolBar用法详解
查看>>
CMFCRibbonStatusBar用法
查看>>
CMFCControlRendererInfo类的参数
查看>>
史上最详细MFC调用mapX5.02.26步骤(附地图测试GST文件)
查看>>
CMFCShellListCtrl使用方法
查看>>
mapnik的demo运行
查看>>
python支持下的mapnik安装
查看>>
milvus手册
查看>>
查看pytorch基于cuda 的哪个版本
查看>>
多目标跟踪的简单理解
查看>>
Near-Online Multi-target Tracking with Aggregated Local Flow Descriptor
查看>>
Joint Tracking and Segmentation of Multiple Targets
查看>>
Subgraph Decomposition for Multi-Target Tracking
查看>>
JOTS: Joint Online Tracking and Segmentation
查看>>
CDT: Cooperative Detection and Tracking for Tracing Multiple Objects in Video Sequences
查看>>
Improving Multi-frame Data Association with Sparse Representations for Robust Near-online Multi-ob
查看>>
Virtual Worlds as Proxy for Multi-Object Tracking Analysis
查看>>