containers

How to change the size of the font of a JLabel to take the maximum size

大憨熊 提交于 2019-11-26 16:14:31
I have a JLabel in a Container. The defaut size of the font is very small. I would like that the text of the JLabel to take the maximum size. How can I do that? coobird Not the most pretty code, but the following will pick an appropriate font size for a JLabel called label such that the text inside will fit the interior as much as possible without overflowing the label: Font labelFont = label.getFont(); String labelText = label.getText(); int stringWidth = label.getFontMetrics(labelFont).stringWidth(labelText); int componentWidth = label.getWidth(); // Find out how much the font can grow in

heapq with custom compare predicate

女生的网名这么多〃 提交于 2019-11-26 16:03:37
I am trying to build a heap with a custom sort predicate. Since the values going into it are of 'user-defined' type, I cannot modify their built-in comparison predicate. Is there a way to do something like: h = heapq.heapify([...], key=my_lt_pred) h = heapq.heappush(h, key=my_lt_pred) Or even better, I could wrap the heapq functions in my own container so I don't need to keep passing the predicate. jsbueno According to the heapq documentation , the way to customize the heap order is to have each element on the heap to be a tuple, with the first tuple element being one that accepts normal

Getting container/parent object from within python

五迷三道 提交于 2019-11-26 15:48:03
问题 In Python, is it possible to get the object, say Foo, that contains another object, Bar, from within Bar itself? Here is an example of what I mean class Foo(object): def __init__(self): self.bar = Bar() self.text = "Hello World" class Bar(object): def __init__(self): self.newText = foo.text #This is what I want to do, #access the properties of the container object foo = Foo() Is this possible? Thanks! 回答1: Pass a reference to the Bar object, like so: class Foo(object): def __init__(self):

How can I write a generic container class that implements a given interface in C#?

我的未来我决定 提交于 2019-11-26 15:46:37
问题 Context: .NET 3.5, VS2008. I'm not sure about the title of this question, so feel free to comment about the title, too :-) Here's the scenario: I have several classes, say Foo and Bar, all of them implement the following interface: public interface IStartable { void Start(); void Stop(); } And now I'd like to have a container class, which gets an IEnumerable<IStartable> as an argument in its constructor. This class, in turn, should also implement the IStartable interface: public class

Why use non-member begin and end functions in C++11?

爷,独闯天下 提交于 2019-11-26 15:44:01
Every standard container has a begin and end method for returning iterators for that container. However, C++11 has apparently introduced free functions called std::begin and std::end which call the begin and end member functions. So, instead of writing auto i = v.begin(); auto e = v.end(); you'd write using std::begin; using std::end; auto i = begin(v); auto e = end(v); In his talk, Writing Modern C++ , Herb Sutter says that you should always use the free functions now when you want the begin or end iterator for a container. However, he does not go into detail as to why you would want to.

How to check if a process is running inside docker container

若如初见. 提交于 2019-11-26 15:36:50
问题 [Updated1] I have a shell which will change TCP kernel parameters in some functions, but now I need to make this shell run in Docker container, that means, the shell need to know it is running inside a container and stop configuring the kernel. Now I'm not sure how to achieve that, here is the contents of /proc/self/cgroup inside the container: 9:hugetlb:/ 8:perf_event:/ 7:blkio:/ 6:freezer:/ 5:devices:/ 4:memory:/ 3:cpuacct:/ 2:cpu:/docker

Ad hoc polymorphism and heterogeneous containers with value semantics

荒凉一梦 提交于 2019-11-26 15:32:16
问题 I have a number of unrelated types that all support the same operations through overloaded free functions (ad hoc polymorphism): struct A {}; void use(int x) { std::cout << "int = " << x << std::endl; } void use(const std::string& x) { std::cout << "string = " << x << std::endl; } void use(const A&) { std::cout << "class A" << std::endl; } As the title of the question implies, I want to store instances of those types in an heterogeneous container so that I can use() them no matter what

What is the right approach when using STL container for median calculation?

吃可爱长大的小学妹 提交于 2019-11-26 15:31:39
问题 Let's say I need to retrieve the median from a sequence of 1000000 random numeric values. If using anything but std::list , I have no (built-in) way to sort sequence for median calculation. If using std::list , I can't randomly access values to retrieve middle (median) of sorted sequence. Is it better to implement sorting myself and go with e.g. std::vector , or is it better to use std::list and use std::list::iterator to for-loop-walk to the median value? The latter seems less overheadish,

General use cases for C++ containers

☆樱花仙子☆ 提交于 2019-11-26 15:15:41
问题 What are the general use cases for the C++ standard library containers? bitset deque list map multimap multiset priority_queue queue set stack vector For example, a map is generally better for a paired search. 回答1: A picture is worth a thousand words. It's available from nolyc , the informative bot of ##C++ on Freenode, using the command "container choice" or "containerchoice". The link to this picture you receive in response is hosted at adrinael.net , which suggests we should thank Adrinael

What&#39;s the difference between ClusterIP, NodePort and LoadBalancer service types in Kubernetes?

泪湿孤枕 提交于 2019-11-26 15:01:43
问题 1 - I'm reading the documentation and I'm slightly confused with the wording. It says: ClusterIP : Exposes the service on a cluster-internal IP. Choosing this value makes the service only reachable from within the cluster. This is the default ServiceType NodePort : Exposes the service on each Node’s IP at a static port (the NodePort). A ClusterIP service, to which the NodePort service will route, is automatically created. You’ll be able to contact the NodePort service, from outside the