tostring

Is specialization of std::to_string for custom types allowed by the C++ standard?

坚强是说给别人听的谎言 提交于 2019-12-18 13:53:10
问题 In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types? namespace std { string to_string(::MyClass const & c) { return c.toString(); } } Sample use-case: int main() { MyClass c; std::cout << std::to_string(c) << std::endl; } 回答1: In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types? No. First of all, it is not a template function so you can't specialize it at all. If you're asking about adding your

Naming(toString) Lambda-Expressions for Debugging purpose

一笑奈何 提交于 2019-12-18 09:46:26
问题 Sometimes it is usefull to name lambdas. Especially when you pass them around as parameter. A realy simple example is public class Main { public static void main(String[] args) { Predicate<String> p = nameIt("isNotEmpty", (s) -> !s.trim().isEmpty()); maybePrint("Hello", p); maybePrint(" ", p); } static <T> void maybePrint(T s, Predicate<T> pred) { if (pred.test(s)) { System.out.println(s.toString()); } else { System.err.println(pred + " says no to \"" + s + "\""); } } } It would be nice to

StringBuilder.ToString() throws OutOfMemoryException

别来无恙 提交于 2019-12-18 05:44:14
问题 I have a created a StringBuilder of length "132370292", when I try to get the string using the ToString() method it throws OutOfMemoryException . StringBuilder SB = new StringBuilder(); for(int i =0; i<=5000; i++) { SB.Append("Some Junk Data for testing. My Actual Data is created from different sources by Appending to the String Builder."); } try { string str = SB.ToString(); // Throws OOM mostly Console.WriteLine("String Created Successfully"); } catch(OutOfMemoryException ex) { StreamWriter

Dynamically changing the text of items in a Winforms ComboBox

混江龙づ霸主 提交于 2019-12-18 05:05:06
问题 I have a Winforms ComboBox that contains instances of a custom class. When the items are first added to the Items collection of the ComboBox , the ToString method is call on each of them. However when the user changes the language the application is running in, the result of the ToString method changes. Therefore how can I get the ComboBox to call the ToString method on all items again without having to remove all items from the ComboBox and adding them back in? 回答1: Thanks svick,

Dynamically changing the text of items in a Winforms ComboBox

折月煮酒 提交于 2019-12-18 05:04:39
问题 I have a Winforms ComboBox that contains instances of a custom class. When the items are first added to the Items collection of the ComboBox , the ToString method is call on each of them. However when the user changes the language the application is running in, the result of the ToString method changes. Therefore how can I get the ComboBox to call the ToString method on all items again without having to remove all items from the ComboBox and adding them back in? 回答1: Thanks svick,

Python Enum class (with tostring fromstring)

余生长醉 提交于 2019-12-18 04:49:05
问题 I've found a simply way to implement(hack) an enum into Python: class MyEnum: VAL1, VAL2, VAL3 = range(3) I can then call this as such: bob = MyEnum.VAL1 Sexy! Alright, now I want to be able to get both the numerical value if given a string, or a string if given a numerical value. Let's say I want the strings to exactly match up to the Enum key's The best I could think of is something like this: class MyEnum: VAL1, VAL2, VAL3 = range(3) @classmethod def tostring(cls, val): if (val == cls.VAL1

Get Command Prompt Output to String In Java

跟風遠走 提交于 2019-12-18 04:12:47
问题 I need a java method that will read command prompt output and store it into a String to be read into Java. This is what I have so far but isn't working right. public void testGetOutput() { System.out.println("\n\n****This is the testGetOutput Method!****"); String s = null; String query = "dir " + this.desktop; try { Runtime runtime = Runtime.getRuntime(); InputStream input = runtime.exec("cmd /c " + query).getInputStream(); BufferedInputStream buffer = new BufferedInputStream(input);

Difference between Object.prototype.toString.call(arrayObj) and arrayObj.toString() [duplicate]

寵の児 提交于 2019-12-18 03:37:33
问题 This question already has answers here : Why “foo”.toString() is not the same as toString.call(“foo”)? (3 answers) Closed 4 years ago . I have gone through some materials and haven't completely grasped the concept when it comes to syntax such as the following : var arrObj = [1,2,3]; Object.prototype.toString.call(arrObj); //Gives "[object Array]" arrObj.toString(); // Gives "1,2,3" How are the lines 2 and 3 different? As far as I understood, both call the toString method with current object

Why toString() method works differently between Array and ArrayList object in Java

末鹿安然 提交于 2019-12-17 22:41:58
问题 String[] array = {"a","c","b"}; ArrayList<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); System.out.println(array); System.out.println(list); For list [a, b, c] is output while for array some address is output. When we want to output the array values, we can use Arrays.toString(array); which works just like list . I just wonder why we can't call toString() directly on array to get the values. Isn't it more intuitive and convenient to do so? What results

Printing out a linked list using toString

Deadly 提交于 2019-12-17 19:50:54
问题 Ok guys, so I am trying to learn how to print out a linked list. I have all the methods that I would need to use for the list, but I can't figure out how to display the values of the nodes. Right now there is nothing in my main method because I kept getting errors trying to call non static methods in the main. I have a toString method that displays the contents of the list. How would I go about calling this toString to display the value of each node? Any advice will be greatly appreciated.