stringstream的使用_@pegasus.rutgers.edu

本文介绍如何使用C++中的字符串流类istringstream和ostringstream进行字符串到数字的转换及反之,包括解析输入、格式化输出等常见应用场景。

The stringstream is a class that is useful for extracting data from or writing formatted data to strings.

A very common question, for example is this: ``How do I convert a string to a number ? ''. Of course, there's no way to do so in general, since not all strings look like numbers. But it's certainly not unusual that we'd want to extract a number from a string. Note that the g++ compiler ships without the sstream header. You can download it from here Here's how one does it:
stringstream1.cc

FAQ: How Do I Convert String To Number ?


#include <iostream>
#include <sstream>
using namespace std;
int main()
{
	int a;
	string s = "456";
	istringstream sin(s);
	sin >> a;
	cout << a << endl;
	return 0;
}

The istringstream class is an input stream attached to a string. The constructor copies the string s into its private buffer, and then we may extract data from it using regular stream semantics, in this case we use sin>>a.

FAQ: How Do I Convert A Number To A String ?

or how do I do ``sprintf'' in C++ ?

You may have guessed -- the answer is to use an ostringstream. This data type behaves in a similar way to the istringstream. The main difference is that there is an extra method, str(). This method returns the string that lies in the ostringstream object. There's also a verision of str() that takes a string argument -- this initialises the streams underlying string buffer to that argument. This is commonly used to clear a stream for reuse -- one can call mystream.str("");

stringstream4.cc

#include <sstream>
#include <iostream>
int main()
{
    std::ostringstream strout;
    int x = 42;
    strout << "The answer to the question is " << 42 << std::endl;
    cout << strout.str() << endl;
    strout.str("");
    strout << 53.2;
    cout << strout.str() << endl;
    return 0;
}

Using Stringstreams To Parse Input

A problem that often comes up is this: suppose you have written the following code:

stringstream2.cc

#include <iostream>
	
using namespace std;
int main()
{
    int x;
    do
        cout << "Enter a positive integer (0 to quit)" << endl;
    while ( cin >> x && x != 0 );
    return 0;
}

 

What happens if the user enters

2 3
Or worse, if they enter:
5.0
The problem is that the extraction operator does not expect each item extracted to be on a seperate line. So if you do expect this, you need to be explicit about it in your code. The way to do this is use getline() to read a line of input into a string and then use that string to create an istringstream from which we can extract data. After we've extracted the data we need, we can check for trailing garbage. Here's an example:
stringstream3.cc

#include <sstream>
#include <iostream>

using std::cin;
using std::cout;
using std::end;

int main()
{
    int x;
    char ch;
    std::string myString;
    while (getline ( cin, myString ))
    {
        std::istringstream strin(myString);
        strin >> x;
        if (!strin)
            cout << "Bad input" << endl;
        else if ( strin >> ch )
            cout << "Bad input" << endl;
        else
            cout << "You entered: " << x << endl;
    }
    return 0;
}


Some notes:
  • The istringstream object is declared within the loop, so its scope is the block of the loop. So a new istringstream object is created and the old one is destroyed for each iteration of the loop.
  • The test (!strin) checks to see if the stream is in an error state.
  • The attempt to extract a character is a test to see if there's anything left in the stream after we extract an integer. Note that this ignores whitespace (which is the desired effect in this case)
  • If there are no problems, then the extraction was succesful.

strstream considered harmful

There exists a deprecated class similar to stringstream that is called strstream. Do not confuse these two classes. They are not the same. strstream has a very error prone interface because of the way it handles memory. One problem is that it does not append trailing nulls when str() is called. So you must append a trailing null, or std::ends. Another important thing to remember is that if you use ostrstream with a dynamic buffer, like this:

std::ostrstream strout;
strout << "The answer is ... " << 42 << std::endl << std::ends;
strout.str();
Then calling str has the peculiar side effect that the caller is responsible for managing the memory allocated by strout's buffer, which is pretty silly since the caller does not know how the memory was allocated (it may be allocated using malloc() or new). So to make the stupid thing take its memory back, one makes the following call:
strout.freeze(0);

 

It's worth mentioning that there is another, safer way to use ostrstream and that is to use it with a static buffer. If you do this, you don't need to deal with this freeze() nonsense. To do this, call the constructor that takes a character array as an argument.

char a[100];
std::ostrstream strout(a,100);
strout << "the answer is" << 42 << std::endl << std::ends;
std::cout << a << std::endl;
 
内容概要:本文介绍了基于贝叶斯优化的CNN-LSTM混合神经网络在时间序列预测中的应用,并提供了完整的Matlab代码实现。该模型结合了卷积神经网络(CNN)在特征提取方面的优势与长短期记忆网络(LSTM)在处理时序依赖问题上的强大能力,形成一种高效的混合预测架构。通过贝叶斯优化算法自动调参,提升了模型的预测精度与泛化能力,适用于风电、光伏、负荷、交通流等多种复杂非线性系统的预测任务。文中还展示了模型训练流程、参数优化机制及实际预测效果分析,突出其在科研与工程应用中的实用性。; 适合人群:具备一定机器学习基基于贝叶斯优化CNN-LSTM混合神经网络预测(Matlab代码实现)础和Matlab编程经验的高校研究生、科研人员及从事预测建模的工程技术人员,尤其适合关注深度学习与智能优化算法结合应用的研究者。; 使用场景及目标:①解决各时间序列预测问题,如能源出力预测、电力负荷预测、环境数据预测等;②学习如何将CNN-LSTM模型与贝叶斯优化相结合,提升模型性能;③掌握Matlab环境下深度学习模型搭建与超参数自动优化的技术路线。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注贝叶斯优化模块与混合神经网络结构的设计逻辑,通过调整数据集和参数加深对模型工作机制的理解,同时可将其框架迁移至其他预测场景中验证效果。
PLATFORM: Vendoroh 3.1.400 > Hi3861 HARDWARE: HI3861 900MHz, DEBUG: Current (jlink) On-board (jlink) Building in debug mode Verbose mode can be enabled via `-v, --verbose` option builder(["src\out\hispark_pegasus\wifiiot_hispark_pegasus\target.elf"], []) pip installed. ohos-build installed. SCons installed. [OHOS INFO] [1/2] ACTION //device/hisilicon/hispark_pegasus/sdk_liteos:run_wifiiot_scons(//build/lite/toolchain:riscv32-unknown-elf) [OHOS ERROR] [327/328] ACTION //device/hisilicon/hispark_pegasus/sdk_liteos:run_wifiiot_scons(//build/lite/toolchain:riscv32-unknown-elf) [OHOS ERROR] FAILED: obj/device/hisilicon/hispark_pegasus/sdk_liteos/run_wifiiot_scons_build_ext_components.txt [OHOS ERROR] C:\Users\小郭\AppData\Local\Programs\Python\Python38\python.exe ../../../build/lite/build_ext_components.py --path=../../../device/hisilicon/hispark_pegasus/sdk_liteos "--command=sh hm_build.sh E:/hi3861_hdu_iot_application/src/out/hispark_pegasus/wifiiot_hispark_pegasus win" --target_dir=E:/hi3861_hdu_iot_application/src/out/hispark_pegasus/wifiiot_hispark_pegasus/obj/device/hisilicon/hispark_pegasus/sdk_liteos/build.log --out_dir=E:/hi3861_hdu_iot_application/src/out/hispark_pegasus/wifiiot_hispark_pegasus/error.log [OHOS ERROR] CreateProcess failed: The system cannot find the file specified. [OHOS ERROR] [1/2] ACTION //device/hisilicon/hispark_pegasus/sdk_liteos:run_wifiiot_scons(//build/lite/toolchain:riscv32-unknown-elf) [OHOS ERROR] FAILED: obj/device/hisilicon/hispark_pegasus/sdk_liteos/run_wifiiot_scons_build_ext_components.txt [OHOS ERROR] C:\Users\小郭\AppData\Local\Programs\Python\Python38\python.exe ../../../build/lite/build_ext_components.py --path=../../../device/hisilicon/hispark_pegasus/sdk_liteos "--command=sh hm_build.sh E:/hi3861_hdu_iot_application/src/out/hispark_pegasus/wifiiot_hispark_pegasus win" --target_dir=E:/hi3861_hdu_iot_application/src/out/hispark_pegasus/wifiiot_hispark_pegasus/obj/device/hisilicon/hispark_pegasus/sdk_liteos/build.log --out_dir=E:/hi3861_hdu_iot_application/src/out/hispark_pegasus/wifiiot_hispark_pegasus/error.log [OHOS ERROR] CreateProcess failed: The system cannot find the file specified. [OHOS ERROR] you can check build log in E:\hi3861_hdu_iot_application\src\out\hispark_pegasus\wifiiot_hispark_pegasus\build.log [OHOS ERROR] command: "E:\hi3861_hdu_iot_application\src\prebuilts\build-tools\win64-x64\bin\ninja.exe -w dupbuild=warn -C E:\hi3861_hdu_iot_application\src\out\hispark_pegasus\wifiiot_hispark_pegasus" failed [OHOS ERROR] return code: 1 [OHOS ERROR] execution path: E:\hi3861_hdu_iot_application\src [src\out\hispark_pegasus\wifiiot_hispark_pegasus\target.elf] Error -1 process_command_results: 1
09-21
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值