C++ c++;如何将字符串数组转换为字符数组

C++ c++;如何将字符串数组转换为字符数组,c++,arrays,string,char,C++,Arrays,String,Char,我的程序应该有store=5个字符串值——Simpsun GN120、Sonic Lux10、Ultimax G42、Antalpha A200和Nickov N230,然后进行计算,为每个值生成代码。代码将包含值的前3个字母和最后3个字母 值的第一个代码:Simpsun GN120 看起来是这样的:Sim120 我最大的问题是,我无法创建字符串数组,因为获取数组中每个值的长度会使程序崩溃,所以现在我已经创建了一个程序来进行此计算,但只有当字符串不是数组时,如果有人能给我一些技巧,我可以如何改进

我的程序应该有store=5个字符串值——Simpsun GN120、Sonic Lux10、Ultimax G42、Antalpha A200和Nickov N230,然后进行计算,为每个值生成代码。代码将包含值的前3个字母和最后3个字母

值的第一个代码:Simpsun GN120

看起来是这样的:Sim120

我最大的问题是,我无法创建字符串数组,因为获取数组中每个值的长度会使程序崩溃,所以现在我已经创建了一个程序来进行此计算,但只有当字符串不是数组时,如果有人能给我一些技巧,我可以如何改进代码,使该字符串进入数组

#include <iostream>
using namespace std;
int main()
{
    string str = "Simpsun GN120";
    int i;
    string productCode[5];


    for (i = 0; i < str.length(); i++)
    {
        if (i == 0 || i == 1 || i == 2)
        {
            productCode[0] += str[i];
        }
        if (i == str.length() - 1 || i == str.length() - 2 || i == str.length() - 3)
        {
            productCode[0] += str[i];
        }

    }
    cout << productCode[0];

}
#包括
使用名称空间std;
int main()
{
string str=“Simpsun GN120”;
int i;
字符串productCode[5];
对于(i=0;i非常感谢你的回答

using namespace std;
int main()
{
    string str[2] = { "Simpsun GN120", "Sonic Lux10" };
    int i;
    string productCode[5];

    for (int i = 0; i < 2; i++)
    {
        productCode[i] = str[i].substr(0, 3) + str[i].substr(str[i].length() - 3);
    }

    cout << productCode[0] << endl;
    cout << productCode[1];
}
使用名称空间std;
int main()
{
字符串str[2]={“Simpsun GN120”,“Sonic Lux10”};
int i;
字符串productCode[5];
对于(int i=0;i<2;i++)
{
productCode[i]=str[i].substr(0,3)+str[i].substr(str[i].length()-3);
}

非常感谢你的回答

using namespace std;
int main()
{
    string str[2] = { "Simpsun GN120", "Sonic Lux10" };
    int i;
    string productCode[5];

    for (int i = 0; i < 2; i++)
    {
        productCode[i] = str[i].substr(0, 3) + str[i].substr(str[i].length() - 3);
    }

    cout << productCode[0] << endl;
    cout << productCode[1];
}
使用名称空间std;
int main()
{
字符串str[2]={“Simpsun GN120”,“Sonic Lux10”};
int i;
字符串productCode[5];
对于(int i=0;i<2;i++)
{
productCode[i]=str[i].substr(0,3)+str[i].substr(str[i].length()-3);
}

cout使用string类很简单。运行一个循环来执行
productCode[i]=str[i].substr(0,3)+str[i].substr(str[i].length()-3);

您的工作已经完成。

使用string类很简单。运行一个循环来执行
productCode[i]=str[i].substr(0,3)+str[i].substr(str[i].length()-3);

您的工作已经完成。

productCode[0]=str.substr(0,3)+str.substr(str.length()-3)的可能重复项;我可以将字符串转换为字符数组等等,但问题是我想将该行字符串str=“Simpsun GN120”;放在数组中,看起来像这个字符串str[5]={“Simpsun GN120”、“Sonic Lux10”、“Ultimax G42”,“Antalpha A200”,“Nickov N230”};稍后我将更改我的字符串数组,该数组不具有赋值常量大小,它将扩展,因为我必须输入每个名称并将其存储在数组中,如“Simpson GN120”“依此类推。我当前的代码正在工作,但正如我上面所说的,只使用普通字符串,因为如果我想为str.length创建字符串数组,则无法检查数组中每个名称的大小,因为我必须为每个名称单独创建代码substr是什么意思?productCode[0]=str.substr(0,3)+str str.substr(str.length()-3);@jignatiusPossible replicate of productCode[0]=str.substr(0,3)+str.substr(str.length()-3);我可以将字符串转换为字符数组等等,但问题是我想将该行字符串str=“Simpsun GN120”转换为数组,它看起来像这个字符串str[5]={“Simpsun GN120”、“Sonic Lux10”、“Ultimax G42”、“Antalpha A200”,Nickov N230“};稍后我将更改我的字符串数组,该数组不具有赋值常量大小,它将扩展,因为我必须输入每个名称并将其存储在数组中,如“Simpsun GN120”等等。我当前的代码正在工作,但正如我上面所说的,只使用普通字符串,因为如果我想为str.length创建字符串数组,那么检查数组中每个名称的大小是不可行的,因为我必须为每个名称单独创建代码substr意味着什么?productCode[0]=str.substr(0,3)+str str.substr(str.length()-3)而不是C样式字符串数组,你可以使用一个STD::vector(可以添加或删除这个)或STD:::Adtry:FixSuffic。这是C++风格。我想数组(固定大小)以后不会工作,我会让用户能够输入他想要的尽可能多的摄影工具包,并将它们添加到str阵列中,所以阵列的大小会改变我以前从未使用过vector,我仍在努力学习一些东西,但谢谢你,我会记住这一点,并尝试了解更多关于vector的信息。std::vector是什么我希望能够帮助。代替C++风格的字符串数组,可以使用一个STD::(可以添加或删除这个)或STD:::Adtry.(固定大小)。这是C++风格。我想数组(固定大小)以后不会工作,我会让用户能够输入他想要的尽可能多的摄影工具包,并将它们添加到str阵列中,所以阵列的大小会改变我以前从未使用过vector,我仍在努力学习一些东西,但谢谢你,我会记住这一点,并尝试了解更多关于vector的信息。std::vector是什么你想要。很高兴我能帮忙。