stl

Where does unordered_map<K, V>::iterator come from?

ぐ巨炮叔叔 提交于 2021-02-19 05:40:07
问题 When I'm using a std::unordered_map<K, V> I know that the iterator to each key-value pair is of type std::unordered_map<K, V>::iterator . I also know that the iterator itself points to a pair<const K, V> . However, the only reason I know the iterator points to a pair is from looking at example code. Where is this behavior defined? For example, if I go to the documentation at cppreference.com, I don't see where this behavior is explained. It only says that the member iterator is defined as a

How do I declare a vector of vector::size_type?

折月煮酒 提交于 2021-02-19 03:52:07
问题 I want a vector whose elements are of the type vector::size_type However, you cannot declare: vector<vector::size_type> aVec; because size_type is part of the template itself, so you have to use the type itself, I need something like: vector<vector<T>::size_type> aVec; but what should the T be? It's really a circular problem. :) If vector had just used size_t as its size type (but not had a special typedef for the size_type which could vary maybe based on the type that the vector is holding),

How do I declare a vector of vector::size_type?

◇◆丶佛笑我妖孽 提交于 2021-02-19 03:52:05
问题 I want a vector whose elements are of the type vector::size_type However, you cannot declare: vector<vector::size_type> aVec; because size_type is part of the template itself, so you have to use the type itself, I need something like: vector<vector<T>::size_type> aVec; but what should the T be? It's really a circular problem. :) If vector had just used size_t as its size type (but not had a special typedef for the size_type which could vary maybe based on the type that the vector is holding),

Where is the performance gain of the erase-remove idiom coming from

南笙酒味 提交于 2021-02-19 03:15:40
问题 I need to erase all elements from a vector which fulfill a certain criteria. My first approach would be to loop through the vector and call vector::erase on all elements which fulfill the criteria. As far as I understand, vector::erase has a bad performance for this use case, because it removes the item from the underlying array, and moves the rest of the vector forward by one element (or more if you erase a range of elements). When you remove multiple elements, the rear elements will be

Find the first character that is not whitespace in a std::string

只愿长相守 提交于 2021-02-19 03:01:04
问题 Lets say I have std::wstring str(L" abc"); The contents of the string could be arbitrary. How can I find the first character that is not whitespace in that string, i.e. in this case the position of the 'a'? 回答1: This should do it (C++03 compatible, in C++11 you can use a lambda): #include <cwctype> #include <functional> typedef int(*Pred)(std::wint_t); std::string::iterator it = std::find_if( str.begin(), str.end(), std::not1<Pred>(std::iswspace) ); It returns an iterator, subtract str.begin(

Step/Stride Iterator for use with std::minmax_element

孤街浪徒 提交于 2021-02-18 16:56:31
问题 I have a 1D float array which represents a m *n (rows and columns) table of float values. My requirement is to find a min/max element for each row and column. For rows I can easily do it by using std::minmax_element by specifying a range of n elements. But for columns I need to use a stride iterator as elements are placed are not contiguous but placed at a step interval of n. Is there a standard iterator in boost/STL that can be used. The other option is to write my own version of it. What is

Step/Stride Iterator for use with std::minmax_element

只愿长相守 提交于 2021-02-18 16:56:24
问题 I have a 1D float array which represents a m *n (rows and columns) table of float values. My requirement is to find a min/max element for each row and column. For rows I can easily do it by using std::minmax_element by specifying a range of n elements. But for columns I need to use a stride iterator as elements are placed are not contiguous but placed at a step interval of n. Is there a standard iterator in boost/STL that can be used. The other option is to write my own version of it. What is

Can range-based for loops be aware of the end?

天大地大妈咪最大 提交于 2021-02-18 09:52:13
问题 Given the minimal C++11 STL example: set<int> S = {1,2,3,4}; for(auto &x: S) { cout << x; cout << ","; } Is there a way to check if x is the one right before the end? The goal in this example is to output 1,2,3,4 and not the final comma at the end. Currently I use a standard for loop with two iterators, set<int>::const_iterator itr; set<int>::const_iterator penultimate_end_itr = --S.end(); for(itr=S.begin(); itr!=penultimate_end_itr;++itr) cout << (*itr) << ','; cout << (*penultimate_end_itr)

Can range-based for loops be aware of the end?

限于喜欢 提交于 2021-02-18 09:51:30
问题 Given the minimal C++11 STL example: set<int> S = {1,2,3,4}; for(auto &x: S) { cout << x; cout << ","; } Is there a way to check if x is the one right before the end? The goal in this example is to output 1,2,3,4 and not the final comma at the end. Currently I use a standard for loop with two iterators, set<int>::const_iterator itr; set<int>::const_iterator penultimate_end_itr = --S.end(); for(itr=S.begin(); itr!=penultimate_end_itr;++itr) cout << (*itr) << ','; cout << (*penultimate_end_itr)

using erase with remove_if

若如初见. 提交于 2021-02-17 04:53:09
问题 This thing drive me crazy, I couldn't understand it : // v contains : 101, 1, 2, 3, 4 , 5 v.erase(remove_if(v.begin(), v.end(), bind2nd(less<int>(), 3)), v.end()); // v contains now : 101, 3, 4, 5 Why there is v.end() as a second argument in erase ?? I read that remove_if returns an iterator to the element that follows the last element not removed, what does it mean.... v.erase(v.begin(), v.end()) should erase all the vector elements, but how in the example above it does not erase 3, 4 and 5