Vitality.fff 2023-04-02 15:55 采纳率: 65.2%
浏览 10
已结题

如何输出vector<string>类型函数的返回值?#数据结构#打印朋友圈问题

我在完成一个打印朋友圈的问题,目的是输入汉语拼音后通过定义的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);
    }
//接下来不知道怎么写了。。。
}
  • 写回答

2条回答 默认 最新

  • threenewbee 2023-04-02 16:04
    关注

    vector vec = fc.getFriendCircle(name);
    for (int i = 0; i < vec.size(); i++)
    {
    cout << vec[i] << endl;
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月10日
  • 已采纳回答 4月2日
  • 创建了问题 4月2日

悬赏问题

  • ¥15 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动