如何在 C++ 中将字符串数组转换为字符串类型,如将每个元素连接成一个字符串,并在字符串上使用子字符串?

How to convert a string array to string type in C++, as in concatenate every element together into one string, and use substring on the string?

例如:

读取文件输入并存储到

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