选择所有元素,但一个带有std :: vector的给定索引的元素?

发布于 2025-02-10 06:52:20 字数 181 浏览 0 评论 0原文

给定一个std :: vector,例如ints

std :: vector vec {10,20,30}

如何选择除给定索引以外的所有项目,例如int i = 1结果

std :: vector {10,30}

Given a std::vector, for example of ints

std::vector vec{10, 20, 30}

how to select all items except of with given index, for example int i=1 resulting

std::vector {10, 30}?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2025-02-17 06:52:20

如果您只想从原始向量“选择”值,我将创建另一个具有所有新值的向量。

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> vect{ 10, 20, 30 };

    vector<int> selected;

    int i = 1;

    for (int j = 0; j < vect.size(); j++) {
        if (j != i) {
            selected.push_back(vect[j]);
        }
    }

    // Added so you can check the new values
    for (int z = 0; z < selected.size(); z++) {
        cout << selected[z] << " ";
    }

    return 0;
}

但是,如果您想从原始矢量中删除值,我建议使用vector.erase方法(研究文档)。

If you just want to "select" values from the original vector, I would create another vector with all the new values.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> vect{ 10, 20, 30 };

    vector<int> selected;

    int i = 1;

    for (int j = 0; j < vect.size(); j++) {
        if (j != i) {
            selected.push_back(vect[j]);
        }
    }

    // Added so you can check the new values
    for (int z = 0; z < selected.size(); z++) {
        cout << selected[z] << " ";
    }

    return 0;
}

However, if you want to erase values from your original vector I would recommend using the vector.erase method (research the documentation).

纵情客 2025-02-17 06:52:20

这是您可以通过向量和索引的功能,它将在没有该索引元素的情况下返回新向量。

#include <iostream>
#include <vector>
using namespace std;


// returns a new vector without the v[index]

 vector<int> getElementsExceptIndex(vector<int> v, int index){
    vector<int> newVector;
    for(auto &x:v ){
     if(( &x - &v[0]) != index)
      newVector.push_back(x);
   }
    return newVector;
}

int main() {
   vector<int> originalVector{ 10, 20, 30 ,33,53};
   int index=1;
   
   auto RemovedIndexVector = getElementsExceptIndex(originalVector,index);
   
   for(auto item:RemovedIndexVector)
     cout<<item<<" ";
    return 0;
}
// Output - 10 30 33 53

希望这会有所帮助

Here is a function for it where you can pass the vector and index and it will return you the new vector without that index element.

#include <iostream>
#include <vector>
using namespace std;


// returns a new vector without the v[index]

 vector<int> getElementsExceptIndex(vector<int> v, int index){
    vector<int> newVector;
    for(auto &x:v ){
     if(( &x - &v[0]) != index)
      newVector.push_back(x);
   }
    return newVector;
}

int main() {
   vector<int> originalVector{ 10, 20, 30 ,33,53};
   int index=1;
   
   auto RemovedIndexVector = getElementsExceptIndex(originalVector,index);
   
   for(auto item:RemovedIndexVector)
     cout<<item<<" ";
    return 0;
}
// Output - 10 30 33 53

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文