C++ 检查数组索引是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/256807/
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 14:05:06  来源:igfitidea点击:

Check if array index exists

c++arrays

提问by AntonioCS

Is there any way to check if a given index of an array exists? I am trying to set numerical index but something like 1, 5, 6,10. And so I want to see if these indexes already exist and if they do just increase another counter.

有没有办法检查数组的给定索引是否存在?我正在尝试设置数字索引,但类似于 1、5、6、10。所以我想看看这些索引是否已经存在,以及它们是否只是增加了另一个计数器。

I normally work with php but I am trying to do this in c++, so basically I am trying to ask if there is an isset() way to use with c++

我通常使用 php,但我试图用 c++ 来做这件事,所以基本上我想问问是否有一种 isset() 方法可以与 c++ 一起使用

PS: Would this be easier with vectors? If so, can anyone point me to a good vector tutorial? Thanks

PS:使用矢量会更容易吗?如果是这样,任何人都可以给我指出一个好的矢量教程吗?谢谢

回答by user23167

In C++, the size of an array is fixed when it is declared, and while you can access off the end of the declared array size, this is very dangerous and the source of hard-to-track-down bugs:

在 C++ 中,数组的大小在声明时是固定的,虽然您可以在声明的数组大小的末尾访问,但这是非常危险的,并且是难以追踪的错误来源:

int i[10];
i[10] = 2; // Legal but very dangerous! Writing on memory you don't know about

It seems that you want array-like behavior, but without all elements being filled. Traditionally, this is in the realms of hash-tables. Vectors are not such a good solution here as you will have empty elements taking up space, much better is something like a map, where you can test if an element exists by searching for it and interpreting the result:

似乎您想要类似数组的行为,但没有填充所有元素。传统上,这是在哈希表领域。矢量在这里不是一个很好的解决方案,因为您将有空元素占用空间,更好的是像地图这样的东西,您可以在其中通过搜索元素并解释结果来测试元素是否存在:

#include <map>
#include <string>

// Declare the map - integer keys, string values    
std::map<int, std::string> a;

// Add an item at an arbitrary location
a[2] = std::string("A string");

// Find a key that isn't present
if(a.find(1) == a.end())
{
   // This code will be run in this example
   std::cout << "Not found" << std::endl;
}
else
{
   std::cout << "Found" << std::endl;
}

One word of warning: Use the above method to find if a key exists, rather than something like testing for a default value

警告一句话:使用上面的方法来查找键是否存在,而不是像测试默认值那样

if(a[2] == 0)
{
    a[2] = myValueToPutIn;
}

as the behavior of a map is to insert a default constructed object on the first access of that key value, if nothing is currently present.

因为映射的行为是在第一次访问该键值时插入一个默认构造的对象,如果当前没有任何东西存在。

回答by mdec

My personal vote is for using a vector. They will resize dynamically, and as long as you don't do something stupid (like try and access an element that doesn't exist) they are quite friendly to use.

我个人投票赞成使用矢量。它们会动态调整大小,只要您不做一些愚蠢的事情(例如尝试访问不存在的元素),它们就非常易于使用。

As for tutorials the best thing I could point you towards is a google search

至于教程,我可以向您指出的最好的事情是谷歌搜索

回答by Steve

It sounds to me as though really a map is closest to what you want. You can use the Map class in the STL (standard template library)(http://www.cppreference.com/wiki/stl/map/start).

在我看来,好像真的地图最接近你想要的。您可以使用 STL(标准模板库)( http://www.cppreference.com/wiki/stl/map/start) 中的 Map 类。

Maps provide a container for objects which can be referenced by a key (your "index").

地图为可以通过键(您的“索引”)引用的对象提供了一个容器。

回答by MukeshD

To do this without vectors, you can simply cross-check the index you are tying to access with the size of array. Like: if(index < array_size)it is invalid index.

要在没有向量的情况下做到这一点,您可以简单地交叉检查您要访问的索引与数组的大小。像:if(index < array_size)它是无效的索引。

In case the size is not known to you, you can find it using the sizeofoperator.

如果您不知道尺寸,您可以使用sizeof运算符找到它。

For example:

例如:

int arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3};
int arr_size = sizeof(arr)/sizeof(arr[0]);