简体   繁体   中英

How to iterate each element of a dynamically allocated array in c++

I'm trying to iterate an array which has 5 elements, each one with 1024 bytes. How can I iterate each byte of each element?

My code: Class.hh:

static char *sheets[5];

Class.cc:

#define SHEET_SIZE 1024
Class::sheets[0] = new char[SHEET_SIZE];

Because if I do:

for(int i = 0;i<5;i++)
{
    cout << sheets[i] << endl;
}

Wouldn't this print like the whole element and iterate one sheet at a time?

One issue is that you don't know the length of each character array. Better to use std::string or std::vector<std::string> .

Here's some code to iterate over the matrix:

for (int row = 0; row < 5; ++row)
{
    // If row is a C-string then we could use a nul terminator.
    // Otherwise we'll assume a maximum length.
    static const int MAXIMUM_COLUMNS = 1024;
    for (int column = 0;
         (sheets[row][column] != 0) || (column < MAXIMUM_COLUMNS); 
         ++column)
    {
        Do_Something(sheets[row][column]);
        // Or
        std::cout << sheets[row][column];
    }
    std::cout << "\n";
}

The value of sheets[i] is a char pointer which will hopefully be terminated with a zero char otherwise it will segfault your application. If the zero char is at the beginning or in the middle it will not print the entire array so yes, it will fail your requirement.

If you want to iterate over every char of every array you have to do a little bit more:

for(int i = 0;i<5;i++)
{
    cout << "Sheet " << i << endl;
    for ( int j=0; j<SHEET_SIZE; ++j ) {
        cout << sheets[i][j];
    }
    cout << endl;
}

Wouldn't this print like the whole element and iterate one sheet at a time?

It depends on what you have in a sheet . If they are only printable characters (ASCII code between 32 and 126) ending with a null character (0x00), you would get an expected output ( 1023 characters). But, if your sheet contains non-printable characters you won't get a "good" output; and the print will be cut once a null character (0x00) within a sheet .

You can have more control over each char in a sheet if you walk each sheet just as any other array. For example you could treat each sheet as an array of bytes, not just as a string, and dump its contents in hex:

  • Walk the sheets array, and, for each sheet ,
  • walk it as well, get each individual char, extract top and bottom nibbles, print them.

[Demo]

#include <iostream>  // cout
#include <numeric>  // iota

const size_t number_of_sheets{5};
const size_t sheet_size{5};

void print(char* sheet)
{
    for (int i{0}; i < sheet_size; ++i)
    {
        auto to_string = [](auto c) -> char {
            if (0 <= c and c <= 9) { return c + '0'; }
            if (10 <= c and c <= 15) { return c - 10 + 'a'; }
        };
        auto bottom_nibble{(sheet[i] & 0x0f)};
        auto top_nibble{((sheet[i] >> 4) & 0x0f)};
        std::cout << (i ? "\'" : "") << to_string(top_nibble) << to_string(bottom_nibble);
    }
}

int main() {
    static char* sheets[number_of_sheets];
    for (int i{0}; i < number_of_sheets; ++i)
    {
        sheets[i] = new char[sheet_size];
        std::iota(sheets[i], sheets[i] + sheet_size, i * 0x11);
        std::cout << "Sheet " << i << ": ";
        print(sheets[i]);
        std::cout << "\n";
    }
}

// Outputs:
//
//     Sheet 0: 00'01'02'03'04
//     Sheet 1: 11'12'13'14'15
//     Sheet 2: 22'23'24'25'26
//     Sheet 3: 33'34'35'36'37
//     Sheet 4: 44'45'46'47'48

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM