简体   繁体   中英

Lists to 2D arrays and iterators c++

I would like to take the contents of a std list and pass them through a 2x10 array via iterator, it should start from element [2][10]

@Suthiro was kind enough to provide this code that works for the elements moving forward from element [0][0].

auto jj = 1u;
while (!condition)
{
intarray[0][0] = *it;
++it;
for (auto ii=jj; ii >= 1 ; --ii)
    intarray[0][ii] = intarray[0][ii-1];
++jj;
}

So I have tried every variation of change possible

I think the closest I have got is this piece here.

FileList = List;
auto jj = 1;

it = begin(FileList);
std::advance(it, 10);

for (auto& i : FileList) {
    FileArray[1][9] = *it;
    ++it;
    for (auto ii = 19; ii>=1; --ii)
    {
        FileArray[0][ii - 1] = FileArray[0][ii];
        ++jj;
 }

I have stepped through with breakpoints to try and figure it out but after a lot of hours I am reaching out for a bit of help.

OK, from what I understand, you want to assign values from a std::list to an 2 dimensional array. I hopy that you mean a std::array and not a C-Style array that should not be used in C++.

But, I fear you mean a C-Style array.

Anyway, I will show you 3 solutions

  • Using a C++ std::array and do everything with iterators
  • Using a C-Style array and use the index operator
  • Using a C-Style array and use pointers, similar to iterators

We will filled the arrays from back to the end.

With C++ iterator this is extremly simple, becuase we can use so called reverse_iterator . If you initialize this with rbegin() then it will point to the last element. Incrementing it (++) will actually go to the previous element.

So, a very elegant solution.

Please see:

#include <iostream>
#include <list>
#include <array>
#include <numeric>

// The dimensions and size for our array
constexpr size_t NumberOfRows = 2U;
constexpr size_t NumberOfColumns = 10U;

int main() {

    // This is our std::list. 
    std::list<int> myList;

    // Fill the list with some demo values
    myList.resize(NumberOfRows * NumberOfColumns);
    std::iota(myList.begin(), myList.end(), 0);

    // -------------------------------------------------------------
    // This is the solution for a C++ std::list and a C++ std::array

    // Get an iterator to the begin of the list
    std::list<int>::iterator listIterator = myList.begin();

    // This is a 2 dimensional std::array with 2 rows and 10 columns
    std::array<std::array<int, NumberOfColumns>, NumberOfRows> myArray;

    // This is an iterator with which we can iterate over the rows in our target array. 
    // Since we ant to iterate from end to begin, we use an reverse iterator
    std::array<std::array<int, NumberOfColumns>, NumberOfRows>::reverse_iterator arrayRowIterator;

    // Iterate over the rows beginning from rbegin(), so, from the end
    for (arrayRowIterator = myArray.rbegin(); arrayRowIterator != myArray.rend(); ++arrayRowIterator) {

        // This is an iterator with which we can iterate over the columns in our target array. 
        // Since we want to iterate from end to begin, we use an reverse iterator
        std::array<int, NumberOfColumns>::reverse_iterator arrayColumnIterator;

        // Iterate over the columns beginning from rbegin(), so, from the end
        for (arrayColumnIterator = arrayRowIterator->rbegin(); arrayColumnIterator != arrayRowIterator->rend(); ++arrayColumnIterator) {
            // Assign the value
            *arrayColumnIterator = *listIterator;

            // Point to next value in the list
            ++listIterator;
        }
    }

    // Show debug output 
    for (size_t row = 0; row < NumberOfRows; ++row) {
        for (size_t column = 0; column < NumberOfColumns; ++column) {
            std::cout << myArray[row][column] << " ";
        }
        std::cout << "\n";
    }

    // --------------------------------------------------------------------------------------
    // If you use a C-Style array, what I fear then you can use the following simple approach

    // Define 2 dimensional C-Style array
    int myCStyleArray[NumberOfRows][NumberOfColumns];

    // Reset the iterator back to the list begin
    listIterator = myList.begin();

    // Iterate ove the C-Style array from end to begin
    for (int row = NumberOfRows - 1; row >= 0; --row) {
        for (int column = NumberOfColumns - 1; column >= 0; --column) {
            myCStyleArray[row][column] = *listIterator;
            ++listIterator;
        }
    }

    // Show debug output 
    for (size_t row = 0; row < NumberOfRows; ++row) {
        for (size_t column = 0; column < NumberOfColumns; ++column) {
            std::cout << myCStyleArray[row][column] << " ";
        }
        std::cout << "\n";
    }

    // -----------------------------------------------------------------------------------
    // This is the solution for a C++ std::list C-Style array, using pointers as iterators
    // If you check carefully, then you will se the similarity with pointers

    // Define 2 dimensional C-Style array
    int myCStyleArray2[NumberOfRows][NumberOfColumns];

    // Reset the iterator back to the list begin
    listIterator = myList.begin();

    // Define a pointer to an int[10]. Please note: In C++ you cannot use an int**
    int(*cStyleArrayRowIterator)[NumberOfColumns];

    // Iterate over the rows
    for (cStyleArrayRowIterator = myCStyleArray2 + NumberOfRows - 1; cStyleArrayRowIterator >= myCStyleArray2; --cStyleArrayRowIterator) {

        int* cStyleArrayColumnIterator;
        // Iterator over the columns
        for (cStyleArrayColumnIterator = *cStyleArrayRowIterator + NumberOfColumns - 1; cStyleArrayColumnIterator >= *cStyleArrayRowIterator; --cStyleArrayColumnIterator) {
            // Assign the values
            *cStyleArrayColumnIterator = *listIterator;
            ++listIterator;
        }
    }
    // Show debug output 
    for (size_t row = 0; row < NumberOfRows; ++row) {
        for (size_t column = 0; column < NumberOfColumns; ++column) {
            std::cout << myCStyleArray[row][column] << " ";
        }
        std::cout << "\n";
    }
    return 0;
}

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