[英]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]我想获取 std 列表的内容并通过迭代器将它们传递给 2x10 数组,它应该从元素 [2][10] 开始
@Suthiro was kind enough to provide this code that works for the elements moving forward from element [0][0]. @Suthiro 非常友好地提供了适用于从元素 [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.好的,据我了解,您想将
std::list
中的值分配给二维数组。 I hopy that you mean a std::array
and not a C-Style array that should not be used in C++.我希望您的意思是
std::array
而不是不应在 C++ 中使用的 C 样式数组。
But, I fear you mean a C-Style array.但是,我担心您的意思是 C 样式数组。
Anyway, I will show you 3 solutions无论如何,我将向您展示 3 个解决方案
std::array
and do everything with iteratorsstd::array
并使用迭代器做所有事情We will filled the arrays from back to the end.我们将从后到尾填写 arrays。
With C++ iterator this is extremly simple, becuase we can use so called reverse_iterator
.使用 C++ 迭代器,这非常简单,因为我们可以使用所谓的
reverse_iterator
。 If you initialize this with rbegin()
then it will point to the last element.如果你用
rbegin()
初始化它,那么它将指向最后一个元素。 Incrementing it (++) will actually go to the previous element.将其递增 (++) 实际上会将 go 增加到前一个元素。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.