Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created July 22, 2015 01:42
Show Gist options
  • Save krysseltillada/f79fc81b0d3a044506f4 to your computer and use it in GitHub Desktop.
Save krysseltillada/f79fc81b0d3a044506f4 to your computer and use it in GitHub Desktop.
std::distance
#include <iostream>
#include <vector>
int main ()
{
std::vector <int> vec = {22, 33, 2, 1, 2, 5};
int i = 2;
for (std::vector <int>::iterator it = vec.begin(); it != vec.end(); ++it) {
if (*it == i ) {
std::cout << i << " was found at element " << std::distance(vec.begin(), it) << std::endl;
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment