std

How I can define a list of map::iterator and map of list::iterator

扶醉桌前 提交于 2019-12-10 09:23:39
问题 I need a list of Map::iterator and map of List::iterator. How I can do this: typedef std::list<Map::iterator> List; typedef std::map<int, List::iterator> Map; Maybe I can use something like forward declaration for iterator? 回答1: Something like this should help you: #include <cassert> #include <iostream> #include <list> #include <map> #include <string> struct decl_t { typedef std::map<std::string, decl_t> map_t; typedef std::list<std::pair<int, typename map_t::iterator>> list_t; list_t:

How to match only those numbers which have an even number of `%`s preceding them?

与世无争的帅哥 提交于 2019-12-10 04:24:36
问题 I want to catch numbers appearing anywhere in a string, and replace them with "(.+)". But I want to catch only those numbers which have an even number of % s preceding them. No worries if any surrounding chars get caught up: we can use capture groups to filter out the numbers. I'm unable to come up with an ECMAscript regular expression. Here is the playground: abcd %1 %%2 %%%3 %%%%4 efgh abcd%12%%34%%%666%%%%11efgh A successful catch will behave like this: Things I have tried: If you have

Do custom container iterators guarantee ADL to consider namespace std?

∥☆過路亽.° 提交于 2019-12-10 04:11:58
问题 I have no intention of using this in real code. I promise. Does the standard guarantee that std namespace is going to be found when a function argument is of type container::iterator and container::iterator isn't a typedef for a built-in type? For example #include <set> #include <algorithm> int main() { std::set<int> s; find(s.begin(), s.end(), 0); //do I have a guarantee that std::find will be found? } In other words, can the iterator class be defined in such a namespace that std won't be

Is os.popen really deprecated in Python 2.6?

泪湿孤枕 提交于 2019-12-10 03:54:41
问题 The on-line documentation states that os.popen is now deprecated. All other deprecated functions duly raise a DeprecationWarning. For instance: >>> import os >>> [c.close() for c in os.popen2('ps h -eo pid:1,command')] __main__:1: DeprecationWarning: os.popen2 is deprecated. Use the subprocess module. [None, None] The function os.popen, on the other hand, completes silently: >>>len(list(os.popen('ps h -eo pid:1,command'))) 202 Without raising a warning. Of the three possible scenarios It is

Why is std::less better than “<”?

谁都会走 提交于 2019-12-10 03:19:17
问题 C++ primer, 5th, 14.8.2, Using a Library Function Object with the Algorithms: vector<string *> nameTable; // vector of pointers // error: the pointers in nameTable are unrelated, so < is undefined sort(nameTable.begin(), nameTable.end(), [](string *a, string *b) { return a < b; }); // ok: library guarantees that less on pointer types is well defined sort(nameTable.begin(), nameTable.end(), less<string*>()); Then I checked the std::less implementation: template<typename _Tp> struct less :

std::all_of not accepting class member function as function with 1 argument

…衆ロ難τιáo~ 提交于 2019-12-09 23:55:32
问题 I can't figure what I am doing wrong with this std::all_of call. I have a class Statistics: class Statistics { public: bool isDataSet() const { return m_data.size() > 0; } private: std::vector<double> m_data; }; Each instance of Statistics class corresponds to a certain object. In another function in a different file I want to display statistics only if data has been initialized in all Statistics instances. I want to use std::all_of function in the following manner: if( std::all_of(m_stats

An std container inside a template method

喜你入骨 提交于 2019-12-09 18:06:26
问题 Greetings. I don't know very well how to explain myself, but I believe a piece of code will make you understand what I'm intenting to do : template<class A, class B> void myFunction(A<B>& list) { typename A<B>::iterator current = list.begin(); typename A<B>::iterator end = list.end(); while (current != end) { current++; } } Where A is an STL container (vector, list...). It's like inception, but with templates : a template, inside a template, etc... The thing is : what do you do when one of

Index of minimum element in a std::list

怎甘沉沦 提交于 2019-12-09 16:41:22
问题 If I have a std::vector<int> , I can obtain the index of the minimum element by subtracting two iterators: int min_index = std::min_element(vec.begin(), vec.end()) - vec.begin(); However, with containers that don't have random access iterators, for example a std::list<int> , this doesn't work. Sure, it is possible to do something like int min_index = std::difference(l.begin(), std::min_element(l.begin(), l.end())); but then I have to iterate twice through the list. Can I get the index of the

Why does std::vector work with incomplete types in class definitions?

社会主义新天地 提交于 2019-12-09 16:41:07
问题 The following question came up: The c++ standard seems to say, that std::vector requires a complete type to work. (See https://en.cppreference.com/w/cpp/container/vector ) Then, why does the following code still compile? #include <vector> struct parent; struct child { std::vector<parent> parents; //parent is incomplete here! }; struct parent { std::vector<child> children; }; This seems counterintuitive. If std::vector requires a complete type, then std::vector<parent> should not compile

C++: “vector<int>::size_type variable” - what is the point of declaring in this way?

拈花ヽ惹草 提交于 2019-12-09 14:19:47
问题 I think this is a very basic question but I couldn't just figure it out. I was used to using arrays in C++ but I'm now starting to learn vectors. I was making a test code, and I came across a question. First of all, here's the code I made: #include <iostream> #include <vector> #include <numeric> using namespace std; int main(){ vector<double> score(10); for(vector<double>::size_type i=0;i<20;i++) { cout<<"Enter marks for student #"<<i+1<<":"<<flush; cin>>score[i]; } double total = accumulate