insert-iterator

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

半腔热情 提交于 2019-12-29 06:34:07
问题 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()); 回答1: 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

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

主宰稳场 提交于 2019-11-29 05:25:26
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()); James Kanze 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.) From http:/

Insert into an STL queue using std::copy

佐手、 提交于 2019-11-27 22:09:54
I'd like to use std::copy to insert elements into a queue like this: vector<int> v; v.push_back( 1 ); v.push_back( 2 ); queue<int> q; copy( v.begin(), v.end(), insert_iterator< queue<int> >( q, q.front() ) ); But this fails to compile, complaining that begin is not a member of std::queue . Note: I tried it with std::inserter too - this also failed, this time saying that 'reference' is not a member of 'std::queue'. std::back_inserter and std::back_insert_iterator also fail with the same error. Am I missing something obvious, or do insert_iterator s just not work with queues? Unfortunately std:

Insert into an STL queue using std::copy

只愿长相守 提交于 2019-11-26 20:55:51
问题 I'd like to use std::copy to insert elements into a queue like this: vector<int> v; v.push_back( 1 ); v.push_back( 2 ); queue<int> q; copy( v.begin(), v.end(), insert_iterator< queue<int> >( q, q.front() ) ); But this fails to compile, complaining that begin is not a member of std::queue . Note: I tried it with std::inserter too - this also failed, this time saying that 'reference' is not a member of 'std::queue'. std::back_inserter and std::back_insert_iterator also fail with the same error.