大约有 6,000 项符合查询结果(耗时:0.0236秒) [XML]
How to parse a string to an int in C++?
...ng.
This leads me to my second piece of advice: do no use Boost's lexical_cast for this. Consider what the lexical_cast documentation has to say:
Where a higher degree of control is
required over conversions,
std::stringstream and
std::wstringstream offer a more
appropriate path. Where
...
How can I convert a long to int in Java?
...ted, in Java 8:
Math.toIntExact(value);
Original Answer:
Simple type casting should do it:
long l = 100000;
int i = (int) l;
Note, however, that large numbers (usually larger than 2147483647 and smaller than -2147483648) will lose some of the bits and would be represented incorrectly.
For ...
How do I make the method return type generic?
...nimal> T callFriend(String name, Class<T> type) {
return type.cast(friends.get(name));
}
Then call it as such:
jerry.callFriend("spike", Dog.class).bark();
jerry.callFriend("quacker", Duck.class).quack();
This code has the benefit of not generating any compiler warnings. Of course ...
How can I output the value of an enum class in C++11
...ts integer value. You need to explicitly convert it to an integer using a cast:
std::cout << static_cast<std::underlying_type<A>::type>(a) << std::endl;
You may want to encapsulate the logic into a function template:
template <typename Enumeration>
auto as_integer(...
How can I convert a std::string to int?
...stinguish between bad input (see here).
4. Fourth option: Boost's lexical_cast
#include <boost/lexical_cast.hpp>
#include <string>
std::string str;
try {
int i = boost::lexical_cast<int>( str.c_str());
float f = boost::lexical_cas...
C++ equivalent of java's instanceof
...
Try using:
if(NewType* v = dynamic_cast<NewType*>(old)) {
// old was safely casted to NewType
v->doSomething();
}
This requires your compiler to have rtti support enabled.
EDIT:
I've had some good comments on this answer!
Every time you need...
Select SQL Server database size
...y:
SELECT
database_name = DB_NAME(database_id)
, log_size_mb = CAST(SUM(CASE WHEN type_desc = 'LOG' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
, row_size_mb = CAST(SUM(CASE WHEN type_desc = 'ROWS' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
, total_size_mb = CAST(SUM(size) * 8. ...
Performance surprise with “as” and nullable types
...d is of the expected type, takes but a few machine code instructions. The cast is also easy, the JIT compiler knows the location of the value bits in the object and uses them directly. No copying or conversion occurs, all machine code is inline and takes but about a dozen instructions. This neede...
Ignore Typescript Errors “property does not exist on value of type”
...erHtmlElement.getBBox();
###Edit, late 2016
Since TypeScript 1.6 prefered casting operator is as those lines can be squashed into:
let coordinates = (outerElement[0] as any).getBBox();
###Other solutions
Of course if you'd like to do it right, which is an overkill sometimes, you can:
Create own i...
How to loop through all enum values in C#? [duplicate]
...Foos));
Or the typed version:
var values = Enum.GetValues(typeof(Foos)).Cast<Foos>();
I long ago added a helper function to my private library for just such an occasion:
public static class EnumUtil {
public static IEnumerable<T> GetValues<T>() {
return Enum.GetVa...