Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives: Gain familiarity with the STL by adding and removing items from STL

ID: 3597635 • Letter: O

Question

Objectives: Gain familiarity with the STL by adding and removing items from STL containers; compare performance of adding and removing items from containers.

Requirement: All answers must be typed in this document.

You will have to write code to answer the questions below. Include your printed code and its output at the end of this document. To time the execution of the various data structures you may use the following approach.

-------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <time.h>

clock_t timerStart, timerEnd;

timerStart = clock();

// place code you want to time here

timerEnd = clock();

// Elapsed time in milliseconds is timerEnd – timerStart – Convert all answers to decimal seconds (e.g., 0.124 secs).

------------------------------------------------------------------------------------------------------------------------------------------------------------

In Visual Studio 2015, declare the following STL containers:

a vector that holds ints – add items using push_back()

a list that holds ints – add items using push_front()

a forward_list that holds ints – add items using push_front()

a stack (with the default deque implementation) that holds ints

a stack (implemented as a vector) that holds ints

a stack (implemented as a list) that holds ints

a queue (with the default queue implementation) that holds ints – add with push(), remove with pop()

a queue (implemented as a deque) that holds ints – add with push_back, remove with pop_front()

a queue (implemented as a list) that holds ints – add with push(), remove with pop()

For each of the following comparisons provide the following:

# of items, time it took to perform the operations on the data structures, percentage faster

Example: Added 1,000,000 ints - Vector took 2.6 seconds, List took 3 seconds - Vector is 15% faster

Your analysis of the reason for the time difference. This analysis must be a reasonable explanation (i.e. saying a vector implementations is faster/slower “due to contiguous storage” is inadequate. Explaining how/why contiguous storage makes a vector faster/slower should be the goal).

Exercises – To ensure you make maximum use of system memory, make each comparison separately (comment out code that does not apply to the comparison):

(10 pts) Compare a. and b. above by adding 1,000,000 random ints

(10 pts) Compare b. and c. above by adding 1,000,000 random ints

(30 pts) Compare d., e., and f. above by pushing 1,000,000 random ints and then popping 500,000 of those – compare the pushing of items and popping of items separately

(30 pts) Compare g., h., and i. above by enqueing 1,000,000 random ints and then dequeing 500,000 of those – compare the enqueing of items and dequeing of items separately.

Provide an analysis that summarizes your overall fundings across all comparisons made.

Note: If you are unable to allocate the stated number of ints, allocate as many as your memory allows, and state the number allocated in your answer.

Explanation / Answer

5

6

#include

<stack>

7

#include

<queue>

8

using

namespace

std;

9  

10

int

main()

11 {

12    vector<

int

> vector1, vector2;

13    list<

int

> list1, list2;

14    deque<

int

> deque1, deque2;

15    set<

int

> set1, set2;

16    multiset<

int

> multiset1, multiset2;

17    stack<

int

> stack1, stack2;

18    queue<

int

> queue1, queue2;

19  

20    cout <<

"Vector: "

<< endl;

21    vector1.push_back(

1

);

22    vector1.push_back(

2

);

23    vector2.push_back(

30

);

24    cout <<

"size of vector1: "

<< vector1.size() << endl;

25    cout <<

"size of vector2: "

<< vector2.size() << endl;

26    cout <<

"maximum size of vector1: "

<< vector1.max_size() <<

endl;

27    cout <<

"maximum size of vector2: "

<< vector1.max_size() <<

endl;

28    vector1.swap(vector2);

29    cout <<

"size of vector1: "

<< vector1.size() << endl;

30    cout <<

"size of vector2: "

<< vector2.size() << endl;

31    cout <<

"vector1 < vector2? "

<< (vector1 < vector2)

32      << endl << endl;

33  

34    cout <<

"List: "

<< endl;

35    list1.push_back(

1

);

36    list1.push_back(

2

);

37    list2.push_back(

30

);

38    cout <<

"size of list1: "

<< list1.size() << endl;

39    cout <<

"size of list2: "

<< list2.size() << endl;

40    cout <<

"maximum size of list1: "

<< list1.max_size() << endl;

41    cout <<

"maximum size of list2: "

<< list2.max_size() << endl;

42    list1.swap(list2);

43    cout <<

"size of list1: "

<< list1.size() << endl;

44    cout <<

"size of list2: "

<< list2.size() << endl;

45    cout <<

"list1 < list2? "

<< (list1 < list2) << endl << endl;

46  

47    cout <<

"Deque: "

<< endl;

48    deque1.push_back(

1

);

49    deque1.push_back(

2

);

50    deque2.push_back(

30

);

51    cout <<

"size of deque1: "

<< deque1.size() << endl;

52    cout <<

"size of deque2: "

<< deque2.size() << endl;

53    cout <<

"maximum size of deque1: "

<< deque1.max_size() << endl;

54    cout <<

"maximum size of deque2: "

<< deque2.max_size() << endl;

55    list1.swap(list2);

56    cout <<

"size of deque1: "

<< deque1.size() << endl;

57    cout <<

"size of deque2: "

<< deque2.size() << endl;

58    cout <<

"deque1 < deque2? "

<< (deque1 < deque2) << endl << endl;

59  

60    cout <<

"Set: "

<< endl;

61    set1.insert(

1

);

6

62    set1.insert(

1

);

63    set1.insert(

2

);

64    set2.insert(

30

);

65    cout <<

"size of set1: "

<< set1.size() << endl;

66    cout <<

"size of set2: "

<< set2.size() << endl;

67    cout <<

"maximum size of set1: "

<< set1.max_size() << endl;

68    cout <<

"maximum size of set2: "

<< set2.max_size() << endl;

69    set1.swap(set2);

70    cout <<

"size of set1: "

<< set1.size() << endl;

71    cout <<

"size of set2: "

<< set2.size() << endl;

72    cout <<

"set1 < set2? "

<< (set1 < set2) << endl << endl;

73  

74    cout <<

"Multiset: "

<< endl;

75    multiset1.insert(

1

);

76    multiset1.insert(

1

);

77    multiset1.insert(

2

);

78    multiset2.insert(

30

);

79    cout <<

"size of multiset1: "

<< multiset1.size() << endl;

80    cout <<

"size of multiset2: "

<< multiset2.size() << endl;

81    cout <<

"maximum size of multiset1: "

<<

82          multiset1.max_size() << endl;

83    cout <<

"maximum size of multiset2: "

<<

84          multiset2.max_size() << endl;

85    multiset1.swap(multiset2);

86    cout <<

"size of multiset1: "

<< multiset1.size() << endl;

87    cout <<

"size of multiset2: "

<< multiset2.size() << endl;

88    cout <<

"multiset1 < multiset2? "

<<

89          (multiset1 < multiset2) << endl << endl;

90  

91    cout <<

"Stack: "

<< endl;

92    stack1.push(

1

);

93    stack1.push(

1

);

94    stack1.push(

2

);

95    stack2.push(

30

);

96    cout <<

"size of stack1: "

<< stack1.size() << endl;

97    cout <<

"size of stack2: "

<< stack2.size() << endl;

98    cout <<

"stack1 < stack2? "

<< (stack1 < stack2) << endl << endl;

99  

100    cout <<

"Queue: "

<< endl;

101    queue1.push(

1

);

102    queue1.push(

1

);

103    queue1.push(

2

);

104    queue2.push(

30

);

105    cout <<

"size of queue1: "

<< queue1.size() << endl;

106    cout <<

"size of queue2: "

<< queue2.size() << endl;

107    cout <<

"queue1 < queue2? "

<< (queue1 < queue2) << endl << endl;

108  

109   

return

0

;

110 }

Sample output

Vector:

size of vector1: 2

size of vector2: 1

maximum size of vector1: 1073741823

maximum size of vector2: 1073741823

size of vector1: 1

size of vector2: 2

7

vector1 < vector2? 0

List:

size of list1: 2

size of list2: 1

maximum size of list1: 4294967295

maximum size of list2: 4294967295

size of list1: 1

size of list2: 2

list1 < list2? 0

Deque:

size of deque1: 2

size of deque2: 1

maximum size of deque1: 4294967295

maximum size of deque2: 4294967295

size of deque1: 1

size of deque2: 2

deque1 < deque2? 0

Set:

size of set1: 2

size of set2: 1

maximum size of set1: 4294967295

maximum size of set2: 4294967295

size of set1: 1

size of set2: 2

set1 < set2? 0

Multiset:

size of multiset1: 3

size of multiset2: 1

maximum size of multiset1: 4294967295

maximum size of multiset2: 4294967295

size of multiset1: 1

size of multiset2: 3

multiset1 < multiset2? 0

Stack:

size of stack1: 3

size of stack2: 1

stack1 < stack2? 1

Queue:

size of queue1: 3

size of queue2: 1

queue1 < queue2? 1