Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ find index of an element

#include <iostream>
#include <vector> 
#include <iterator> 
#include <algorithm>  
using namespace std;

int main()
{
    vector<int> v = {1,2,3,4,6,4,5};
    // Get index of element from iterator
    int index = distance(v.begin(), find(v.begin(), v.end(), 4));
    cout << index;
}
Comment

get index of value c++

vector<int> arr = { 6, 3, 5, 2, 8 };
vector<int>::iterator itr = std::find(arr.begin(), arr.end(), elem);

if (itr != end(arr)) {
	cout << "Element " << elem << " is present at index " << distance(arr, itr) << " in the given array";
}
else {
	cout << "Element is not present in the given array";
}
Comment

c++ find index of element in array

int valueIndex = std::distance(
  yourArray, 
  std::find(
    yourArray, 
    yourArray + sizeof(yourArray) / sizeof(yourArray[0]),
    yourValueToFind));
Comment

PREVIOUS NEXT
Code Example
Cpp :: access last element in vector in c++ 
Cpp :: what is time complexity of min_element() 
Cpp :: c++ split long code 
Cpp :: c++ enum rand 
Cpp :: c++ srand() 
Cpp :: distinct colors cses solution 
Cpp :: priority queue c++ time complexity 
Cpp :: sqrt cpp 
Cpp :: qt qstring to double 
Cpp :: check if c++ is installed 
Cpp :: default rule of five c++ 
Cpp :: c++ split string by space into vector 
Cpp :: c++ loop pyramid 
Cpp :: rank() in c++ 
Cpp :: how to play sound in c++ 
Cpp :: removing a character from a string in c++ 
Cpp :: c++ fibonacci 
Cpp :: C++ generate a random letter 
Cpp :: create random vectors c++ 
Cpp :: c++ declaring and initializing strings 
Cpp :: c++ extend class 
Cpp :: c++ char to uppercase 
Cpp :: how to round to nearest whole number unity 
Cpp :: how to put bitset into a string in c++ 
Cpp :: random number of 0 or 1 c++ 
Cpp :: max element in array c++ stl 
Cpp :: sieve cpp 
Cpp :: c++ inline in .cpp and not in header 
Cpp :: find max element in array c++ 
Cpp :: read and write file in c++ 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =