C++ 检查 std::vector 中是否存在给定索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17700287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:21:50  来源:igfitidea点击:

Check if a given index exists in std::vector

c++vectoriteratorstd

提问by leon22

I need index access to my std::vector, and therefore I must check if an index is already available to first delete them, and then set a new value.

我需要对我的 进行索引访问std::vector,因此我必须检查索引是否已经可用以首先删除它们,然后设置一个新值。

Here's my setter function:

这是我的 setter 函数:

void SetVector(int position, int value) {
    std::vector<int>iterator it = testVector.begin();
    // need a check here
    testVector.insert(it-testVector.begin()+position, value);
}

Or is this the wrong C++ collection for my needs? (should grow dynamically, so no std:arraypossible). Could use a std::mapbut maybe it's also possible with std::vector.

或者这是我需要的错误 C++ 集合?(应该动态增长,所以std:array不可能)。可以使用 astd::map但也许也可以使用std::vector.

回答by Mike Seymour

The requirements aren't entirely clear from the question, but I'm assuming that you want to end up with testVector[position] == value, whether or not positionwas in range to begin with.

问题中的要求并不完全清楚,但我假设您希望以 结束testVector[position] == value,无论是否position在开始的范围内。

First grow the vector if it's too small. This will insert zero-values after whatever is already there.

如果向量太小,首先增加向量。这将在已经存在的任何内容之后插入零值。

if (position >= testVector.size()) {
    testVector.resize(position+1);
}

Then assign the element you want to set:

然后分配要设置的元素:

testVector[position] = value;

回答by doctorlove

I don't believe the question is clear. If you want

我不相信这个问题是清楚的。如果你想

"to first delete them, and then set a new value."

“先删除它们,然后再设置一个新值。”

this might work

这可能有效

void SetVector(int position, int value) {
    if (position < testVector.size()) {
        testVector[position] = value;
    }
    else {
        testVector.push_back(value);
    }
}

You should really make the int positionthe testVector's size_type.

你真的应该int positiontestVector's size_type

回答by Alexis

You can use std::vector::atwho throw an exception if you don't have anything at this index.

如果您在此索引上没有任何内容,您可以使用std::vector::at抛出异常。

The function automatically checks whether n is within the bounds of valid elements in the vector, throwing an out_of_range exception if it is not (i.e., if n is greater or equal than its size). This is in contrast with member operator[], that does not check against bounds.

该函数会自动检查 n 是否在向量中有效元素的范围内,如果不在则抛出 out_of_range 异常(即,如果 n 大于或等于其大小)。这与不检查边界的成员 operator[] 形成对比。

And since you get a reference on the object at the given index, you can change/delete the value

并且由于您在给定索引处获得了对对象的引用,因此您可以更改/删除该值

void SetVector(int position, int value) {
   try
    {
       testVector.at(position) = value;
    }
   catch (const std::out_of_range& oor) {
      testVector.resize(position + 1);
      testVector[position] = value;
   }
}

回答by Sanyam Goel

first get an iterator for your vector by using

首先通过使用为您的向量获取迭代器

 std::vector<int>::iterator it;

it = myvector.begin();

it = myvector.begin();

for (it=myvector.begin(); it<myvector.end(); it++)
    std::cout << ' ' << *it;

Using thsi iterator you can traverse all elements and perform respective operation like remove element

使用 thsi 迭代器,您可以遍历所有元素并执行相应的操作,例如删除元素