首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MultiMap不打印列表

MultiMap不打印列表
EN

Stack Overflow用户
提问于 2021-08-09 23:30:40
回答 2查看 53关注 0票数 0

我的代码有两个问题。

第一个问题是我的multimap不能打印,我不知道为什么。

第二个问题是,我不知道如何创建一个函数来搜索和查找我的multimap中的特定项目。

您可以随意更改代码,但我需要保留类或将其更改为结构。

代码语言:javascript
复制
#include<iostream>
#include<string> 
#include<iterator>
#include<map>
#include<algorithm>
#include<iomanip>
using namespace std;

class SongNode {
public:
    string title;
    string artist;
    float rating;
};

//Constants
#define INFINITE_LOOP 1
#define LEFT_JUSTIFIED 30

//Prototypes

void sortByArtist(multimap<string, SongNode>& newSongNode); 
void sortByRating(multimap<string, SongNode>& newSongNode); 


//FUNCTION      : void sortByArtist
//PARAMETER     : multimap<string, SongNode>&newSongNode
//RETURN        : void
//DESCRIPTION   : This function will sort the multimap by the artist of the song
void sortByArtist(multimap<string, SongNode>&newSongNode) {
    multimap<string, SongNode>temp; 

    for (auto i = newSongNode.begin(); i != newSongNode.end(); i++) 
        temp.insert(pair<string, SongNode>(i->second.artist, i->second)); 
    newSongNode.clear(); 

    for (auto i = temp.begin(); i != temp.end(); i++)
        newSongNode.insert(pair<string, SongNode>(i->second.artist, i->second)); 
    temp.clear(); 
}

//FUNCTION      : void sortByRating
//PARAMETER     : (multimap<string, SongNode>& newSongNode
//RETURN        : void
//DESCRIPTION   : This function will sort the multimap by the rating. 
void sortByRating(multimap<string, SongNode>& newSongNode) {

    multimap<float, SongNode, greater<int>> temp; 

    for (auto i = newSongNode.begin(); i != newSongNode.end(); i++)
        temp.insert(pair<int, SongNode>(i->second.rating, i->second)); 
    newSongNode.clear(); 

    for (auto i = temp.begin(); i != temp.end(); i++)
        newSongNode.insert(pair<string, SongNode>(" ", i->second)); 
    temp.clear(); 
}
    
int main() {

    multimap<string, SongNode> songList; 
    multimap<string, SongNode>::iterator itr; 
    string title; 
    string artist; 
    int rating; 
    SongNode* newSongNode = new SongNode; 

    cout << "Please enter a title artist and rating\n"; 

    while (INFINITE_LOOP) {
        cout << "Title: "; 
        cin >> title;

        if (title == ".") {
            break; 
        }

        cout << "Artist: "; 
        cin >> artist; 

        cout << "Rating: ";
        cin >> rating; 

        //Error check  for rating 
        while (INFINITE_LOOP) {     

            if (rating < 1) {
                cout << "The rating you have entered is less then 1\n";
                cout << "Rating: ";
                cin >> rating;
                break;
            }

            else if (rating > 5) {
                cout << "The rating you have to enterd is greater then 5\n";
                cout << "Rating: ";
                cin >> rating;
                break; 
            }

            else
                break;
        }

        SongNode* newNode = new SongNode; 

        newNode->title = title;
        newNode->artist = artist; 
        newNode->rating = rating; 

        songList.insert(pair<string, SongNode>(title, *newSongNode));
    }

    //display the two lists here. 
    cout << "Artist Sorted List\n"; 
    sortByArtist(songList); 
    for (itr = songList.begin(); itr != songList.end(); ++itr) {
        cout << "Title" << left << setw(LEFT_JUSTIFIED) << itr->second.title; 
        cout << "Artist" << left << setw(LEFT_JUSTIFIED) << itr->second.artist; 
        cout << "Rating" << left << setw(LEFT_JUSTIFIED) << itr->second.rating<<endl; 
    }

    cout << "\n"; 
    cout << "Rating Sorted List\n"; 
    sortByRating(songList); 

    delete newSongNode; 
    return 0;
}
EN

回答 2

Stack Overflow用户

发布于 2021-08-09 23:45:10

首先,您有两个名称相似的变量,并且您肯定会滥用它们:

代码语言:javascript
复制
    SongNode* newSongNode = new SongNode; 
    // ...
    SongNode* newNode = new SongNode; 
   
    newNode->title = title;
    newNode->artist = artist; 
    newNode->rating = rating; 

    songList.insert(pair<string, SongNode>(title, *newSongNode));

您正在初始化newNode,但插入了newSongNode。这些值是用空字符串初始化的,因此可以解释不打印的原因。

至于“在多地图中搜索”,要更具体地说明你的问题是什么。

票数 1
EN

Stack Overflow用户

发布于 2021-08-10 00:04:31

您不会用用数据填充的SongNode对象填充songList。您有一个不包含任何数据的额外SongNode,它是您要放入songList的唯一对象。因此,没有什么可打印的。

试着这样做:

代码语言:javascript
复制
#include <iostream>
#include <string> 
#include <iterator>
#include <map>
#include <algorithm>
#include <iomanip>
#include <limits>
using namespace std;

struct SongNode {
    string title;
    string artist;
    float rating;
};

//Constants
#define INFINITE_LOOP 1
#define LEFT_JUSTIFIED 30

//Prototypes

void sortByArtist(multimap<string, SongNode>& newSongNode); 
void sortByRating(multimap<string, SongNode>& newSongNode); 


//FUNCTION      : void sortByArtist
//PARAMETER     : multimap<string, SongNode>&newSongNode
//RETURN        : void
//DESCRIPTION   : This function will sort the multimap by the artist of the song
void sortByArtist(multimap<string, SongNode> &newSongNode) {
    multimap<string, SongNode> temp; 

    for (auto &elem : newSongNode) 
        temp.insert(make_pair(elem.second.artist, elem.second)); 
    newSongNode.clear(); 

    for (auto &elem : temp)
        newSongNode.insert(make_pair(elem.second.artist, elem.second)); 
}

//FUNCTION      : void sortByRating
//PARAMETER     : (multimap<string, SongNode>& newSongNode
//RETURN        : void
//DESCRIPTION   : This function will sort the multimap by the rating. 
void sortByRating(multimap<string, SongNode>& newSongNode) {

    multimap<float, SongNode, greater<int>> temp; 

    for (auto &elem : newSongNode)
        temp.insert(make_pair(elem.second.rating, elem.second)); 
    newSongNode.clear(); 

    for (auto &elem : temp)
        newSongNode.insert(make_pair(" ", elem.second)); 
}
    
int main() {

    multimap<string, SongNode> songList; 
    string title, artist;
    float rating; 

    cout << "Please enter a title artist and rating\n"; 

    while (INFINITE_LOOP) {
        cout << "Title: "; 
        cin >> title;

        if (title == ".") {
            break; 
        }

        cout << "Artist: "; 
        cin >> artist; 

        cout << "Rating: ";
        cin >> rating; 

        //Error check for rating
        while (INFINITE_LOOP) {     
            if (!cin) {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "The rating you have entered is invalid\n";
            }
            else if (rating < 1.0f) {
                cout << "The rating you have entered is less then 1\n";
            }
            else if (rating > 5.0f) {
                cout << "The rating you have to entered is greater then 5\n";
            }
            else
                break;

            cout << "Rating: ";
            cin >> rating;
        }

        SongNode newNode; 

        newNode.title = title;
        newNode.artist = artist; 
        newNode.rating = rating; 

        songList.insert(make_pair(title, newNode));
    }

    //display the two lists here. 

    cout << "Artist Sorted List\n"; 
    sortByArtist(songList); 
    for (auto &elem : songList) {
        cout << "Title" << left << setw(LEFT_JUSTIFIED) << elem.second.title; 
        cout << "Artist" << left << setw(LEFT_JUSTIFIED) << elem.second.artist; 
        cout << "Rating" << left << setw(LEFT_JUSTIFIED) << elem.second.rating << endl; 
    }
    cout << "\n";

    cout << "Rating Sorted List\n"; 
    sortByRating(songList); 
    for (auto &elem : songList) {
        cout << "Title" << left << setw(LEFT_JUSTIFIED) << elem.second.title; 
        cout << "Artist" << left << setw(LEFT_JUSTIFIED) << elem.second.artist; 
        cout << "Rating" << left << setw(LEFT_JUSTIFIED) << elem.second.rating << endl; 
    }
    cout << "\n";

    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68719569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档