how to find the index of an element in a vector c++
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";
}#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v = { 7, 3, 6, 2, 6 };
int key = 6;
std::vector<int>::iterator itr = std::find(v.begin(), v.end(), key);
if (itr != v.cend()) {
std::cout << "Element present at index " <<
std::distance(v.begin(), itr);
}
else {
std::cout << "Element not found";
}
return 0;
}
Also in C++:
- Title
- how to run c++ file mingw cmd
- Category
- C++
- Title
- delay millis arduino
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- convert integer to string c++
- Category
- C++
- Title
- how to decalre a string in c++
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- how to avoid tle in c++
- Category
- C++
- Title
- how to use a new node c++
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- bool function in c++
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- c++ while loop code
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- if esle in c++
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- c++ code 2d block
- Category
- C++
- Title
- how to find the index of an element in a vector c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- ue4 c++ struct
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- c++ template function
- Category
- C++
- Title
- c++ menu selection with arrow keys
- Category
- C++
- Title
- count a character in a string c++
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- preorder traversal c++
- Category
- C++
- Title
- c++ for loops
- Category
- C++
- Title
- c++ get ascii value of char
- Category
- C++
- Title
- stack function in cpp
- Category
- C++
- Title
- convert GLFWwindow* to IntPtr
- Category
- C++
- Title
- c++ char if
- Category
- C++
- Title
- c++ stack
- Category
- C++
- Title
- new in c++
- Category
- C++
- Title
- traverse a map
- Category
- C++
- Title
- how to use assrt c++
- Category
- C++
- Title
- switch c++
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- removing repeated characters in a string c++
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- variant hold type
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- apple and orange hackerrank solution in c++
- Category
- C++
- Title
- min heap priority queue c++
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- c++ push multiple elements to vector
- Category
- C++
- Title
- c++ class template
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- get data from terminal c++
- Category
- C++
- Title
- opperanf >> c++
- Category
- C++
- Title
- findung the mode in c++
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- c++ switch
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- mingw32/bin/ld.exe: C:\Users\mfrom\AppData\Local\Temp\ccSKcRks.o:PizzaPi.cpp:(.text$_ZN5PizzaC2Ev[__ZN5PizzaC2Ev]+0xa): undefined reference to `vtable for Pizza' collect2.exe: error: ld returned 1 exit status
- Category
- C++
- Title
- floor() in c++
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- Runtime Error: Runtime ErrorBad memory access (SIGBUS)
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- iterative preorder traversal
- Category
- C++
- Title
- c++ short if
- Category
- C++
- Title
- is x prime?
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- C++ w3schools
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- c++ append to list
- Category
- C++
- Title
- sum of two numbers c++
- Category
- C++
- Title
- double pointers C++
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- what is sigsegv error in c++
- Category
- C++
- Title
- cpp loop through object
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- std::reverse
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- c++ ros subscriber
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- nan c++ example
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- prefix sum array
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- Newton's sqrt in c++
- Category
- C++