Search
 
SCRIPT & CODE EXAMPLE
 

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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: hello world in cpp 
Cpp :: 2114. Maximum Number of Words Found in Sentences leetcode solution in c++ 
Cpp :: c++ print hello world 
Cpp :: c++ fill array with 0 
Cpp :: struct and return functions in c++ 
Cpp :: c++ ros subscriber 
Cpp :: chess perft 5 
Cpp :: repeat character n times c++ 
Cpp :: fill two dimension array c++ 
Cpp :: qimage transformed 
Cpp :: C++ Area of Scalene Triangle 
Cpp :: c++ index of nth occurence 
Cpp :: C++ sqlite open file in other directory 
Cpp :: draw rect outline sdl2 
Cpp :: certificate exe application 
Cpp :: how to define an unsigned signal in VHDL 
Cpp :: c++ compare strings ignore case 
Cpp :: qstring get if empty 
Cpp :: vector of structs c++ 
Cpp :: user defined key for map in c++ 
Cpp :: find character in string c++ 
Cpp :: comment in c++ 
Cpp :: swap values in array c++ 
Cpp :: all of the stars lyrics 
Cpp :: c++ mst kruskal 
Cpp :: C++ convert integer to digits, as vector 
Cpp :: round double to n decimal places c++ 
Cpp :: c++ open all files in directory 
Cpp :: cpp unions 
Cpp :: sort vector using marge sorting in c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =