containers

Obtain iterator from pointer or reference

烂漫一生 提交于 2019-12-18 08:30:13
问题 I would like to know if it is possible to obtain an iterator to an object inside a container (e.g. std::vector<...> ) by only having access to the object inside the container, e.g. through a reference (which implies we have access to a pointer to it using the & operator). For example, normally we declare an iterator as std::vector<int>::iterator = vec.begin(); or std::vector<int>::iterator = next(vec.begin(), idx); but in the first example we are most probably about to iterate through the

C++: How do I pass a container of derived classes to a function expecting a container of their base classes?

老子叫甜甜 提交于 2019-12-18 07:06:47
问题 HI! Anyone know how I can make the line " chug(derlist); " in the code below work? #include <iostream> #include <list> using namespace std; class Base { public: virtual void chug() { cout << "Base chug\n"; } }; class Derived : public Base { public: virtual void chug() { cout << "Derived chug\n"; } void foo() { cout << "Derived foo\n"; } }; void chug(list<Base*>& alist) { for (list<Base*>::iterator i = alist.begin(), z = alist.end(); i != z; ++i) (*i)->chug(); } int main() { list<Base*>

How to index and query STL map containers by multiple keys?

北战南征 提交于 2019-12-18 04:00:21
问题 I came across one requirement where the record is stored as Name : Employee_Id : Address where Name and Employee_Id are supposed to be keys that is, a search function is to be provided on both Name and Employee Id. I can think of using a map to store this structure std::map< std:pair<std::string,std::string> , std::string > // < < Name , Employee-Id> , Address > but I'm not exactly sure how the search function will look like. 回答1: Boost.Multiindex This is a Boost example In the above example

Checking for NaN presence in a container

╄→尐↘猪︶ㄣ 提交于 2019-12-18 03:25:33
问题 NaN is handled perfectly when I check for its presence in a list or a set. But I don't understand how. [UPDATE: no it's not; it is reported as present if the identical instance of NaN is found; if only non-identical instances of NaN are found, it is reported as absent.] I thought presence in a list is tested by equality, so I expected NaN to not be found since NaN != NaN. hash(NaN) and hash(0) are both 0. How do dictionaries and sets tell NaN and 0 apart? Is it safe to check for NaN presence

Checking for NaN presence in a container

*爱你&永不变心* 提交于 2019-12-18 03:23:06
问题 NaN is handled perfectly when I check for its presence in a list or a set. But I don't understand how. [UPDATE: no it's not; it is reported as present if the identical instance of NaN is found; if only non-identical instances of NaN are found, it is reported as absent.] I thought presence in a list is tested by equality, so I expected NaN to not be found since NaN != NaN. hash(NaN) and hash(0) are both 0. How do dictionaries and sets tell NaN and 0 apart? Is it safe to check for NaN presence

Implementing a compile-time “static-if” logic for different string types in a container

谁说胖子不能爱 提交于 2019-12-18 01:06:11
问题 I'd like to write a function template that operates on a container of strings, for example a std::vector . I'd like to support both CString and std::wstring with the same template function. The problem is that CString and wstring have different interfaces, for example to get the "length" of a CString , you call the GetLength() method, instead for wstring you call size() or length() . If we had a "static if" feature in C++, I could write something like: template <typename ContainerOfStrings>

Where are docker images and containers stored when we use it with Windows?

余生颓废 提交于 2019-12-17 23:38:20
问题 NOTE: Am super new to both Windows and Docker The tutorial I've been using says that they are under /var/lib/docker/containers if we're using Linux, but I can't seem to find that on my Windows machine. Thanks! 回答1: Enter docker-machine with docker-machine ssh there you should find your containers. sudo ls /var/lib/docker/containers 回答2: Things might have changed with Windows 10 Anniversary Update. I installed Docker from source here (https://master.dockerproject.org/windows/amd64/docker-1.13

C++ iterator and const_iterator problem for own container class

寵の児 提交于 2019-12-17 23:28:07
问题 I'm writing an own container class and have run into a problem I can't get my head around. Here's the bare-bone sample that shows the problem. It consists of a container class and two test classes: one test class using a std:vector which compiles nicely and the second test class which tries to use my own container class in exact the same way but fails miserably to compile. #include <vector> #include <algorithm> #include <iterator> using namespace std; template <typename T> class MyContainer {

Is it impossible to use an STL map together with a struct as key?

时间秒杀一切 提交于 2019-12-17 23:23:35
问题 I have the following code: struct Node { int a; int b; }; Node node; node.a = 2; node.b = 3; map<int, int> aa; aa[1]=1; // OK. map<Node, int> bb; bb[node]=1; // Compile error. When I tried to map an instance of my struct Node to an int , I got a compile error. Why? 回答1: For a thing to be usable as a key in a map, you have to be able to compare it using operator<() . You need to add such an operator to your node class: struct Node { int a; int b; bool operator<( const Node & n ) const { return

Get Docker Container CPU Usage as Percentage

橙三吉。 提交于 2019-12-17 23:17:20
问题 Docker provides an interactive stats command, docker stats [cid] which gives up to date information on the CPU usage, like so: CONTAINER CPU % MEM USAGE/LIMIT MEM % NET I/O 36e8a65d 0.03% 4.086 MiB/7.798 GiB 0.05% 281.3 MiB/288.3 MiB I'm trying to get the CPU usage as a percentage in a digestible format to do some analysis. I've seen the stats in /sys/fs which seem to provide similar values as the Docker Remote API which gives me this JSON blob: { "cpu_usage": { "usage_in_usermode":