How do I copy a stack in Java?

Just use the clone() -method of the Stack-class (it implements Cloneable). Here’s a simple test-case with JUnit: @Test public void test() { Stack<Integer> intStack = new Stack<Integer>(); for(int i = 0; i < 100; i++) { intStack.push(i); } Stack<Integer> copiedStack = (Stack<Integer>)intStack.clone(); for(int i = 0; i < 100; i++) { Assert.assertEquals(intStack.pop(), copiedStack.pop()); } } … Read more

Copying non null-terminated unsigned char array to std::string

std::string has a constructor that takes a pair of iterators and unsigned char can be converted (in an implementation defined manner) to char so this works. There is no need for a reinterpret_cast. unsigned char u_array[4] = { ‘a’, ‘s’, ‘d’, ‘f’ }; #include <string> #include <iostream> #include <ostream> int main() { std::string str( u_array, … Read more

How to copy text to clipboard/pasteboard with Swift

If all you want is plain text, you can just use the string property. It’s both readable and writable: // write to clipboard UIPasteboard.general.string = “Hello world” // read from clipboard let content = UIPasteboard.general.string (When reading from the clipboard, the UIPasteboard documentation also suggests you might want to first check hasStrings, “to avoid causing … Read more