'How to output a specific Map value's key in alphabetical order in C++?
I'm trying to print out the contents of a specific map value, but in alphabetical letter based on the letters inside of the brackets.
Below is the original output which doesn't meet what I'm trying to do.
| Element [ab] Element 2 Element [a] Element 1 Element [b] Element 3 Element [e] Element 6 Element [d] Element 5 Element [c] Element 4 |
Now the output I'm trying to get is below, which would be using the printSelectiveAlphabetic() function
| Element [a] Element 1 Element [ab] Element 2 Element [b] Element 3 Element [c] Element 4 Element [d] Element 5 Element [e] Element 6 |
So how do I adjust my printSelectiveAlphabetic() function to get this desired output?
Code Below:
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <map>
using namespace std;
//Prints whole map
void printMap(map<string, vector<string>> mapVar) {
cout << endl;
map<string ,vector<string>> :: iterator it;
for(it=mapVar.begin();it !=mapVar.end();++it)
{
cout << it->first << endl;
for(auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
cout << *it2 << " ";
cout << endl;}
}
}
//Prints specific value of map
void printSelectiveMap(vector<string> v, map<string, vector<string>> helpMap) {
cout << "\t|"<<endl;
string searchValue = v[0];
for (auto map_iter = helpMap.cbegin(); map_iter != helpMap.cend(); ++map_iter) {
if (v[0] == map_iter->first) {
int count = 0;
for (auto vec_iter = map_iter->second.cbegin(); vec_iter != map_iter->second.cend(); ++vec_iter) {
cout << "\t" << searchValue << " " << *vec_iter << endl;
}
}
}
cout << "\t|"<<endl;
}
//A copy of the above function, what I'm trying to change to have alaphabetical output
//This needs to use the letters, not the numbers in the vector to implement this
void printSelectiveAlphabetic(vector<string> v, map<string, vector<string>> helpMap) {
cout << "\t|"<<endl;
string searchValue = v[0];
for (auto map_iter = helpMap.cbegin(); map_iter != helpMap.cend(); ++map_iter) {
if (v[0] == map_iter->first) {
int count = 0;
for (auto vec_iter = map_iter->second.cbegin(); vec_iter != map_iter->second.cend(); ++vec_iter) {
cout << "\t" << searchValue << " " << *vec_iter << endl;
}
}
}
cout << "\t|"<<endl;
}
int main() {
map<string, vector<string>> maphelp;
vector<string> vect;
vect.push_back("[ab] Element 2");
vect.push_back("[a] Element 1");
vect.push_back("[b] Element 3");
vect.push_back("[e] Element 6");
vect.push_back("[d] Element 5");
vect.push_back("[c] Element 4");
maphelp.insert(pair<string,vector<string>>("Element", vect));
cout << endl;
vector<string> vect1;
vect.push_back("[ab] Element1 2");
vect1.push_back("[a] Element1 1");
vect1.push_back("[b] Element1 3");
vect1.push_back("[e] Element1 6");
vect1.push_back("[d] Element1 5");
vect1.push_back("[c] Element1 4");
maphelp.insert(pair<string,vector<string>>("Element1", vect1));
cout << "-------------------" << endl;
cout << "This output is not part of the question." << endl;
cout << "These are just printed variables below for reference:" << endl;
cout << endl;
cout << "Entire map printed.";
printMap(maphelp);
cout << endl;
vector<string> v;
v.push_back("Element");
cout << "Selective value of map to be printed: " << v[0] << endl;
cout << "-------------------" << endl;
cout << endl;
cout << "Now below is the original output which doesn't meet what I'm trying to do." << endl;
printSelectiveMap(v, maphelp);
cout << endl;
cout << "Now the output I'm trying to get is below, which would be using the printSelectiveAlphabetic() function" << endl;
//function I'm trying to adjust
//PrintSelectiveAlphabetic()
cout << "\t|"<<endl;
cout << "\tElement [a] Element 1" << endl;
cout << "\tElement [ab] Element 2" << endl;
cout << "\tElement [b] Element 3" << endl;
cout << "\tElement [c] Element 4" << endl;
cout << "\tElement [d] Element 5" << endl;
cout << "\tElement [e] Element 6" << endl;
cout << "\t|"<<endl;
cout << "-------------------" << endl;
cout<<endl;
cout << "So how do I adjust my printSelectiveAlphabetic() function to get this desired output?"<< endl;
}
Solution 1:[1]
The simplest way would to use std::sort to sort the vector corresponding to v[0] to achieve your expected output as shown below:
void printSelectiveAlphabetic(vector<string> v, map<string, vector<string>> helpMap) {
cout << "\t|"<<endl;
std::sort(helpMap.at(v[0]).begin(), helpMap.at(v[0]).end()); //sort the vector corresponding to the key v[0]
for(const std::string& element: helpMap[v[0]])
{
std::cout<<v[0] <<" "<<element<<std::endl;
}
cout << "\t|"<<endl;
}
The output of the above program is:
|
Element [a] Element 1
Element [ab] Element 2
Element [b] Element 3
Element [c] Element 4
Element [d] Element 5
Element [e] Element 6
|
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anoop Rana |
