cpp priorityqueue cusotm comparitor code example

Example 1: find_first_of

// find_if example
#include <iostream>     // std::cout
#include <algorithm>    // std::find_if
#include <vector>       // std::vector

bool IsOdd (int i) {
  return ((i%2)==1);
}

int main () {
  std::vector<int> myvector;

  myvector.push_back(10);
  myvector.push_back(25);
  myvector.push_back(40);
  myvector.push_back(55);

  std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
  std::cout << "The first odd value is " << *it << '\n';

  return 0;
}

Example 2: how to instanciate map.entry java

/**
 * @author Jack Waller <[email protected]>
 * @version 1.1
 * @since 2020-03-28
 * A implementation of Map.Entry.
 */
public final class Pair<K, V> implements Map.Entry<K, V> {

    //variables
    private final K key;
    private V value;

    //constructor
    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    //methods

    /**
     * Returns the key.
     * @return K
     */
    @Override
    public K getKey() {
        return key;
    }

    /**
     * Returns the value.
     * @return V
     */
    @Override
    public V getValue() {
        return value;
    }

    /**
     * Sets the value, returns the old value
     * @param v value to set
     * @return V
     */
    @Override
    public V setValue(V v) {
        V old = this.value;
        this.value = v;
        return old;
    }
}

Tags:

Cpp Example