如何擦除前 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>>>?

本文关键字:lt gt vector int 3d 例如 pair 擦除 何擦除 最后 一列      更新时间:2023-10-16

如何擦除前 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