casting

like atoi but to float

☆樱花仙子☆ 提交于 2020-01-22 13:31:56
问题 Is there a function similar to atoi which converts a string to float instead of to integer? 回答1: atof() (or std::atof() talking C++ - thanks jons34yp) 回答2: boost::lexical_cast<float>(str); This template function is included in the popular Boost collection of libraries, which you'll want learn about if you're serious about C++. 回答3: Convert a string to any type (that's default-constructible and streamable): template< typename T > T convert_from_string(const std::string& str) { std:

Java Generics casts strangely [duplicate]

孤街醉人 提交于 2020-01-22 08:01:10
问题 This question already has answers here : Why does this use of Generics not throw a runtime or compile time exception? (3 answers) Closed 2 years ago . I am using java 8. I recently came across this: public class Test { public static void main(String[] args) { String ss = "" + (Test.<Integer>abc(2)); System.out.println(Test.<Integer>abc(2)); } public static <T> T abc(T a) { String s = "adsa"; return (T) s; } } This does not throw a java.lang.ClassCastException. Why is that? I always thought +

Why doesn't Kotlin perform automatic type-casting?

心不动则不痛 提交于 2020-01-21 07:23:12
问题 var a : Double a = Math.sin(10) // error: the integer literal does not conform to the expected type Double a = Math.sin(10.0) //This compiles successfully println(a) Why doesn't kotlin perform implicit type conversion and force us to pass the exact type of data? fun sin(value: Double): Double // at kotlin documentation 回答1: We all know that Kotlin has both non-nullable Int and nullable Int? . When we use Int? this happens: Kotlin actually 'boxes' JVM primitives when Kotlin needs a nullable

how can i generate a unique int from a unique string?

淺唱寂寞╮ 提交于 2020-01-20 15:22:48
问题 I have an object with a String that holds a unique id . (such as "ocx7gf" or "67hfs8") I need to supply it an implementation of int hascode() which will be unique obviously. how do i cast a string to a unique int in the easiest/fastest way? 10x. Edit - OK. I already know that String.hashcode is possible. But it is not recommended in any place. Actually' if any other method is not recommended - Should I use it or not if I have my object in a collection and I need the hashcode. should I concat

how can i generate a unique int from a unique string?

狂风中的少年 提交于 2020-01-20 15:14:54
问题 I have an object with a String that holds a unique id . (such as "ocx7gf" or "67hfs8") I need to supply it an implementation of int hascode() which will be unique obviously. how do i cast a string to a unique int in the easiest/fastest way? 10x. Edit - OK. I already know that String.hashcode is possible. But it is not recommended in any place. Actually' if any other method is not recommended - Should I use it or not if I have my object in a collection and I need the hashcode. should I concat

MongoDb - Change type from Int to Double

无人久伴 提交于 2020-01-20 07:58:35
问题 We have a collection that looks like this: { "_id" : "10571:6", "v" : 261355, "ts" : 4.88387e+008 } Now, some of the "v" are ints, some are doubles. I want to change them all to doubles. I've tried a few things but nothing works (v is an int32 for this record, I want to change it to a double): db.getCollection('VehicleLastValues') .find ( {_id : "10572:6"} ) .forEach ( function (x) { temp = x.v * 1.0; db.getCollection('VehicleLastValues').save(x); }} Things I've tried: x.v = x.v * 1.1 / 1.1;

MongoDb - Change type from Int to Double

浪尽此生 提交于 2020-01-20 07:58:15
问题 We have a collection that looks like this: { "_id" : "10571:6", "v" : 261355, "ts" : 4.88387e+008 } Now, some of the "v" are ints, some are doubles. I want to change them all to doubles. I've tried a few things but nothing works (v is an int32 for this record, I want to change it to a double): db.getCollection('VehicleLastValues') .find ( {_id : "10572:6"} ) .forEach ( function (x) { temp = x.v * 1.0; db.getCollection('VehicleLastValues').save(x); }} Things I've tried: x.v = x.v * 1.1 / 1.1;

VB.NET: Boolean from `Nothing` sometimes `false`, sometimes Nullreference-Exception

情到浓时终转凉″ 提交于 2020-01-20 03:27:07
问题 Coming from Basic boolean logic in C# , I was wondering why: Dim b As Boolean Dim obj As Object = Nothing 'followig evaluates to False' b = DirectCast(Nothing, Boolean) 'This throws an "Object reference not set to an instance of an object"-Exception' b = DirectCast(obj, Boolean) A CType(obj, Boolean) would evaluate to False (just as CBool(obj)) . I think it is because the compiler uses a helper function, but that is not my theme. Why does casting Nothing to Boolean evaluates to False ,

Is using const_cast for read-only access to a const object allowed?

不问归期 提交于 2020-01-19 16:22:19
问题 In C++ I have a function that only requires read-only access to an array but is mistakenly declared as receiving a non-const pointer: size_t countZeroes( int* array, size_t count ) { size_t result = 0; for( size_t i = 0; i < count; i++ ) { if( array[i] == 0 ) { ++result; } } return result; } and I need to call it for a const array: static const int Array[] = { 10, 20, 0, 2}; countZeroes( const_cast<int*>( Array ), sizeof( Array ) / sizeof( Array[0] ) ); will this be undefined behaviour? If so

casting between base class and child classes

纵饮孤独 提交于 2020-01-17 15:00:08
问题 I have a question about casting between a base class and its child classes: (1) Why is this allowed? BaseClass b = new ChildClassA(); ChildClassA c = (ChildClassA)b (2) Why is this not allowed? ChildClassA c = (ChildClassA)new BaseClass(); BaseClass b = (BaseClass)c; (3) Why is this allowed? BaseClass b = new BaseClass(); ChildClassA c = (ChildClassA)c; (4) Why is this allowed? ChildClassA c = new ChildClassA(); BaseClass b = (BaseClass)c; 回答1: The reasons the cast is either allowed or not