在C++中返回向量中元素的第一个索引
我想在C++中找到一个向量中某个元素的第一个位置。
假设你有一个向量:[2, 3, 4, 2, 6, 7, 1, 2, 6, 3]。
我想找到数字6的位置。
数字6第一次出现是在索引值为4的地方。
C++中有没有什么函数可以做到这一点呢?
我知道在Python中,我可以用list.index(n)这个方法来帮我找到。
7 个回答
为了进一步解释Geoffrey Tucker的回答,其实你可以把它变成一个模板函数,像这样:
#include <algorithm>
#include <iterator>
#include <vector>
template <typename T=int, class ContainerType=std::vector<T> >
typename std::iterator_traits<typename ContainerType::iterator>::difference_type
get_index_of(const ContainerType& c, const T& t) {
ContainerType::const_iterator itr = std::find(c.begin(), c.end(), t);
return (std::distance(c.begin(), itr));
}
需要注意的是,如果你查找的项目不在容器里,返回的索引实际上会比c.size()
的值还要大,其中c
就是容器(在你的例子中是一个向量)。这和Geoffrey的实现不同,他返回的是-1;而在这里,我们让容器类型自己决定这个函数的返回值是什么。
你需要做类似这样的事情:
int getIndexOf(std::vector<int> v, int num)
{
for(std::vector<int>::size_type i = 0; i != v.size(); i++)
{
if(v[i] == num)
{
return i;
}
}
return -1;
}
编辑: 由于效率确实很重要,也许这个方法是可行的。我把每个项目在列表中的位置存储到它对应的哈希值里,使用的是一个无序多重映射。注意:这是在假设列表的内容不会频繁变化的情况下。
#include <unordered_map>
#include <algorithm>
typedef std::unordered_multimap<int,int>::const_iterator IntMapIterator;
typedef std::pair<int,int> IntPair;
std::unordered_multimap<int,int> hashValues(const std::vector<int>& vec)
{
std::unordered_multimap<int,int> hashedValues;
for(std::vector<int>::size_type i = 0; i != vec.size(); i++)
{
hashedValues.emplace(vec[i], i);
}
return hashedValues;
}
struct IntPairComparator
{
bool operator()(const IntPair& left, const IntPair& right) const
{
return left.second < right.second;
}
};
int getEarliestIndex(const std::unordered_multimap<int,int>& hashedValues, int num)
{
std::pair<IntMapIterator,IntMapIterator> range = hashedValues.equal_range(num);
IntPair minPair = *std::min_element(range.first, range.second, IntPairComparator());
return minPair.second;
}
int main(int argc, const char* argv[])
{
std::vector<int> bigVector;
// do stuff and fill contents of vector
std::unordered_multimap<int,int>& hashedValues = hashValues(bigVector);
int earliestIndex = getEarliestIndex(hashedValues, 6);
}
你可以使用:
InputIterator find (InputIterator beg, InputIterator end, const T& value)
这个是在 #include <algorithm>
中定义的。
用法
假设你有以下这个向量:
std::vector<int> numberVector;
numberVector.push_back(1);
numberVector.push_back(2);
numberVector.push_back(3);
numberVector.push_back(4);
numberVector.push_back(5);
你可以通过以下方式找到 4
的索引:
std::vector<int>::iterator position = std::find(
numberVector.begin(), numberVector.end(), 4
);
然后检查它是否被找到:
bool exists = (position != numberVector.end());
如果存在,那么你可以通过以下方式获取索引:
int index = position - numbVector.begin();
当然可以!请看下面的内容:
这段代码是用来处理一些数据的。它的主要目的是从一个地方获取信息,然后对这些信息进行一些操作,最后把结果输出到另一个地方。
首先,代码会连接到一个数据库,数据库就像一个存储信息的地方。接着,它会查询这个数据库,找出我们需要的数据。查询就像是在问数据库:“嘿,你能告诉我关于这个特定信息的所有内容吗?”
一旦拿到数据,代码会对这些数据进行处理。处理的过程可能包括筛选、排序或者计算一些值。想象一下,你在整理一堆文件,挑出你需要的部分,然后把它们按顺序放好。
最后,处理完的数据会被输出到一个文件或者显示在屏幕上。输出就像是把整理好的文件放到一个文件夹里,或者把它们展示给别人看。
总的来说,这段代码的工作流程就是:连接数据库 -> 查询数据 -> 处理数据 -> 输出结果。希望这个解释能帮助你理解这段代码的基本功能!
std::vector<int> vct;
//insert value
//use std::find to get iterator
auto itr=std::find(vct.begin(), vct.end(), 6);
if(itr==vct.end())
return;
auto index=std::distance(vct.begin(), itr);