Question

In: Computer Science

What are some tips and best practices on how to use sets and maps properly in...

What are some tips and best practices on how to use sets and maps properly in C++? Please provide two examples.

Solutions

Expert Solution

Sets and Maps are both used to store unique as well as sorted values. So if you want to use them properly, first you need to learn about their differences and how and when to use them.

Differences:-

  1. Both use "Red Black Tree" for their storage purpose.
  2. sets are just used to store values (some particular values).
  3. Maps are used to store values as pair.

Uses:-

We use sets when we only want to display the sorted values. But when we need to print their frequency of the values, then we use the maps concept here.

Below are two examples for sets and maps.

Source Code:-

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> S;
S.insert(10);
S.insert(5);
S.insert(1);
S.insert(20);
cout<< "Values in Set : ";
for(auto s : S) {
cout << s << " ";
}
return 0;
}

Output :-

Values in Set : 1 5 10 20

Source Code:-

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
std::map<int, int> M;
M[10] = 1010;
M[5] = 101;
M.insert({4, 100});
cout << "Values in Map : ";
for(auto m : M) {
cout<< m.first << " " << m.second << "\n";
}
return 0;
}

Output:-

Values in Map : 4 100

5 101

10 1010

Hope this helps you!!


Related Solutions

how to approach sets and relation type easy moderate problem and show some tips tecniques and...
how to approach sets and relation type easy moderate problem and show some tips tecniques and shortcuts formula ..i would give positive response if you help me a little.... thankyou
What are some best practices for managing budget opportunities (in higher educaton)? Describe these practices.
What are some best practices for managing budget opportunities (in higher educaton)? Describe these practices.
What are some of the best practices for improving an organization's ethical climate?
What are some of the best practices for improving an organization's ethical climate?
what are some of the best supply chain practices for a beverage manufacturer?
what are some of the best supply chain practices for a beverage manufacturer?
Questions to Answer: What are some of the best practices for Security In the Public Cloud?...
Questions to Answer: What are some of the best practices for Security In the Public Cloud? With regard to Security: Do any of the approaches differ in the Public Cloud than they do in on premise deployments? Did you find any any providers guidance (AWS, etc.) on migration and associated security considerations? Provide examples that you encountered within your research and indicate what portions will apply to each stage of your migration. Systems Availability: What issues does Public Cloud computing...
Amongst rank and file employees, how should we distribute compensation? What are some best practices? What...
Amongst rank and file employees, how should we distribute compensation? What are some best practices? What compensation systems do you think are best and why?
Give your perspective on what are some of the best practices in handling customer calls. in...
Give your perspective on what are some of the best practices in handling customer calls. in 150 words
What are some memory tips that you can use to improve your listening skills? What benefits...
What are some memory tips that you can use to improve your listening skills? What benefits does listening offer in your everyday life? Explain why the listening process is easier when you understand the basic stages of listening.
What are some best practices associated with situations whereby prospects bringing up a product sold by...
What are some best practices associated with situations whereby prospects bringing up a product sold by one of your competitors?
Explain some of the best practices that organizations adopted to benefit other organizations?
Explain some of the best practices that organizations adopted to benefit other organizations?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT