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.
If you can sort it, then you can get either the first element, if you sorted it descenent, or the last element, if you sorted it ascendent.

But if you don't want to sort it, you can go throughout the vector, and at every step checking for the highest int, and memorize the string:

```
maxInt = array[0]; //int
maxString = array[0].String;
for (i=1;i<arrayLength;i++){
      if (array[i] > maxInt) {
             maxInt = array[i];
             maxString = array[i].String;
      } 
}

```

And then you can simply print out the maxString.