如何在队列中存储数组
How to store arrays in queues?
我正在编写一段代码,我想出了一个问题,这是如何在队列中插入数组?让我举个例子来解释你。
array1[9]={1,3,4,2,1,2,3,4,1}
array2[9]={2,3,4,2,4,2,2,2,3}
//create a queue (I don't know how to declare a queue for this issue)...
queue.push(array1);
queue.push(array2);
//##############################
//when I print queue.front()...
//########################
printf("%d",queue.front());
上面的代码片段应该打印 array1 的每个元素!!怎么做??
如果使用 std::array
而不是 C 样式数组,这很容易。
std::array<int, 9> array1 = { 1, 3, 4, 2, 1, 2, 3, 4, 1 };
std::array<int, 9> array2 = { 2, 3, 4, 2, 4, 2, 2, 2, 3 };
std::queue<std::array<int, 9>> queue;
queue.push(array1);
queue.push(array2);
for (auto i : queue.front()) {
std::cout << i;
}
对于这个问题来说,这个答案太简单了。 如果您需要动态数组大小,只需在您最喜欢的搜索引擎中准确搜索即可:-)
int array1[9]={1,3,4,2,1,2,3,4,1};
int array2[9]={2,3,4,2,4,2,2,2,3};
std::vector<int> a1(9);
std::vector<int> a2(9);
for(int i =0; i< 9;i++){
a1[i]=array1[i];
a2[i]=array2[i];// <= a2[i] should take values from array2[i]
}
std::queue<std::vector<int>> queue;
queue.push(a1);
queue.push(a2);
//std::cout<<queue.size()<<std::endl;
std::vector<int> temp(9);
int size =queue.size();
for(int i =0; i< size;i++){
temp=queue.front();
std::cout<<"Array["<<i<<"]: ";
for(int j =0; j< temp.size();j++)
std::cout<<temp[j]<<",";
std::cout<<std::endl;
queue.pop();
}
相关文章:
- 使用无符号字符数组有效存储内存
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 在c++中获取两个大int,并将它们存储在数组中
- 使用 Xcode 将双精度存储在数组C++中
- 访问表示 2D 矩阵的 1D 存储数组
- 从输入文件读取整数,仅存储数组C 中的唯一整数
- 如何在队列中存储数组
- 按列主要顺序迭代行大量存储数组
- 如何使存储数组的二进制搜索稳定
- 将行中的字段从textfile中更改为存储数组,而不是重新编写文件
- 存储数组索引值C++
- 存储数组的地方,如果我在返回后声明它
- 缺少存储数组地址的内存
- 使用static在函数中存储数组各个组件的值
- 如何存储数组的每个相邻子数组的和
- 在c++中存储数组映射
- 存储数组的地址列表,以解析原始的非终止文本
- 用于存储数组的字符串
- c++存储数组下一个位置的地址
- 在 for 循环中创建和存储数组