Home > database >  How to use std::list next?
How to use std::list next?

Time:06-24

I tried to make playlist with std::list but it's difficult to implement now, next, previous help me

std::list<int> mlist;
std::list<int>::iterator head;
head=mlist.begin();
std::cout << *head << std::endl;

CodePudding user response:

Your list is empty, what were you expecting it to print?

Try this instead

std::list<int> mlist;  // empty list
mlist.push_front(123); // add 123 to the beginning of the list
std::list<int>::iterator head = mlist.begin(); // iterator to the first element of the list
std::cout << *head << std::endl; // prints 123

You can make this code a bit shorter and more readable by using auto instead of std::list<int>::iterator (the compiler knows what type head must be).

std::list<int> mlist;  // empty list
mlist.push_front(123); // add 123 to the beginning of the list
auto head = mlist.begin(); // iterator to the first element of the list
std::cout << *head << std::endl; // prints 123

It's common to use auto for iterator variables.

For next and previous, just use and -- same as other iterators.

std::list<int> mlist;  // empty list
mlist.push_back(123); // add 123 to the end of the list
mlist.push_back(456); // add 456 to the end of the list
mlist.push_back(789); // add 789 to the end of the list
// loop through list from first to last, using   
for (auto i = mlist.begin(); i != mlist.end();   i)
    std::cout << *i << '\n';

CodePudding user response:

Here is a very simple example:

#include <iostream>
#include <string>
#include <list>


struct AudioTrack
{
    int m_ID { };
    std::string m_title { "Unknown" };
    std::string m_singer { "Unknown" };

    AudioTrack( const int ID, const std::string& title,
                const std::string& singer )
    : m_ID( ID ), m_title( title ), m_singer( singer )
    {
    }
};


int main( )
{
    std::list<AudioTrack> playList;
    playList.emplace_back( AudioTrack { 123, "Still D.R.E.", "Dr. Dre" } );
    playList.emplace_back( AudioTrack { 425, "Roar", "Katy Perry" } );
    playList.emplace_back( AudioTrack { 174, "Bad Blood", "Taylor Swift" } );

    std::cout << "Enter N to play the next song, "
                 "P to play the previous song, "
                 "C to play the current song, "
                 "E to exit\n";

    const auto head { playList.cbegin( ) };
    auto item { head };

    char choice { };

    do
    {
        std::cout << "\nYour choice: ";
        std::cin >> choice;

        switch ( choice )
        {
        case 'N':
            std::cout << "Playing the next song...\n";
              item;
            if ( item == playList.cend( ) ) { item = head; }
            std::cout << "Song info: \nTitle: "
                      << ( *item ).m_title
                      << "\nSinger: " << ( *item ).m_singer
                      << '\n';
            break;
        case 'P':
            std::cout << "Playing the previous song...\n";
            --item;
            if ( item == std::prev( head, 1 ) ) { item = std::prev( playList.cend( ), 1 ); }
            std::cout << "Song info: \nTitle: "
                      << ( *item ).m_title
                      << "\nSinger: " << ( *item ).m_singer
                      << '\n';
            break;
        case 'C':
            std::cout << "Playing the current song...\n";

            std::cout << "Song info: \nTitle: "
                      << ( *item ).m_title
                      << "\nSinger: " << ( *item ).m_singer
                      << '\n';
            break;

        }

    } while ( choice != 'E' );
}

Sample input/output:

Enter N to play the next song, P to play the previous song, C to play the current song, E to exit

Your choice: C
Playing the current song...
Song info: 
Title: Still D.R.E.
Singer: Dr. Dre

Your choice: P
Playing the previous song...
Song info: 
Title: Bad Blood
Singer: Taylor Swift

Your choice: P
Playing the previous song...
Song info: 
Title: Roar
Singer: Katy Perry

Your choice: E

Notice that by placing some if statements in the switch statement I have made the list act like a circular list. But it's not a circular list on its own. Consequently, you can go from the last element to the first or vice versa without invoking undefined behavior.

  •  Tags:  
  • c
  • Related