Vector of strings inserted into map

There are random words which has any two strings with same first letter. For example: Apple, Giraffe, Lion, Leopard, Ape, Goat. 

I would like to display the two string with their first character in ASCII as a pairs. 

65 - Ape, Apple

71 - Giraffe, Goat

76 - Leopard, Lion



Click here to debug the below program

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
  
void sortString(std::vector<std::string>& arr, 
                std::map<int, std::vector<std::string>>& mp)
{
    std::sort(arr.begin(), arr.end());
    for (int i = 0, sz(arr.size()); i < sz; ) {
        std::string str = arr[i];
        std::vector<std::string> indices;
        indices.push_back(arr[i]);
        indices.push_back(arr[i+1]);
        /*std::cout << "i " << i << " key " << (str.at(0)) << " value " 
                  << arr[i] << " - " << arr[i+1] << std::endl; */
        mp.insert(std::make_pair((int)(str.at(0)), indices));
        i = i+2;
    }
    return;
}

void printMapElements(std::map<int, std::vector<std::string>> mp)
{
    std::cout << "\nIndices Pair: \n";
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
        std::cout << "Key:ASCII " << itr->first << "   ";
        std::vector<std::string> idx = itr->second;
        std::cout << "Value: " << idx[0] << "  " << idx[1]; 
        std::cout << "\n";
    } 
    return;
}
  
void printArray(std::vector<std::string> arr)
{
    for (int i = 0; i < arr.size(); i++)
        std::cout << arr[i] << " ";
    std::cout << std::endl;
}
  
int main()
{
    std::vector<std::string> str = { "Hello", "Colourful", "World",
                           "How", "Cute", "Watch" };
    std::map<int, std::vector<std::string>> mp;
    
    std::cout << "Before Inserting into Map: \n";
    printArray(str);
    sortString(str, mp);
    
    std::cout << "\nSorted array: \n";
    printArray(str);
    
    printMapElements(mp);
    
    return 0;
}


Output:

1

Before Inserting into Map: 

Hello Colourful World How Cute Watch 


Sorted array: 

Colourful Cute Hello How Watch World 


Indices Pair: 

Key:ASCII 67   Value: Colourful  Cute

Key:ASCII 72   Value: Hello  How

Key:ASCII 87   Value: Watch  World


Comments