Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

how to find index of a vector in c++

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	vector<int> v = { 7, 3, 6, 2, 6 };
	int key = 6;
	vector<int>::iterator itr = find(v.begin(), v.end(), key);
	int index = distance(v.begin(), itr);
	cout << index << endl;

	return 0;
}
 
PREVIOUS NEXT
Tagged: #find #index #vector
ADD COMMENT
Topic
Name
5+5 =