如何擦除前 7 列加上最后一列,第 9 列,仅将第 8 列留在 3d 矢量中,例如 vector<vector<pair<int,int>>>?
How do I erase the first 7 columns plus the last, the 9th, only leaving the 8th column in a 3d vector like vector<vector<pair<int,int>>>?
如何擦除前 7 列加上最后 9 列,只将第 8 列留在 3D 矢量中,如 vector<vector<pair<int,int>>>
?
这工作正常:
for (vector<vector<pair<int,int> > >::iterator it = allPathCoordinates.begin();
it != allPathCoordinates.end();
it += 1)
{
v_temp = *it;
for(vector<pair<int,int> >::iterator it2 = v_temp.begin();
it2 != v_temp.end();
++it2)
{
apair = *it2;
openPoints[apair.first][apair.second] = 0;
closedPoints[apair.first][apair.second] = 1;
allObstacles[apair.first][apair.second] = Wall;
point[apair.first][apair.second] = Purple;
}
}
cout << "allPathCoordinates.size():"
<< allPathCoordinates.size() << endl; // 79512
cout << "sizeof(allPathCoordinates):"
<< sizeof(allPathCoordinates) << endl; //24
vector< vector<pair<int,int> > >::size_type sz;
sz = allPathCoordinates.capacity();
cout << "capacity: " << sz << 'n';
allPathCoordinates.erase(allPathCoordinates.begin(),
allPathCoordinates.begin()+7);
cout << "allPathCoordinates.size():"
<< allPathCoordinates.size() << endl;
cout << "sizeof(allPathCoordinates):"
<< sizeof(allPathCoordinates) << endl;
sz=allPathCoordinates.capacity();
cout<< "capacity: " << sz << 'n';
输出:
所有路径坐标大小():79512大小(所有路径坐标):24容量:131072allPathCoordinates.size():79505大小(所有路径坐标):24容量:131072
可以看出,我只减少了元素的数量7.不是我想要的。我想减少列数,总共 8 列中有 9 列。你好头脑里奥特和鲍勃感谢您关于"仅复制该列"的回答。问题是IO已经尝试过使用memcpy,分配,copy_n和复制,但在过去的两周里无济于事。如果您有想法,请使用我上面的代码向我展示如何操作。
对于你"头脑风暴",我想这样描述我的问题:"查看所有路径坐标(矢量>>所有路径坐标;)作为具有 9 列的动态 3D 数组,具有不同数量的整数对的未知行数。我想保持的是第 8 列完好无损,其未知数量的整数对作为第 3 维。这应该理解为好像我主要对元素不感兴趣,即一对中的第一个或第二个整数值。当提取第 8 列(使用 memcpy、赋值、copy_n、复制或其他更好的方式)时,我将迭代它以获取整数对(坐标),以便在 point() 函数中进一步使用,例如 point[first][second]=Yellow;感谢您的关注。博
如果我理解你的问题,你需要的是在erase
之后打电话给std::vector::shrink_to_fit
。
我们也可能不同意什么是vector
的"列"。让我用一个最小的程序来解释:
#include <iostream>
#include <vector>
#include <iomanip>
#include <utility>
using std::cout;
using std::vector;
using std::pair;
using thing = vector<vector<pair<int,int>>>;
void print ( thing &a ) {
for ( auto && row : a ) {
for ( auto && col : row ) {
cout << std::setw(6) << col.first << ','
<< std::setw(3) << col.second;
}
cout << 'n';
}
cout << "nouter vector size: " << a.size()
<< " capacity: " << a.capacity() << 'n';
cout << "inner vector size: " << a[0].size()
<< " capacity: " << a[0].capacity() << "nn";
}
int main() {
thing a = {
{ {1, 2}, {3, 4}, {5, 6} }, // <- that's row 0: a[0]
{ {7, 9}, {10, 11}, {12, 13} },
{ {14, 15}, {16, 17}, {18, 19} },
{ {20, 21}, {22, 23}, {24, 25} }
}; // ^^^^^^
// This is a column, all the pairs a[i][0] for 0 <= i < N
print(a);
// erase the second row
a.erase(a.begin() + 1);
a.shrink_to_fit();
print(a);
// erase the second column
for ( auto && row : a ) {
row.erase(row.begin() + 1);
row.shrink_to_fit();
}
print(a);
return 0;
}
输出为:
1, 2 3, 4 5, 6
7, 9 10, 11 12, 13
14, 15 16, 17 18, 19
20, 21 22, 23 24, 25
outer vector size: 4 capacity: 4
inner vector size: 3 capacity: 3
1, 2 3, 4 5, 6
14, 15 16, 17 18, 19
20, 21 22, 23 24, 25
outer vector size: 3 capacity: 3
inner vector size: 3 capacity: 3
1, 2 5, 6
14, 15 18, 19
20, 21 24, 25
outer vector size: 3 capacity: 3
inner vector size: 2 capacity: 2
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中