Splitting each element of an array into an another array

I'm currently working on a C++ project. For this project, I need to split elements of an array into another array. I will specify my need. Currently, I have an array like this:

array_1 = [first,second,third]

I need to change this array to something like this:

array_2 = [[f,i,r,s,t],[s,e,c,o,n,d],[t,h,i,r,d]]

How can I do this? it would be a great help for me. Thanks for reading my question.

3 answers

  • answered 2021-05-15 10:19 CroCo

    C++ provides a plethora of solutions for this problem. A convenient way is to use vector. See the following.

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<string> array_1 = {"first","second","third"};
        vector< vector<char> > array_2;
        
        for(int i(0); i < array_1.size(); ++i){
            vector<char> charVec(array_1[i].begin(), array_1[i].end()); 
            array_2.push_back( charVec );
        }
        
        return 0;
    }
    

  • answered 2021-05-15 10:21 DanielGS

    you could use the std vector library for this. In this example I worked with strings and chars but should be the same way for others types (the std vector library works with templates).

    #include <stdio.h>
    #include <string>
    #include <vector>
    
    int main ()
    {
        //We initialize our vectors
        std::vector<std::string> array1;
        array1.push_back("first");
        array1.push_back("second");
        array1.push_back("third");
    
        std::vector<char> array2;
        //Now, using the string's fuction .size() we can iterate our string and save
        //each character into array2 with push_back()
        for(int i=0; i<array1.size(); i++)
        {
            std::string aux = array1[i];
            for(int j=0; j<aux.size(); j++)
            {
                array2.push_back(aux[j]);
            }
        }
        //Finally we print the result
        for(int i=0; i<array1.size(); i++)
        {
            printf("%s\n",array1[i].c_str());
        }
        for(int i=0; i<array2.size(); i++)
        {
            printf("%c\n",array2[i]);
        }
    
        return 0;    
    }
    

  • answered 2021-05-15 10:22 Serdar Oztetik

    You don't seem to have types in your array_1 so I am assuming it is std::vector<std::string>

    std::vector<std::string> array_1 = {"first", "second", "third"};
    

    and assuming array_2 is a vector of char vectors: std::vector<std::vector<char>>

    you need to loop over array_1 then loop over each character in each string. create a new vector and put it into array_2

    for(const auto& word : array_1)
    {
        std::vector<char> vec;
        for(const auto& c : word)
        {
            vec.push_back(c);
        }
        array_2.push_back(vec);
    }
    

    full working implementation would be:

    #include <vector>
    #include <string>
    
    int main()
    {
        std::vector<std::string> array_1 = {"first", "second", "third"};
        std::vector<std::vector<char>> array_2 = {};
        for(const auto& word : array_1)
        {
            std::vector<char> vec;
            for(const auto& c : word)
            {
                vec.push_back(c);
            }
            array_2.push_back(vec);
        }
    }