0

So I have been looking at the following post for converting a vector into an array but this method does not seem to be converting for my use case.

How to convert vector to array

vector<array<int, 256>> table; // is my table that I want to convert

// There is then code in the middle that will fill it

int** convert = &table[0][0] // is the first method that I attempted

convert = table.data(); // is the other method to convert that doesn't work

I belive my understanding of the back end of the data type is where my knowledge falls short. Any help on this would be appreciated

edit: I have changed form C style array's to C++ arrays

  • `vector` looks weird already, it probably should be `std::vector>` – πάντα ῥεῖ Feb 27 '19 at 23:59
  • 1
    I don't think you can use `vector` in practice in the first place. How do you insert elements into it? – eerorika Feb 28 '19 at 00:03
  • 1
    An array decays to a pointer (`int *` in this case). An array of arrays decays to a pointer to an array (`int *[SIZE]`), not a double pointer. Taking the address of `table[0][0]` provides a pointer to that one element, an `int *`. Making a 2D array out of `int **` takes a bit of work and is often the least efficient way to do the job. There's probably a good formal answer embedded in this comment if I try hard enough to eke it out. – user4581301 Feb 28 '19 at 00:04
  • As far as it stands eerorika and πάντα ῥεῖ I have had no issues in regards to using the C style array but I am now looking at redoing this as a CPP array to see if this fixes my issue – yeroc-sebrof Feb 28 '19 at 00:08
  • @yeroc_sebrof Well, the major problem I see is that neither `vector` nor `std::vector>` would store the data in a single, consecutive chunk of memory, thus a simple cast can't be applied. – πάντα ῥεῖ Feb 28 '19 at 00:11
  • @πάνταῥεῖ I woudl expect that to be incorrect given the answer to the question I linked to. I will look into the answer Anroca provided first before looking into other methods – yeroc-sebrof Feb 28 '19 at 00:20
  • @yeroc_sebrof No, what I said isn't _incorrect_. Using a simple cast is something fundamentally different as doing a copy. – πάντα ῥεῖ Feb 28 '19 at 00:23
  • @eerorika by pushing an pre-initalised row I have stored. `int newRow[256] = { 0 };` – yeroc-sebrof Feb 28 '19 at 00:36
  • @yeroc_sebrof I can't get that working: http://coliru.stacked-crooked.com/a/e2499f9b3ed7216e Can you show me a minimal demo of how to use it? – eerorika Feb 28 '19 at 00:49
  • 1
    @yeroc_sebrof The native pointer arithmetic coming with a `int**` would calculate offsets based on a consecutive memory block that can be addressed with the simple formula _`addr = base_addr + (row_index * row_size) + col_index` – πάντα ῥεῖ Feb 28 '19 at 00:54
  • @eerorika, it seems I had too much trust in IntelliSense. It seems that this method does not work. Thank you for bringing this to my attention – yeroc-sebrof Feb 28 '19 at 00:58

2 Answers2

0

Assuming use of C++ 11, include <algorithm>

you could probably use std::copy.

I've not tested it but believe you could just do:

std::copy(&table[0][0], &table[0][0]+256*table.size(), &myArray[0][0]);

where the parameters are effectively:

std::copy(<source obj begin>, <source obj end>, <dest obj begin>);

More on that here: https://en.cppreference.com/w/cpp/algorithm/copy

OffBy0x01
  • 178
  • 1
  • 9
0

While there is going to be a route that should work via casting, the easiest thing I can guarantee will work is to make an array of pointers to ints that contains pointers to the arrays in the source vector.

// make vector of pointers to int
std::vector<int*> table2(table.size());

// fill pointer vector pointers to arrays in array vector
for (int i = 0; i < size; i++ )
{
    table2[i] = table[i];
}

Example:

#include <vector>
#include <iostream>
#include <iomanip>
#include <memory>

constexpr int size = 4;

// test by printing out 
void func(int ** arr)
{
    for (int i = 0; i < size; i++ )
    {
        for (int j = 0; j < size; j++ )
        {
            std::cout << std::setw(5) << arr[i][j] <<  ' ';
        }
        std::cout << '\n';
    }
}

int main()
{
    std::vector<int[size]> table(size);

    // fill values
    for (int i = 0; i < size; i++ )
    {
        for (int j = 0; j < size; j++ )
        {
            table[i][j] = i*size +j;
        }
    }

    // build int **
    std::vector<int*> table2(table.size());
    for (size_t i = 0; i < size; i++ )
    {
        table2[i] = table[i];
    }

    //call function
    func(table2.data());

}

Looks like you're stuck doing this he thar way because of the requirement for a int **, when possible try to use a simple matrix class instead.

user4581301
  • 33,082
  • 7
  • 33
  • 54