Search
 
SCRIPT & CODE EXAMPLE
 

CPP

std distance c++

// C++ program to demonstrate std::distance() 
#include <iostream> 
#include <vector> 
#include <iterator> 
using namespace std; 
int main() 
{ 
    vector<int> v; 
    int i; 
  
    for (i = 0; i < 10; ++i)  
    { 
        v.push_back(i); 
    } 
  
    /*v contains 0 1 2 3 4 5 6 7 8 9*/
  
    vector<int>::iterator first; 
    vector<int>::iterator last; 
  
    // first pointing to 0 
    first = v.begin(); 
  
    // last pointing to 5 
    last = v.begin() + 5; 
  
    // Calculating no. of elements between first and last 
    int num = std::distance(first, last); 
  
    // Displaying num 
    cout << num << "
"; 
    return 0; 
} 
Comment

std distance

// Calculates the number of elements between first and last.

#include <iterator>     									// std::distance
#include <vector>     										// std::vector
#include <algorithm>   							// Just if you use std::find

vector<int> arr = {2,5,3,8,1};
int size = std::distance(arr.begin(), arr.end()); 			// 5

auto it = std::find(arr.begin(), arr.end(), 8);
int position = std::distance(arr.begin(), it); 				// 3
Comment

PREVIOUS NEXT
Code Example
Cpp :: typedef vector c++ 
Cpp :: C++ convert integer to digits, as vector 
Cpp :: malloc in c++ 
Cpp :: c++ infinite for loop 
Cpp :: c++ get time 
Cpp :: factorial using recursion cpp 
Cpp :: round double to n decimal places c++ 
Cpp :: how to open and read text files in c++ 
Cpp :: gfgdf 
Cpp :: c++ open all files in directory 
Cpp :: http.begin() error 
Cpp :: reverse c++ 
Cpp :: run c++ program in mac terminal 
Cpp :: max value of double c++ 
Cpp :: how to get size of char array in c++ 
Cpp :: resize 2d vector c++ 
Cpp :: c++ call method in same class 
Cpp :: cpp ifstream 
Cpp :: string to decimal c++ strtol 
Cpp :: what does the modularity mean in c++ 
Cpp :: ubuntu dotnet core install 
Cpp :: how to create a vector in c++ 
Cpp :: 2-Dimensional array in c++ 
Cpp :: Count Prefix of a Given String solution leetcode 
Cpp :: int max c++ 
Cpp :: string to integer in c++ 
Cpp :: how to find last character of string in c++ 
Cpp :: cpp when use size_t 
Cpp :: image shapes in opencv c++ 
Cpp :: reverse sort a vector 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =