std distance c 2b 2b

Solutions on MaxInterview for std distance c 2b 2b by the best coders in the world

showing results for - "std distance c 2b 2b"
Irene
26 Jan 2018
1// C++ program to demonstrate std::distance() 
2#include <iostream> 
3#include <vector> 
4#include <iterator> 
5using namespace std; 
6int main() 
7{ 
8    vector<int> v; 
9    int i; 
10  
11    for (i = 0; i < 10; ++i)  
12    { 
13        v.push_back(i); 
14    } 
15  
16    /*v contains 0 1 2 3 4 5 6 7 8 9*/
17  
18    vector<int>::iterator first; 
19    vector<int>::iterator last; 
20  
21    // first pointing to 0 
22    first = v.begin(); 
23  
24    // last pointing to 5 
25    last = v.begin() + 5; 
26  
27    // Calculating no. of elements between first and last 
28    int num = std::distance(first, last); 
29  
30    // Displaying num 
31    cout << num << "\n"; 
32    return 0; 
33} 
Auguste
13 Jan 2018
1// Calculates the number of elements between first and last.
2
3#include <iterator>     									// std::distance
4#include <vector>     										// std::vector
5#include <algorithm>   							// Just if you use std::find
6
7vector<int> arr = {2,5,3,8,1};
8int size = std::distance(arr.begin(), arr.end()); 			// 5
9
10auto it = std::find(arr.begin(), arr.end(), 8);
11int position = std::distance(arr.begin(), it); 				// 3
12