C++ 如何在c++;

C++ 如何在c++;,c++,C++,假设有一个数组..并且数组的内容为=“ironman”,现在,我需要向这个字符串添加一些额外的字符,如“i*r%o”#n@m^a!n“ 我已经编写了一个在字符串末尾连接的代码,但是我想在字符串之间连接 char in[20] = "ironman"; const unsigned int symbol_size = 5; std::string symbols[symbol_size]; std::string out(in); out = out + "@" + "*" + "#"; 您可以

假设有一个数组..并且数组的内容为=“ironman”,现在,我需要向这个字符串添加一些额外的字符,如“i*r%o”#n@m^a!n“

我已经编写了一个在字符串末尾连接的代码,但是我想在字符串之间连接

char in[20] = "ironman";
const unsigned int symbol_size = 5;
std::string symbols[symbol_size];
std::string out(in);
out = out + "@" + "*" + "#";
您可以使用string.insert(pos、newString)。示例如下:

std::string mystr
mystr.insert(6,str2); 

如果您知道索引,请直接将其指定为“pos”。否则,您可能需要执行某种str.find()操作并传入结果。

如果我正确理解了您的需要,那么您可以使用以下简单的方法

#include <iostream>
#include <string>
#include <cstring>

int main()
{
    char in[] = "ironman";
    char symbols[] = "*%#@^!";

    std::string s;
    s.reserve( std::strlen( in ) + std::strlen( symbols ) );

    char *p = in;
    char *q = symbols;

    while ( *p && *q )
    {
        s.push_back( *p++ );
        s.push_back( *q++ );        
    }

    while ( *p ) s.push_back( *p++ );
    while ( *q ) s.push_back( *q++ );

    std::cout << s << std::endl; 
}   
您可以编写一个单独的函数。比如说

#include <iostream>
#include <string>
#include <cstring>

std::string interchange_merge( const char *s1, const char *s2 )
{
    std::string result;
    result.reserve( std::strlen( s1 ) + std::strlen( s2 ) );

    while ( *s1 && *s2 )
    {
        result.push_back( *s1++ );
        result.push_back( *s2++ );      
    }

    while ( *s1 ) result.push_back( *s1++ );
    while ( *s2 ) result.push_back( *s2++ );

    return result;
}

int main()
{
    char in[] = "ironman";
    char symbols[] = "*%#@^!";

    std::string s = interchange_merge( in, symbols );

    std::cout << s << std::endl; 
}   

用于插入字符。计算两个字符串的数量,创建一个新数组,并从每个字符串副本中提取一个字符到新数组谢谢你的帮助!!
i*r%o#n@m^a!n
#include <iostream>
#include <string>
#include <cstring>

std::string interchange_merge( const char *s1, const char *s2 )
{
    std::string result;
    result.reserve( std::strlen( s1 ) + std::strlen( s2 ) );

    while ( *s1 && *s2 )
    {
        result.push_back( *s1++ );
        result.push_back( *s2++ );      
    }

    while ( *s1 ) result.push_back( *s1++ );
    while ( *s2 ) result.push_back( *s2++ );

    return result;
}

int main()
{
    char in[] = "ironman";
    char symbols[] = "*%#@^!";

    std::string s = interchange_merge( in, symbols );

    std::cout << s << std::endl; 
}   
i*r%o#n@m^a!n