Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between using .begin() vs .end() for std::inserter for std::set?

If there is any difference between it1 and it2?

std::set<sometype> s;  auto it1 = std::inserter(s, s.begin()); auto it2 = std::inserter(s, s.end()); 
like image 769
Loom Avatar asked May 06 '11 09:05

Loom


People also ask

What is the time complexity of set :: insert?

Its time complexity is O(logN) where N is the size of the set. insert(): insert a new element. Its time complexity is O(logN) where N is the size of the set. size(): Returns the size of the set or the number of elements in the set.

What does set insert return?

set insert() function in C++ STL Return Value: The function returns an iterator pointing to the inserted element in the container.

How do you add items to a set in C++?

insert() function is an inbuilt function in C++ STL, which is defined in <set> header file. This function is used to insert elements in the set container. when we insert the element the size of the container is increased by the number of the elements inserted.

What is insert in C++?

The vector::insert() function in C++ Basically, the vector::insert() function from the STL in C++ is used to insert elements or values into a vector container. In general, the function returns an iterator pointing to the first of the inserted elements.


1 Answers

In practice, not much. If you're inserting a large number of already in order elements into an empty set, the second will be somewhat faster, but that's about it. std::insert_iterator calls insert with the iterator; std::set interprets it as a hint, and inserts in constant time (rather than lg n) if the insertion is immediately before the hint. (Actually, if the set is empty, I think both will do exactly the same thing.)

like image 85
James Kanze Avatar answered Oct 03 '22 14:10

James Kanze