Answers for "lower bound c++"

C++
1

lower bound and upper bound in c++

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;cin>>n;
    vector<int>v;

    for(int i=0;i<n;i++){
        cin>>v[i];
    }
    sort(v.begin(),v.end());


    //lower bound for vector
    auto pointer1 = lower_bound(v.begin(), v.end(), 7);
    cout<<(*pointer1)<<endl;

    //lower bound for array;
    int array[n];
    for(int i=0;i<n;i++){
        cin>>array[i];
    }
    sort(array,array+n);
    //lowerbound
    int *pointer2 = lower_bound(array, array+n, 7);

    //if you want upper bound then just replace lower_bound with upper_bound
}
Posted by: Guest on September-04-2021
1

lower_bound c++

// lower_bound/upper_bound example
#include <iostream>     // std::cout
#include <algorithm>    // std::lower_bound, std::upper_bound, std::sort
#include <vector>       // std::vector

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  std::vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20

  std::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30

  std::vector<int>::iterator low,up;
  low=std::lower_bound (v.begin(), v.end(), 20); //          ^
  up= std::upper_bound (v.begin(), v.end(), 20); //                   ^

  std::cout << "lower_bound at position " << (low- v.begin()) << '\n'; 
  std::cout << "upper_bound at position " << (up - v.begin()) << '\n';

  return 0;
}

// Output
// lower_bound at position 3
// upper_bound at position 6
Posted by: Guest on August-02-2020
0

lower bound upper bound cpp

// lower_bound/upper_bound example
#include <iostream>     // std::cout
#include <algorithm>    // std::lower_bound, std::upper_bound, std::sort
#include <vector>       // std::vector

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  std::vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20

  std::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30

  std::vector<int>::iterator low,up;
  low=std::lower_bound (v.begin(), v.end(), 20); //          ^
  up= std::upper_bound (v.begin(), v.end(), 20); //                   ^

  std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
  std::cout << "upper_bound at position " << (up - v.begin()) << '\n';

  return 0;
}
Posted by: Guest on April-23-2021
0

lower bound c++

The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than val.
Posted by: Guest on April-19-2021

Browse Popular Code Answers by Language