例如:
读取文件输入并存储到
char fileInput[200];
然后我使用它将其转换为某种字符串数组
string fileStrArr(fileInput);
该文件的测试输出如下所示:50014002600325041805如何使用带循环的子字符串获取每个4位字符,并将其转换为数字,例如“ 5001”,“ 4002”,“ 6003” ...?所以我想我需要先将字符串数组转换成字符串?
要将字符数组转换为std :: string类型的对象非常简单
std::string s( fileInput );
假设fileInput为零终止。否则,您必须使用其他一些std :: string构造函数
如果我对您的理解正确,则需要以下内容
#include <iostream>
#include <string>
#include <vector>
int main()
{
const size_t N = 4;
std::string s( "50014002600325041805" );
std::vector <int> v;
for ( size_t i = 0; i != s.size(); )
{
std::string t = s.substr( i, N );
v.push_back( std::stoi( t ) );
i += t.size();
}
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
程序输出为
5001 4002 6003 2504 1805
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句