C++ Set and Multiset

Last Updated :
Discuss
Comments

Question 1

What is the output of the following code?

#include <iostream>
#include <set>

int main() {
  std::multiset<int> s {1, 2, 3, 3, 3, 4, 4, 5};
  std::cout << s.count(3) << std::endl;
  return 0;
}
  • 3

  • 2

  • 5

  • 8

Question 2

What is the output of the following code?

#include <iostream>
#include <set>

int main() {
  std::multiset<int> s {1, 2, 3, 3, 3, 4, 4, 5};
  s.erase(3);
  for (auto it = s.begin(); it != s.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
  return 0;
}
  • 1 2 3 4 5

  • 1 2 4 4 5

  • 1 2 4 5

  • 2 4 5

Question 3

What will be the output of the following C++ program?

#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> s;

s.insert(10);
s.insert(15);
s.insert(13);

for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
cout<<endl;
// when 10 is present
auto it = s.upper_bound(10);
cout << (*it) << endl;
return 0;
}

c

  • 10 13 15
    13

  • 10 15 13
    13

  • 13 15 10
    15

  • 15 13 10
    10

Question 4

What is the time complexity of clearing all elements from a multiset in the STL?

  • O(1)

  • O(n)

  • O(log n)

  • O(n log n)

Question 5

Which of the following statements about the multiset container class in the STL is NOT true?

  • A multiset is an ordered container that can store duplicate elements.

  • A multiset has fast element insertion and search times.

  • A multiset does not allow element modification or removal.

  • A multiset has constant time complexity for element access.

Question 6

What will be the output of the following C++ code?

#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 14, 12, 15, 11, 10 };

set<int> s(arr, arr + 5);
for (auto it = s.cbegin(); it != s.cend(); it++)
 cout << *it << " ";
return 0;
}
  • 10 11 12 14 15

  • 12 14 15

  • 15 14 13 12 11

  •  13 12 11

Question 7

What is the return type of the find() member function of the set container class in the STL?

  • int

  • bool

  • iterator

  • const_iterator

Question 8

What is the main difference between a set and a vector in the STL?

  • A set is an ordered container, while a vector is an unordered container.

  • A set stores only unique elements, while a vector can store duplicate elements.

  • A set has constant time complexity for element insertion, while a vector has linear time complexity.

  • A set has constant time complexity for element access, while a vector has linear time complexity.

Question 9

Which of the following statements about the set container class in the STL is NOT true?

  • A set is an ordered container that stores only unique elements.

  • A set has fast element insertion and search times.

  • A set is implemented as a binary search tree.

  • A set does not allow element modification or removal.

Question 10

What is the time complexity of searching for an element in a set in the STL?

  • O(1)

  • O(n)

  • O(log n)

  • O(n log n)

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion