How can I split the contents of a string array to smaller arrays?

The title is a bit hard to understand, so here is my question:
I am reading a file and putting its contents in an array like this:

1
2
3
4
5
6
7
8
9
10
file.open("examplefile.txt");
string arr[3];
int i= 0;
	
while(getline(file, str)){
	arr[i] = str;
	i++;
	}

file.close();


The strings in the array are in this format:

6
10 15 30 20 30 60
30 2 3 4 5 6

And I want to split them into smaller arrays by whitespace.
The result should be an array like this:

 
{{"6"}{"10","15","30","20","30","60"}{"30","2","3","4","5","6"}}

Also, is it possible to give this array as an argument to a function?
Thanks in advance!
Last edited on
You can use a stringstream to split the string.
Is it compulsory to use arrays - vectors would be a better choice?
BTW the strings are numbers why don't read them as int ?
Well, it looks like a 6 x 2 grid, a 2D array.

Will there always be just two rows, or an unknown number?

I presume the first number (6) is the number of columns?

All of your strings appear to be integers. Are you sure you want to treat them as strings, or would they be better as integer or double (floating-point) type?

A basic example of what Thomas1965 and Chervil suggested you could be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <fstream>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>

void waitForEnter();

int main()
{
    std::ifstream inf("examplefile.txt");
    std::vector<std::vector<std::string>> nums;
    for(std::string line; std::getline(inf, line); /**/) {
        std::vector<std::string> tmpvec;
        for(std::istringstream oss(line); oss >> line; /**/) {
            tmpvec.push_back(line);
        }
        nums.push_back(tmpvec);
    }
    inf.close();
    for(const auto& v : nums) {
        for(const auto& s : v) { std::cout << s << ' '; }
        std::cout << '\n';
    }
    std::cout << '\n';
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

There will always be three lines and the number in the first line is the number of the contents of each of the following lines.
I played around with the same idea a little bit, but this isn't really much different from what has already been said.

As already suggested, I'm not sure whether the data should be strings or integers, so I left that option open. Use string or int or double at line 11 according to taste.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

//using dtype = int;
using dtype = std::string;

int load ( std::vector< std::vector<dtype> > & array, string filename);
void print( const std::vector< std::vector<dtype> > & array);
void display( const std::vector< std::vector<dtype> > & array, int size);

int main()
{
    std::vector< std::vector<dtype> > array;

    int size = load(array, "examplefile.txt");

    cout << "size = " << size << '\n';

    print (array);

    display (array, size);
}

int load ( std::vector< std::vector<dtype> > & array, string filename)
{
    ifstream file(filename);

    int size = 0;

    if (!(file >> size))  return 0;
    file.ignore(100, '\n');  // rest of first line to be ignored.

    for (string line; getline(file, line); )
    {
        istringstream ss(line);
        std::vector<dtype> row;
        for (dtype n; ss >> n; )
            row.push_back(n);
        array.push_back(row) ;
    }

    // expect there should be two rows.
    if (array.size() == 2)
        return size;
    return 0;   // problem loading from file
}

void print( const std::vector< std::vector<dtype> > & array)
{
    for (const auto & row : array)
    {
        for (dtype n : row)
            cout << setw(5) << n;
        cout << '\n';
    }
}

void display( const std::vector< std::vector<dtype> > & array, int size)
{
    for (size_t y = 0; y < array.size(); ++y)
    {
        for (int x = 0; x < size; ++x)
            cout << setw(5) << array[y][x];
        cout << '\n';
    }
}


Function load() reads the data from a file into the array.
Functions print() and display() both do the same thing in slightly different ways.
There will always be three lines and the number in the first line is the number of the contents of each of the following lines.

I’m not sure that answers Thomas1965’s questions and all Chervil’s ones:
Question 1:
Thomas1965 wrote:
Is it compulsory to use arrays - vectors would be a better choice?

Question 2:
Thomas1965 wrote:
BTW the strings are numbers why don't read them as int ?

Question 3:
Chervil wrote:
All of your strings appear to be integers. Are you sure you want to treat them as strings, or would they be better as integer or double (floating-point) type?

I’m afraid very often, when they see their sensible requests for details being disregarded, even easy going people became reluctant to give further help.
Topic archived. No new replies allowed.