[英]How to output a specific Map value's key in alphabetical order in C++?
我正在嘗試打印出特定映射值的內容,但是根據括號內的字母按字母順序排列。
下面是不符合我想要做的原始輸出。
| Element [ab] Element 2 Element [a] Element 1 Element [b] Element 3 Element [e] Element 6 Element [d] Element 5 Element [c] Element 4 |
現在我試圖得到的輸出如下,它將使用 printSelectiveAlphabetic() 函數
| Element [a] Element 1 Element [ab] Element 2 Element [b] Element 3 Element [c] Element 4 Element [d] Element 5 Element [e] Element 6 |
那么如何調整我的 printSelectiveAlphabetic() 函數以獲得所需的輸出?
下面的代碼:
#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;
}
最簡單的方法是使用std::sort
對與v[0]
對應的向量進行排序,以實現您的預期輸出,如下所示:
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;
}
上述程序的輸出是:
|
Element [a] Element 1
Element [ab] Element 2
Element [b] Element 3
Element [c] Element 4
Element [d] Element 5
Element [e] Element 6
|
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.