Matrix with C++ Strings

Asked

Viewed 129 times

0

I would like to create a C++ Mxn array for storing strings, like the matrix below:

[['3325309756482910474',  'CARRO',  '2506794813021649539', '618.57'],
 ['3325309756485249504',  'MOTO',  2506794813021649539',   '649.32'],
 ['3328687456208678517',  'BIKE',  '2506794813021649539',  '649.43']]

Is it possible? I tried and I couldn’t.

auto MatrizString() {
    string m = NULL;
    int i, j;

    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            m[i][j] = "Numes";
        }
    }
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            printf("%s", m[i][j]);
        }
        printf("\n");
    }

    return 0;
}
  • And where is the code you tried and which error was reported? Should you not use { and } in place of [ and ]?

  • I added the code. The error reported is: "the expression must have pointer-to-object type".

  • But m is a string or an array of strings?

  • An array of strings. Is it right for me to put m[]? And how, from that, I could create an array?

  • @Brunacastro you declared a string not a string array the right one would be string m[4][4]; , in the second row for a 4x4 Matrix.

  • @user72726, I replaced it and it worked. However, the print gets strange characters: ·┘8·┘T·┘p·┘&#xA;î·┘¿·┘─·┘Ó·┘&#xA;³·┘¹┘4¹┘P¹┘&#xA;l¹┘ê¹┘ñ¹┘└¹┘

  • I must import some library?

  • Since you are programming in C++, consider using cout instead of printf. But if you really want to use printf, you should first convert the string from C++ to C before printing. Ex: printf("%s", m[i][j].c_str())

  • It gave right, vlw to have helped @user72726!

Show 4 more comments

2 answers

1

  • I think you know there’s only three lines here
[['3325309756482910474',  'CARRO',  '2506794813021649539', '618.57'],
 ['3325309756485249504',  'MOTO',  2506794813021649539',   '649.32'],
 ['3328687456208678517',  'BIKE',  '2506794813021649539',  '649.43']]

and 4 in the post and that is a function in C, despite having used the tag C++

In C++ use a class. It’s simpler.

auto

auto in C has another meaning. It is not a type definition. In C++ auto does not apply to that context, as the compiler must have said.

In C++ you can write

    string m[4][4] =
    {
        { "3325309756482910474", "CARRO", "2506794813021649539", "618.57"} ,
        {"3325309756485249504",  "MOTO",  "2506794813021649539", "649.32" },
        { "3328687456208678517", "BIKE",  "2506794813021649539", "649.43" }
    };

If you prefer to declare "Matrix" you can even write

using Matriz = string[4][4];

As in this example

#include <iostream>
using namespace std;
using Matriz = string[4][4];

int main(void)
{
    string m[4][4] =
    {
        { "3325309756482910474", "CARRO", "2506794813021649539", "618.57"} ,
        {"3325309756485249504", "MOTO",   "2506794813021649539", "649.32" },
        { "3328687456208678517", "BIKE",  "X506794813021649539", "649.43" }
    };
    Matriz uma{};
    uma[3][3] = m[2][2];
    cout << "M[3][3] = \"" << uma[3][3] << "\"\n";
    return 0;
}

That shows the expected

M[3][3] = "X506794813021649539"

Use a class and write the methods as you need them. Or even the operators for Matrix.

0


The vector class can make your life easier:

  vector<vector<string>> matriz({
    { "3325309756482910474", "CARRO", "2506794813021649539", "618.57"} ,
    {"3325309756485249504",  "MOTO",  "2506794813021649539", "649.32" },
    { "3328687456208678517", "BIKE",  "2506794813021649539", "649.43" }
  });

It has methods that allows you to insert/remove elements without having to directly manipulate the memory. If you want to add a new element at the end for example:

 matriz.push_back({"a", "b", "c", "d"});

Not to mention that if you want some row to contain a different amount of elements, just add more or less:

 matriz.push_back({"linha de elemento unico"});

Check the documentation to check the other possibilities.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.