Stl queue or multiset?

Hello there, I am doing homework assignmet on STL, and I have a question. One of the function need to take two different arguments and create a data structure, but I'm not sure which container to use. I tried using queue. push but it says that this function can't take two argumenst. Is multiset is better?


1
2
enum eSection{ INTRO, VERSE, CHORUS, TRANSITION };
enum eSectionDurationInSeconds{ INTOR_DUR = 5, VERSE_DUR = 10, CHORUS_DUR = 20, TRANSITION_DUR = 5 };


1
2
3
void Song::addSection(eSection sect, eSectionDurationInSeconds iDuration){
	m_songStructure.push(sec,iDuration);
}

> it says that this function can't take two argumenst

Use a queue of pairs. http://en.cppreference.com/w/cpp/utility/pair

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <queue>
#include <utility>

enum section { INTRO, VERSE, CHORUS, TRANSITION };
enum duration { INTOR_DUR = 5, VERSE_DUR = 10, CHORUS_DUR = 20, TRANSITION_DUR = 5 };

std::queue< std::pair<section,duration> > song_structure ;

void foo( section s, duration  d )
{
	song_structure.emplace( s, d ) ; // C+11

	// or: song_structure.push( std::make_pair(s,d) ) ; // C++98
}
Thank you! :)
std::vector
Store elements in given order and tightly packed in memory (like arrays do).

std::list
Store elements in given order but not tightly packed in memory. Because the elements are linked, insertion and removal of elements is usually (theoretically) faster than for std::vector.

std::queue std::stack
These are like simplified lists. You use them to make it clear to others the fashion in which you add and remove elements. Queues: FIFO (first in first out), stacks: LIFO (last in first out).

std::set
Store a single copy of an element, also it sorts its contents!
If you add to an std::set<int> the values
{4, 3, 3, 3, 1, 5}
it will contain
{1, 3, 4, 5}

std::multiset
Similar to a set, but copies of the same value are permitted.
If you add to an std::multiset<int> the values
{4, 3, 3, 3, 1, 5}
it will contain
{1, 3, 3, 3, 4, 5}

std::map
Stores key and element pairs. You use the key to access an element. For instance, arrays are a type of map.
my_array[11] = 3; // 11 is the key, 3 is the element
However "real" maps can use things other than indexes as keys:
my_map["eleven"] = 3; // "eleven" is the key, 3 is the element

std::multimap
Similar to map, but you can have more elements mapped to a single key. So you can use a key to get a list of elements instead of a single element.

So to answer the question in the thread's title... well I guess you could use a multiset instead of a queue if you wanted to have your elements automatically sorted...
Topic archived. No new replies allowed.