Question: I'm completely new to ajax. I want to pass my html form value to php and then get the output from that php page back in the html page

Ajax code 

```
 $("#submit1").click(function(){
 var formData = $("#add-cart-form").serialize();

 $.ajax({
    type: 'POST',
    url: 'test.php',
    dataType : 'json',
    data: {
        'total_load' : $('#total_load').val(),
        'hours' : $('#hours').val(),
    },
     success: function (data) {
       alert(data)
     },
  });
 var result = $.parseJSON(output);
 alert(result[0]);
});

```

Relevant php code

```
$connected_load = $_POST['total_load'];  $no_of_hours = $_POST['hours'];

 echo json_encode(array(
     $connected_load, 
     $selected_inverter["model"], 
     $selected_inverter["voltage"], 
     $selected_inverter["type"],
     $no_of_hours,$selected_battery));

```

Answer: 
First, sort the vector using stable sort w.r.t to the integer value. Then, use `lower_bound` to search the first element with the maximum integer value. For a `vector v`, the code would look like:

```
bool cmpFirst(std::pair<int, std::string> const& x, 
              std::pair<int, std::string> const& y)
{
    return x.first < y.first;
}

std::stable_sort(v.begin(), v.end(), cmpFirst);
auto firstMaxString = std::lower_bound( v.begin(), v.end(), 
                                        v.back().first, 
                                        cmpFirst)->second;

```

If you need to do it only once, scanning the vector is faster because it is an O(n) operation, while sorting is O(n log n). See the answer from Open Kastle for this solution.
You can use `std::max_element` with a lambda express to search a range for the maximum element using your specific comparison criteria. In this case you define your comparison function to compare only the `int` value in the pair:

```
std::vector<std::pair<int, std::string>> myList = { ... };

auto find = std::max_element(begin(myList), end(myList),
    [](const std::pair<int, std::string>& pairA, const std::pair<int, std::string>& pairB)
    {
        return pairA.first < pairB.first;
    }
);

std::string maximumString = find->second;

```