Question

In C++ please. 4. Explain why inserting an element into map with insert() is considered "safe"...

In C++ please.

4. Explain why inserting an element into map with insert() is
   considered "safe" while inserting with indexing operator is
   considered "unsafe" with respect to an existing element. Give
   example code which demonstrates the safety of element insert with
   insert()
0 0
Add a comment Improve this question Transcribed image text
Answer #1
# Inserting an element into map with insert() is always considered as  "safe" :

In c++ map::insert() is a built-in function (STL) which is used to insert elements with a specific key in the map container.

INSERT is better from the point of safety (it is exceptionally good)

The expression map[key] = value is having two operations:

  1. map[key] -           map[key] is used for creating a map element with default value.
  2. = value -             value is used for copying the value into that element.

Sometimes an exception may occur at the second step. Then result the operation in that case will be only partially done (a new element was added into map, but that element was not initialized with value pair). In this situation when an operation is not complete, but the system state is modified .

insert operation always gives a strong guarantee, means it will never have side effects because insert is either completely done or it leaves the map in unmodified state.

  1. Syntax for insert():
    iterator map_name.insert({key, element})
    

    Parameters: This function accepts a pair of key and element which must be inserted into the map container. if the key already exists in the map then function does not insert the key and element in the map .

    Return Value: The function always returns an iterator pointing to the new element in the container.

# Example code :


#include <bits/stdc++.h>
using namespace std;

int main()
{

  
   map<int, int> mp;     // This line initialize the container

  
   mp.insert({ 2, 30 }); // These lines insert elements in random order
   mp.insert({ 1, 40 });   // These lines insert elements in random order
    mp.insert({ 3, 60 });    // These lines insert elements in random order

    cout << "KEY\tELEMENT\n"; // this line prints the elements
   for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
       cout << itr->first
           << '\t' << itr->second << '\n';
   }
   return 0;
}

Output of the code :

KEY     ELEMENT
1       40
2       30
3       60
Add a comment
Know the answer?
Add Answer to:
In C++ please. 4. Explain why inserting an element into map with insert() is considered "safe"...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ please answer b to e P " b) (2 pts) When we insert an element,...

    c++ please answer b to e P " b) (2 pts) When we insert an element, 5, to the following max heap, what would be the resulting max heap? Give the detailed procedure. 20 c) (2 pts) Based on b), when we remove the root element in the max heap, what would be the resulting max heap? Give the detailed procedure. d) (7 pts) Describe how HeapSort works. Then use the example 20 10 5 15 35 30 25 to...

  • in C++ 1. Answer the following questions: a) 2 pts] Explain why it is important to...

    in C++ 1. Answer the following questions: a) 2 pts] Explain why it is important to indent code. What happens if code is not indented? b) 2 pts) Explain how you can prevent the unintended use of garbage values? c) (2 pts] Give one reason why you should use the keyword const when dealing with constant values. Make sure to explain what could happen if you fail to use this keyword. d) 2 pts) Give an example where simply declaring...

  • Mathematics - Coding Theory Please give an example (or say why the example can not exist)...

    Mathematics - Coding Theory Please give an example (or say why the example can not exist) of a code in which contains a codeword of odd weight. Please explain why you say this. The trivial code and the repetition code does not count! C = (x + 1)(x)) R4

  • in c++ please. thank you! Page 4 of 4 5. Heap and heapsort: answer the following...

    in c++ please. thank you! Page 4 of 4 5. Heap and heapsort: answer the following three questions. a) (1 pt) What is the definition of a max heap? | 0 b) (2 pts) When we insert an element, 5, to the following max heap, what would be the resulting max heap? Give the detailed procedure. (14) (10) c) (2 pts) Based on b), when we remove the root element in the max heap, what would be the resulting max...

  • Can you please help with this question? 4. a) Strain energy component is not considered while...

    Can you please help with this question? 4. a) Strain energy component is not considered while dealing with the n during solidification. Why? (2) while dealing with the nucleation kinetics event b) What would be the effect of higher undercooling on the interian or higher undercooling on the interlamellar spacing of pearlite and explain why? (2) c) Coring is a big headache in alloys having a wide freezing range. Explain why. (2) d) State the difference between natural aging and...

  • Please include all working, i'll give a thumbs up. Thank you. 4. When a radioactive element...

    Please include all working, i'll give a thumbs up. Thank you. 4. When a radioactive element decays, it is because the balance of neutrons and protons inside the nucleus was unstable. Explain what happens in each of these kinds of decay to make the nucleus more stable (these are the three most common decay types): a. Fission b. Beta-decay (B) C. Alpha-decay (a) Which of these is more often associated with the production of energy (and weapons)? Why do you...

  • Please explain why 1010???? and also why c can be represent in that way? please draw table error...

    please explain why 1010???? and also why c can be represent in that way? please draw table error code 9:07 No SIM For an integer n greater than or equal to 0, a code g that associates it with a 4-bit code word g (n) is obtained as shown on the right, but it is assumed that the following condition is satisfied 10001 2 0011 3 0010 0110 5 0111 6 0101 7 0100 8 1100, 9 1101 . For...

  • Please explain why or why not A B C and D can be displaced! thanks! 4....

    Please explain why or why not A B C and D can be displaced! thanks! 4. X is a potential leaving group and could be displaced by the following reaction. + X Indicate if X is displaced when a. CH CH3 161.. b. Br c. OH and d. OCOCH3

  • (a) In 4 or 5 sentences, explain why can we expect companies in the private sector...

    (a) In 4 or 5 sentences, explain why can we expect companies in the private sector (if left alone) to generate pollution? (b) Give one real life specific example of an actual company polluting the environment. Explain what this pollution is and who is hurt by it. (C) When positive externalities exist, a deadweight loss is created. In one or two paragraphs, please explain why a deadweight is created when positive externalities exist and what this deadweight loss represents.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT