1

I see a lot of video and explanation about 2 dimensional array with double pointer which is possible when you are storing int, but what if I wanna store string in that 2 dimensional dynamical array?

For example, I'm planning to input my files into my 2 dimensional dynamical array which depends on how many accounts or data I have in my files.

Let's say I have 2 now, and in my program I add 1 more account, which is going to be 3, let say each of my arrays only has 3 elements inside, and then besides that, I do not want to set a constant array.

How am I going to set a variable of two dimensional dynamical array that stores the string? like the very simple one.

Edit : And can also someone explain me why do we have to delete after using the dynamical array? like what if I store something? does it mean my elements also get deleted? or when we close the console it will distract the actual memory? I do not really understand.

2
  • 2
    Why not simply std::vector<std::string>? You then append as many strings as you want to without having to care for memory management... Technically it is equivalent to a jagged 2d array using raw pointers. Commented Sep 6, 2021 at 12:05
  • @Ruks here you go, int** array = new int*[rows]; and then for (int i = 0; i < rows; i++){int[i] = new int[cols]}; Commented Sep 6, 2021 at 12:19

2 Answers 2

3
string** array = new string*[rows];
for (int i = 0; i < rows; i++)array[i] = new string[cols];}

or

vector<vector<string>> array;
Sign up to request clarification or add additional context in comments.

1 Comment

No way, I remember that I went through this trying to use string, but It couldn't work. and I tried again after saw your comments, somehow it worked. thanks :)
1

In C++, in principle don't do manual memory management.

You can use vectors for dynamic arrays. Here's a sample that also shows how to grow both dimensions:

Live On Compiler Explorer

#include <string>
#include <vector>
#include <fmt/ranges.h>

int main() {
    std::vector<std::vector<std::string> > arr 
    {
        { "account 1", "foo", "bar" },
        { "account 2", "qux", "quz" },
    };

    fmt::print("Before: {}\n", fmt::join(arr, "\n\t"));

    // add a row
    arr.push_back({"account 3", "yo", "lo"});
    fmt::print("Extra row: {}\n", fmt::join(arr, "\n\t"));

    // add a column
    for (auto& row : arr)
        row.push_back("extra");
    fmt::print("Extra column: {}\n", fmt::join(arr, "\n\t"));
}

Prints

Before: {"account 1", "foo", "bar"}
        {"account 2", "qux", "quz"}
Extra row: {"account 1", "foo", "bar"}
        {"account 2", "qux", "quz"}
        {"account 3", "yo", "lo"}
Extra column: {"account 1", "foo", "bar", "extra"}
        {"account 2", "qux", "quz", "extra"}
        {"account 3", "yo", "lo", "extra"}

2 Comments

Thanks, it really helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.