我在完成一个打印朋友圈的问题,目的是输入汉语拼音后通过定义的getFriendCircle函打印出这个人的朋友圈,即将结构体中函数getFriendCircle的返回值onesFriendCircle输出,但是我没做到,以下是我的回答。因为我刚接触C++,可能问题很简单,请帮我改正,不胜感激!!
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;
class FriendCircle{
private:
map<string,map<string, int>>circle;
public:
void init(){
circle["zhao"] = {{"qian",1},{"sun",2},{"li",3},{"zhou",4},{"wu",5},{"zheng",0},{"wang",0}};
circle["qian"] = {{"zhao",2},{"sun",6},{"li",1},{"zhou",1},{"wu",0},{"zheng",0},{"wang",0}};
circle["sun"] = {{"zhao",0},{"qian",2},{"li",1},{"zhou",0},{"wu",1},{"zheng",3},{"wang",5}};
circle["li"] = {{"zhao",6},{"qian",1},{"sun",3},{"zhou",0},{"wu",0},{"zheng",1},{"wang",5}};
circle["zhou"] = {{"zhao",0},{"qian",5},{"sun",3},{"li",3},{"wu",2},{"zheng",2},{"wang",0}};
circle["wu"] = {{"zhao",1},{"qian",5},{"sun",2},{"li",0},{"zhou",3},{"zheng",0},{"wang",6}};
circle["zheng"] = {{"zhao",5},{"qian",0},{"sun",0},{"li",1},{"zhou",1},{"wu",2},{"wang",1}};
circle["wang"] = {{"zhao",3},{"qian",0},{"sun",0},{"li",2},{"zhou",5},{"wu",3},{"zheng",3}};
}
void update(){
circle["zhao"]["zhou"] = 1;
circle["zhao"]["zheng"] = 3;
circle["sun"]["li"] = 5;
circle["wu"]["sun"] = 5;
circle["wu"]["wang"] = 1;
circle["zheng"]["li"] = 3;
circle["zheng"]["wu"] = 6;
circle["wang"]["wu"] = 1;
}
vector<string> getFriendCircle(string& name){
vector<pair<int, string>>friends;
for(auto& item : circle[name]){
friends.emplace_back(make_pair(item.second, item.first));
}
sort(friends.begin(), friends.end());
vector<string>onesFriendCircle;
for(auto& friendpair : friends){
onesFriendCircle.emplace_back(friendpair.second);
}
return onesFriendCircle;
}
void printVector(vector<int>& onesFriendCircle){
for(vector<int>::iterator it=onesFriendCircle.begin(); it!=onesFriendCircle.end(); ++it)
cout<<*it<<endl;
cout<<endl;
}
};
string name;
int main(){
FriendCircle fc;
fc.init();
fc.update();
cin>>name;
if(name!="zhao"&&name!="qian"&&name!="sun"&&name!="li"&&name!="zhou"&&name!="wu"&&name!="zheng"&&name!="wang")
cout<<"WRONG";
else {
fc.getFriendCircle(name);
}
//接下来不知道怎么写了。。。
}